From 0c17635bd39e0605dd3052cde8b689b5bb13313d Mon Sep 17 00:00:00 2001
From: Mathieu Beligon <mathieu@feedly.com>
Date: Tue, 31 Mar 2020 16:12:29 -0400
Subject: [PATCH] [runes] (blend) Add label to point of interest

---
 robots-at-runes/research/dataset/blend/examples/logo.xml     | 2 ++
 robots-at-runes/research/dataset/blend/image_blender.py      | 5 +++--
 .../blend/labeled_image_modifiers/labeled_image_rotator.py   | 4 +++-
 .../blend/labeled_image_modifiers/labeled_image_scaler.py    | 2 +-
 robots-at-runes/research/dataset/labeled_image.py            | 3 ++-
 5 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/robots-at-runes/research/dataset/blend/examples/logo.xml b/robots-at-runes/research/dataset/blend/examples/logo.xml
index 3257c42..b3d47c3 100644
--- a/robots-at-runes/research/dataset/blend/examples/logo.xml
+++ b/robots-at-runes/research/dataset/blend/examples/logo.xml
@@ -2,9 +2,11 @@
 	<point>
 		<x>432</x>
 		<y>76</y>
+		<label>green</label>
 	</point>
 	<point>
 		<x>432</x>
 		<y>13</y>
+		<label>blue</label>
 	</point>
 </annotation>
diff --git a/robots-at-runes/research/dataset/blend/image_blender.py b/robots-at-runes/research/dataset/blend/image_blender.py
index 8e30abd..c19202e 100644
--- a/robots-at-runes/research/dataset/blend/image_blender.py
+++ b/robots-at-runes/research/dataset/blend/image_blender.py
@@ -56,7 +56,7 @@ class ImageBlender:
 
     @staticmethod
     def _translate_poi(poi: PointOfInterest, x: int, y: int) -> PointOfInterest:
-        return PointOfInterest(poi.x + x, poi.y + y)
+        return PointOfInterest(poi.x + x, poi.y + y, poi.label)
 
     def _crop_background(self, background: Image) -> Image:
         h, w, _ = background.shape
@@ -89,7 +89,8 @@ if __name__ == "__main__":
         res.save(EXAMPLES_DIR, f"test_{i}")
 
         plt.imshow(res.image)
-        plt.plot([poi.x for poi in res.point_of_interests], [poi.y for poi in res.point_of_interests], "r.")
+        for poi in res.point_of_interests:
+            plt.plot([poi.x], [poi.y], f"{poi.label[0]}.")
         plt.axis("off")
         plt.tight_layout()
         plt.show()
diff --git a/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_rotator.py b/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_rotator.py
index b73c1eb..860972c 100644
--- a/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_rotator.py
+++ b/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_rotator.py
@@ -25,7 +25,9 @@ class LabeledImageRotator(LabeledImageModifierABC):
         prev_vector_to_center = np.array((poi.x - original_image.shape[1] / 2, poi.y - original_image.shape[0] / 2))
         new_vector_to_center = np.dot(rotation_matrix, prev_vector_to_center)
         return PointOfInterest(
-            int(new_vector_to_center[0] + new_image.shape[1] / 2), int(new_vector_to_center[1] + new_image.shape[0] / 2)
+            int(new_vector_to_center[0] + new_image.shape[1] / 2),
+            int(new_vector_to_center[1] + new_image.shape[0] / 2),
+            poi.label,
         )
 
     def _get_value_from_factor(self, factor: float) -> float:
diff --git a/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_scaler.py b/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_scaler.py
index 73f9255..b98e25f 100644
--- a/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_scaler.py
+++ b/robots-at-runes/research/dataset/blend/labeled_image_modifiers/labeled_image_scaler.py
@@ -19,7 +19,7 @@ class LabeledImageScaler(LabeledImageModifierABC):
     def _generate_modified_poi(
         self, poi: PointOfInterest, original_image: Image, new_image: Image, scale: float
     ) -> PointOfInterest:
-        return PointOfInterest(int(poi.x * scale), int(poi.y * scale))
+        return PointOfInterest(int(poi.x * scale), int(poi.y * scale), poi.label)
 
     def _get_value_from_factor(self, factor: float) -> float:
         intensity = (self.max_scale - 1) * abs(factor) + 1
diff --git a/robots-at-runes/research/dataset/labeled_image.py b/robots-at-runes/research/dataset/labeled_image.py
index 2d66cc3..30d0dac 100644
--- a/robots-at-runes/research/dataset/labeled_image.py
+++ b/robots-at-runes/research/dataset/labeled_image.py
@@ -13,13 +13,14 @@ from polystar.common.models.image import Image
 class PointOfInterest:
     x: int
     y: int
+    label: str
 
     def to_dict(self) -> Dict[str, int]:
         return self.__dict__
 
     @classmethod
     def from_dict(cls, d: Dict[str, Any]):
-        return cls(x=int(d["x"]), y=int(d["y"]))
+        return cls(x=int(d["x"]), y=int(d["y"]), label=d["label"])
 
     @classmethod
     def from_annotation_file(cls, annotation_path: Path) -> List["PointOfInterest"]:
-- 
GitLab