This repository has been archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#19: add abstract class as parser, reader
- Loading branch information
Showing
9 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from base.base_reader import * | ||
from base.base_parser import * | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": ""} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": []}]} | ||
] | ||
}] | ||
} | ||
]} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from utils.reader import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |