Переглянути джерело

moved some files. added a generic annotation file reader

Dimitri Korsch 5 роки тому
батько
коміт
c44fe42ea0

+ 37 - 0
cvdatasets/annotation/__init__.py

@@ -0,0 +1,37 @@
+from cvdatasets.annotations.annotation_types import AnnotationType
+from cvdatasets.annotations.impl.birdsnap import BSNAP_Annotations
+from cvdatasets.annotations.impl.cars import CARS_Annotations
+from cvdatasets.annotations.impl.cub import CUB_Annotations
+from cvdatasets.annotations.impl.dogs import DOGS_Annotations
+from cvdatasets.annotations.impl.flowers import FLOWERS_Annotations
+from cvdatasets.annotations.impl.hed import HED_Annotations
+from cvdatasets.annotations.impl.imagenet import INET_Annotations
+from cvdatasets.annotations.impl.inat import INAT18_Annotations
+from cvdatasets.annotations.impl.inat import INAT19_Annotations
+from cvdatasets.annotations.impl.inat import INAT20_Annotations
+from cvdatasets.annotations.impl.nab import NAB_Annotations
+from cvdatasets.annotations.impl.tigers import TIGERS_Annotations
+
+
+from cvdatasets.annotations.base import BaseAnnotations
+from cvdatasets.annotations.base.bbox_mixin import BBoxMixin
+from cvdatasets.annotations.base.parts_mixin import PartsMixin
+
+__all__ = [
+	"AnnotationType",
+	"BaseAnnotations",
+	"BBoxMixin",
+	"BSNAP_Annotations",
+	"CARS_Annotations",
+	"CUB_Annotations",
+	"DOGS_Annotations",
+	"FLOWERS_Annotations",
+	"HED_Annotations",
+	"INAT18_Annotations",
+	"INAT19_Annotations",
+	"INAT20_Annotations",
+	"INET_Annotations",
+	"NAB_Annotations",
+	"PartsMixin",
+	"TIGERS_Annotations",
+]

+ 0 - 3
cvdatasets/annotations/base/__init__.py → cvdatasets/annotation/base.py

@@ -195,6 +195,3 @@ class BaseAnnotations(abc.ABC):
 	@property
 	def test_uuids(self):
 		return self._uuids(self.test_split)
-
-from .bbox_mixin import BBoxMixin
-from .parts_mixin import PartsMixin

+ 97 - 0
cvdatasets/annotation/files.py

@@ -0,0 +1,97 @@
+import abc
+import logging
+import simplejson as json
+import warnings
+
+from pathlib import Path
+from typing import Any
+from typing import Callable
+from typing import Dict
+from typing import List
+
+class BaseAnnotationFiles(abc.ABC):
+
+	@staticmethod
+	def _parse_opts(fpath_and_opts):
+		if isinstance(fpath_and_opts, (list, tuple)):
+			fpath, *opts = fpath_and_opts
+		else:
+			fpath, opts = fpath_and_opts, []
+
+		return fpath, opts
+
+
+	def __init__(self, *files, root=".", load_strict=False, **named_files):
+		super(BaseAnnotationFiles, self).__init__()
+		self.load_strict = load_strict
+		self.root = Path(root)
+
+		for fpath in files:
+			fpath, opts = self._parse_opts(fpath)
+			self.add_file_content(fpath, *opts)
+
+		for attr, fpath in named_files.items():
+			fpath, opts = self._parse_opts(fpath)
+			self.add_file_content(fpath, *opts, attr=attr)
+
+	def _path(self, fname) -> Path:
+		return self.root / fname
+
+	def _json_reader(self, f) -> Dict[str, Any]:
+		return json.load(f)
+
+	def _line_reader(self, f) -> List[str]:
+		return [line.strip() for line in f if line.strip()]
+
+	def get_reader(self, fpath) -> Callable:
+		return {
+			".json": self._json_reader,
+			".txt": self._line_reader,
+		}.get(Path(fpath).suffix.lower())
+
+	def read_file(self, fpath):
+		with open(fpath) as f:
+			reader = self.get_reader(fpath)
+
+			if reader is None:
+				raise NotImplementedError(f"Don't know how to read \"{fpath.name}\"!")
+
+			elif not callable(reader):
+				raise ValueError(f"The reader for \"{fpath.name}\" was not callable!")
+
+			return reader(f)
+
+	def read_directory(self, fpath):
+		raise NotImplementedError("IMPLEMENT ME!")
+
+	def add_file_content(self, fpath, optional=False, *args, attr=None, **kwargs):
+		fpath = self._path(fpath)
+		attr = attr or fpath.stem.replace(".", "_")
+		content = None
+
+		if fpath.is_file():
+			content = self.read_file(fpath)
+
+		elif fpath.is_dir():
+			content = self.read_directory(fpath)
+
+		elif not optional:
+			msg = f"File \"{fpath}\" was not found!"
+			if self.load_strict:
+				raise AssertionError(msg)
+			else:
+				warnings.warn(msg)
+		else:
+			logging.debug(f"\"{fpath}\" was not found and was ignored, since it was marked as optional")
+
+		setattr(self, attr, content)
+
+if __name__ == '__main__':
+	files = BaseAnnotationFiles(
+		"foo.txt",
+		tad="bar.txt",
+		bar=("fobar.txt", True),
+		root="/Bla",
+		# load_strict=True,
+	)
+	print(files.foo, files.tad, files.bar)

+ 3 - 0
cvdatasets/annotation/mixins/__init__.py

@@ -0,0 +1,3 @@
+
+from cvdatasets.annotations.mixins.bbox_mixin import BBoxMixin
+from cvdatasets.annotations.mixins.parts_mixin import PartsMixin

+ 0 - 0
cvdatasets/annotations/base/bbox_mixin.py → cvdatasets/annotation/mixins/bbox_mixin.py


+ 0 - 0
cvdatasets/annotations/base/parts_mixin.py → cvdatasets/annotation/mixins/parts_mixin.py


+ 0 - 0
cvdatasets/annotations/annotation_types.py → cvdatasets/annotation/types.py


+ 0 - 37
cvdatasets/annotations/__init__.py

@@ -1,37 +0,0 @@
-from cvdatasets.annotations.annotation_types import AnnotationType
-from cvdatasets.annotations.impl.birdsnap import BSNAP_Annotations
-from cvdatasets.annotations.impl.cars import CARS_Annotations
-from cvdatasets.annotations.impl.cub import CUB_Annotations
-from cvdatasets.annotations.impl.dogs import DOGS_Annotations
-from cvdatasets.annotations.impl.flowers import FLOWERS_Annotations
-from cvdatasets.annotations.impl.hed import HED_Annotations
-from cvdatasets.annotations.impl.imagenet import INET_Annotations
-from cvdatasets.annotations.impl.inat import INAT18_Annotations
-from cvdatasets.annotations.impl.inat import INAT19_Annotations
-from cvdatasets.annotations.impl.inat import INAT20_Annotations
-from cvdatasets.annotations.impl.nab import NAB_Annotations
-from cvdatasets.annotations.impl.tigers import TIGERS_Annotations
-
-
-from cvdatasets.annotations.base import BaseAnnotations
-from cvdatasets.annotations.base.bbox_mixin import BBoxMixin
-from cvdatasets.annotations.base.parts_mixin import PartsMixin
-
-__all__ = [
-	"AnnotationType",
-	"BaseAnnotations",
-	"BBoxMixin",
-	"BSNAP_Annotations",
-	"CARS_Annotations",
-	"CUB_Annotations",
-	"DOGS_Annotations",
-	"FLOWERS_Annotations",
-	"HED_Annotations",
-	"INAT18_Annotations",
-	"INAT19_Annotations",
-	"INAT20_Annotations",
-	"INET_Annotations",
-	"NAB_Annotations",
-	"PartsMixin",
-	"TIGERS_Annotations",
-]

+ 0 - 0
cvdatasets/annotations/impl/birdsnap.py → cvdatasets/annotations/birdsnap.py


+ 0 - 0
cvdatasets/annotations/impl/cars.py → cvdatasets/annotations/cars.py


+ 0 - 0
cvdatasets/annotations/impl/cub.py → cvdatasets/annotations/cub.py


+ 0 - 0
cvdatasets/annotations/impl/dogs.py → cvdatasets/annotations/dogs.py


+ 0 - 0
cvdatasets/annotations/impl/flowers.py → cvdatasets/annotations/flowers.py


+ 0 - 0
cvdatasets/annotations/impl/hed.py → cvdatasets/annotations/hed.py


+ 0 - 0
cvdatasets/annotations/impl/imagenet.py → cvdatasets/annotations/imagenet.py


+ 0 - 0
cvdatasets/annotations/impl/__init__.py


+ 0 - 0
cvdatasets/annotations/impl/inat.py → cvdatasets/annotations/inat.py


+ 0 - 0
cvdatasets/annotations/impl/nab.py → cvdatasets/annotations/nab.py


+ 0 - 0
cvdatasets/annotations/impl/tigers.py → cvdatasets/annotations/tigers.py