Browse Source

moved bbox mixins to a separate module

Dimitri Korsch 4 years ago
parent
commit
b334d8ea41

+ 7 - 5
cvdatasets/dataset/__init__.py

@@ -1,10 +1,10 @@
 from cvdatasets.dataset.mixins.base import BaseMixin
+from cvdatasets.dataset.mixins.bounding_box import BBCropMixin
+from cvdatasets.dataset.mixins.bounding_box import BBoxMixin
+from cvdatasets.dataset.mixins.bounding_box import MultiBoxMixin
 from cvdatasets.dataset.mixins.chainer_mixins import IteratorMixin
 from cvdatasets.dataset.mixins.features import PreExtractedFeaturesMixin
-from cvdatasets.dataset.mixins.parts import BBCropMixin
-from cvdatasets.dataset.mixins.parts import BBoxMixin
 from cvdatasets.dataset.mixins.parts import CroppedPartMixin
-from cvdatasets.dataset.mixins.parts import MultiBoxMixin
 from cvdatasets.dataset.mixins.parts import PartCropMixin
 from cvdatasets.dataset.mixins.parts import PartMixin
 from cvdatasets.dataset.mixins.parts import PartRevealMixin
@@ -39,11 +39,13 @@ __all__ = [
 	# features
 	"PreExtractedFeaturesMixin",
 
-	# parts / bounding boxes
+	# bounding boxes
 	"BBCropMixin",
 	"BBoxMixin",
-	"CroppedPartMixin",
 	"MultiBoxMixin",
+
+	# parts
+	"CroppedPartMixin",
 	"PartCropMixin",
 	"PartMixin",
 	"PartRevealMixin",

+ 60 - 0
cvdatasets/dataset/mixins/bounding_box.py

@@ -0,0 +1,60 @@
+import numpy as np
+
+from cvdatasets.dataset.mixins.base import BaseMixin
+
+class BBoxMixin(BaseMixin):
+
+	def bounding_box(self, i):
+		bbox = self._get("bounding_box", i)
+		return [bbox[attr] for attr in "xywh"]
+
+class MultiBoxMixin(BaseMixin):
+	_all_keys=[
+		"x", "x0", "x1",
+		"y", "y0", "y1",
+		"w", "h",
+	]
+
+	def multi_box(self, i, keys=["x0","x1","y0","y1"]):
+		assert all([key in self._all_keys for key in keys]), \
+			f"unknown keys found: {keys}. Possible are: {self._all_keys}"
+
+		boxes = [
+			dict(
+				x=box["x0"], x0=box["x0"], x1=box["x1"],
+
+				y=box["y0"], y0=box["y0"], y1=box["y1"],
+
+				w=box["x1"] - box["x0"],
+				h=box["y1"] - box["y0"],
+			)
+			for box in self._get("multi_box", i)["objects"]
+		]
+
+		return [[box[key] for key in keys] for box in boxes]
+
+class BBCropMixin(BBoxMixin):
+
+	def __init__(self, *, crop_to_bb=False, crop_uniform=False, **kwargs):
+		super(BBCropMixin, self).__init__(**kwargs)
+		self.crop_to_bb = crop_to_bb
+		self.crop_uniform = crop_uniform
+
+	def bounding_box(self, i):
+		x,y,w,h = super(BBCropMixin, self).bounding_box(i)
+		if self.crop_uniform:
+			x0 = x + w//2
+			y0 = y + h//2
+
+			crop_size = max(w//2, h//2)
+
+			x,y = max(x0 - crop_size, 0), max(y0 - crop_size, 0)
+			w = h = crop_size * 2
+		return x,y,w,h
+
+	def get_example(self, i):
+		im_obj = super(BBCropMixin, self).get_example(i)
+		if self.crop_to_bb:
+			bb = self.bounding_box(i)
+			return im_obj.crop(*bb)
+		return im_obj

+ 2 - 57
cvdatasets/dataset/mixins/parts.py

@@ -1,63 +1,8 @@
 import numpy as np
 
 from cvdatasets.dataset.mixins.base import BaseMixin
-
-class BBoxMixin(BaseMixin):
-
-	def bounding_box(self, i):
-		bbox = self._get("bounding_box", i)
-		return [bbox[attr] for attr in "xywh"]
-
-class MultiBoxMixin(BaseMixin):
-	_all_keys=[
-		"x", "x0", "x1",
-		"y", "y0", "y1",
-		"w", "h",
-	]
-
-	def multi_box(self, i, keys=["x0","x1","y0","y1"]):
-		assert all([key in self._all_keys for key in keys]), \
-			f"unknown keys found: {keys}. Possible are: {self._all_keys}"
-
-		boxes = [
-			dict(
-				x=box["x0"], x0=box["x0"], x1=box["x1"],
-
-				y=box["y0"], y0=box["y0"], y1=box["y1"],
-
-				w=box["x1"] - box["x0"],
-				h=box["y1"] - box["y0"],
-			)
-			for box in self._get("multi_box", i)["objects"]
-		]
-
-		return [[box[key] for key in keys] for box in boxes]
-
-class BBCropMixin(BBoxMixin):
-
-	def __init__(self, *, crop_to_bb=False, crop_uniform=False, **kwargs):
-		super(BBCropMixin, self).__init__(**kwargs)
-		self.crop_to_bb = crop_to_bb
-		self.crop_uniform = crop_uniform
-
-	def bounding_box(self, i):
-		x,y,w,h = super(BBCropMixin, self).bounding_box(i)
-		if self.crop_uniform:
-			x0 = x + w//2
-			y0 = y + h//2
-
-			crop_size = max(w//2, h//2)
-
-			x,y = max(x0 - crop_size, 0), max(y0 - crop_size, 0)
-			w = h = crop_size * 2
-		return x,y,w,h
-
-	def get_example(self, i):
-		im_obj = super(BBCropMixin, self).get_example(i)
-		if self.crop_to_bb:
-			bb = self.bounding_box(i)
-			return im_obj.crop(*bb)
-		return im_obj
+from cvdatasets.dataset.mixins.bounding_box import BBoxMixin
+from cvdatasets.dataset.mixins.bounding_box import BBCropMixin
 
 class PartsInBBMixin(BBoxMixin):
 	def __init__(self, parts_in_bb=False, *args, **kwargs):

+ 1 - 1
scripts/tests.sh

@@ -4,4 +4,4 @@ source ${_root}/scripts/config.sh
 
 export BASE_DIR="${_root}/tests"
 
-$PYTHON ${BASE_DIR}/main.py $@
+python ${BASE_DIR}/main.py $@