Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
#19: add abstract class as parser, reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ssaru committed Jun 30, 2019
1 parent 0a8383a commit 2e7b883
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
build:
pipreqs ./ --force

test:
pip3 install -r dev_env/requirements.txt
pytest --pylint --mypy --doctest-modules

format:
isort -y
black icarebot

dep:
pip3 install -r requirements.txt
Empty file added __init__.py
Empty file.
3 changes: 3 additions & 0 deletions base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from base.base_reader import *
from base.base_parser import *

35 changes: 35 additions & 0 deletions base/base_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import logging
import json


class Parser:

ALLOW_EXTENSION = 'json'

def __init__(self, proto_path: str):
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)
self.proto: dict = self._load_proto(proto_path)

def __call__(self, *args, **kwargs):
raise NotImplementedError

def _load_proto(self, proto_file_path: str):
self._is_json(proto_file_path)
with open(proto_file_path) as f:
proto = json.load(f)

return proto

def _is_json(self, path: str) -> None:
extension: str = path.split(".")[-1].lower()
if extension != "json":
self.logger.debug("Debug: proto file is not json")
raise RuntimeError("file is not json")

if __name__ == "__main__":
proto_path = "../config/proto.json"
parser = Parser(proto_path)
_proto = parser.proto
print(_proto)
print(type(_proto))
print(_proto.keys())
13 changes: 13 additions & 0 deletions base/base_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging



class Reader:
def __init__(self):
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)

def __call__(self, *args, **kwargs):
raise NotImplementedError

def _is_validated_file(self, filename: str) -> bool:
raise NotImplementedError
14 changes: 14 additions & 0 deletions config/proto.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"image_path": "",
"image_size": [{"width": ""},
{"height": ""},
{"channel": ""}],
"object": [{"class": ""},
{"boundary_box": [{"xmin": ""},
{"ymin": ""},
{"xmax": ""},
{"ymax": ""}
]
}
]
}
51 changes: 51 additions & 0 deletions config/xml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"image_path": [{"hierarchy": ["annotation", "path"]},
{"options": ["single"]},
{"contents": []}],

"image_size": [ {"hierarchy": ["annotation", "size"]},
{"options": ["multi"]},

{"contents" : [
{"width": [{"hierarchy": ["width"]},
{"options": ["single"]},
{"contents": []}]},

{"height": [{"hierarchy": ["height"]},
{"options": ["single"]},
{"contents": []}]},

{"channel": [{"hierarchy": ["depth"]},
{"options": ["single"]},
{"contents": []}]}
]
}
],

"object": [ {"hierarchy": ["annotation", "object"]},
{"options": ["multi", "recursive"]},
{"contents": [
{"class": [{"hierarchy": ["name"]},
{"options": ["single"]},
{"contents": []}]},
{"boundary_box": [{"hierarchy": ["bndbox"]},
{"options": ["multi"]},
{"contents": [
{"xmin": [{"hierarchy": ["xmin"]},
{"options": ["single"]},
{"contents": []}]},
{"ymin": [{"hierarchy": ["ymin"]},
{"options": ["single"]},
{"contents": []}]},
{"xmax": [{"hierarchy": ["xmax"]},
{"options": ["single"]},
{"contents": []}]},
{"ymax": [{"hierarchy": ["ymax"]},
{"options": ["single"]},
{"contents": []}]}
]
}]
}
]}
]
}
1 change: 1 addition & 0 deletions utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from utils.reader import *
49 changes: 49 additions & 0 deletions utils/reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import xml.etree.ElementTree as Et
from xml.etree.ElementTree import ElementTree

from base import base_reader


class XmlReader(base_reader.Reader):

ALLOW_EXTENSION = "xml"

def __init__(self) -> None:
super(XmlReader, self).__init__()

def __call__(self, xml_path: str) -> None or ElementTree:
if not self._is_validated_file(xml_path):
return None

return self._load_xml(xml_path)

def _is_validated_file(self, filename: str) -> bool:
file_extension: str = filename.split(".")[-1].lower()

if file_extension == XmlReader.ALLOW_EXTENSION:
return True

self.logger.warning("Warning: extension of file not supported. " + \
"this file skip. "
"Supported file extension following : " + \
"`{}`".format(XmlReader.ALLOW_EXTENSION))
return False

@staticmethod
def _load_xml(filepath: str) -> ElementTree:
with open(filepath) as xml:
tree: ElementTree = Et.parse(xml)

return tree


if __name__ == "__main__":
xml_file_path = "../example/voc/label/000001.xml"
wrong_file_path = "../example/voc/label/000001.jpeg"

xml_reader = XmlReader()
xml_tree = xml_reader(xml_file_path)
print(xml_tree)

result = xml_reader(wrong_file_path)
print(result)

0 comments on commit 2e7b883

Please sign in to comment.