From d8ec43918364c37a98e059c55ff1dd96a28e0c4f Mon Sep 17 00:00:00 2001
From: Mathieu Beligon <mathieu@feedly.com>
Date: Sat, 28 Mar 2020 18:14:44 -0400
Subject: [PATCH] [runes] (refactor) Fix poetry dependencies, black code, and
 remove constant file

---
 robots-at-runes/poetry.lock               | Bin 90953 -> 91472 bytes
 robots-at-runes/pyproject.toml            |   1 +
 robots-at-runes/research/constants.py     |   3 --
 robots-at-runes/research/superposition.py |  62 ++++++++++++----------
 4 files changed, 35 insertions(+), 31 deletions(-)
 delete mode 100644 robots-at-runes/research/constants.py

diff --git a/robots-at-runes/poetry.lock b/robots-at-runes/poetry.lock
index e8d0e4d738aeaf5eaa4438030e80abcae0c47a0b..8c2d89db08a86cb509e2a2a9ecb4c6a68f847449 100644
GIT binary patch
delta 480
zcmYk0KZ_JW5XD(^!9S63;Gs#e@wxP$nVz2D(8E|X64UCQ>fPpMx|iu*kGtf;3k?=A
z_8SQM4Ga_oLBEkbE(c~+zk09U`|)l5`{(?NJib1IM5Ct!(SeQOJq1s}61Z)!*~gF|
zM;P#&z+mzg{D3Qh%@{4E<iqMeVcOa?U@?psQje4S1jd+eCrbXRI8Gp)Q%^Yte}&fv
zRp-;D$II#8E6kfd=H8n_Rx0{9=IK=W<+vRS=_pgFR~U6GDgZ+V!<0Y7k$_578sWm{
zHN4(Xc=`69z{Ik@xy<VrKrEYyJ-OZGa`7PGKyV5RKWww_cQe`Csh4v1=RVuZ;}6fz
zq!wMvE7Dr%iX4~9*rqiMYoRp~8lj|9wKYmKP76Vd6f>MvhHG-f7rRIEqy6J|7pHfp
zgx%-wM-LBK=^0zrpJw-FPhaS|L8rKCyOvRf3^lK~WTq{T8drCUb6zt`RmBP*Zn==M
Q7ThXlI0>`A`S|tMUwn+9$p8QV

delta 98
zcmV-o0G<EP$_2^B1+X(1v$YwIKS42LW@9xqG&M3|WH4f7IA&!sFg9UfFg7wZH8wCd
zF*7t`VKp)}Gi5k8H#a#rWH&iCFlJ^qWnp7BGB7zaVlyI>5d{>JFE9qPpILD|vz3>-
E%^3+FOaK4?

diff --git a/robots-at-runes/pyproject.toml b/robots-at-runes/pyproject.toml
index 27e06de..3e9905b 100644
--- a/robots-at-runes/pyproject.toml
+++ b/robots-at-runes/pyproject.toml
@@ -8,6 +8,7 @@ packages = [{ include = "polystar" }]
 [tool.poetry.dependencies]
 python = "^3.7"
 "polystar.common" = {path = "../common"}
+imutils = "^0.5.3"
 
 [tool.poetry.dev-dependencies]
 pytest = "^4.5"
diff --git a/robots-at-runes/research/constants.py b/robots-at-runes/research/constants.py
deleted file mode 100644
index 2998950..0000000
--- a/robots-at-runes/research/constants.py
+++ /dev/null
@@ -1,3 +0,0 @@
-FACT_RESIZE = 1     # Facteur de redimensionnement. La taille de l'image sera
-                    # divisée ou multipliée par ce chiffre au maximum
-MAX_ANGLE = 20      # Max angle in degrees for random rotation
\ No newline at end of file
diff --git a/robots-at-runes/research/superposition.py b/robots-at-runes/research/superposition.py
index f9e1768..2a5b80e 100644
--- a/robots-at-runes/research/superposition.py
+++ b/robots-at-runes/research/superposition.py
@@ -1,14 +1,19 @@
+import random
+import xml.etree.ElementTree as ET
 from pathlib import Path
+from xml.dom.minidom import parseString
 
-import numpy as np
-import pandas as pd
-import matplotlib.pyplot as plt
 import cv2
-import random as rd
+import matplotlib.pyplot as plt
+import numpy as np
 from imutils import rotate_bound
-import constants as cst
-import xml.etree.ElementTree as ET
-from xml.dom.minidom import parseString
+
+# Facteur de redimensionnement. La taille de l'image sera divisée ou multipliée par ce chiffre au maximum
+FACT_RESIZE = 1
+
+# Max angle in degrees for random rotation
+MAX_ANGLE = 20
+
 
 def preprocess_background(path_back):
     background = cv2.imread(path_back)
@@ -20,17 +25,18 @@ def preprocess_sticker(path_item):
     return cv2.cvtColor(item, cv2.COLOR_BGRA2RGBA)
 
 
-def generate_one(background, item, rotate=True, scale=True, custom_rotate=None, custom_scale=None, to_print=False,
-                 save_name=None):
+def generate_one(
+    background, item, rotate=True, scale=True, custom_rotate=None, custom_scale=None, to_print=False, save_name=None
+):
     if rotate:
         if custom_rotate is None:
-            percent_rotate = np.round((2 * rd.random()) - 1, 3)  # entre -1 et 1
+            percent_rotate = np.round((2 * random.random()) - 1, 3)  # entre -1 et 1
         else:
             percent_rotate = custom_rotate
         item = rotate_percentage(item, percent_rotate)
     if scale:
         if custom_scale is None:
-            percent_scale = np.round((2 * rd.random()) - 1, 3)  # entre -1 et 1
+            percent_scale = np.round((2 * random.random()) - 1, 3)  # entre -1 et 1
         else:
             percent_scale = custom_scale
         item = reshape_percentage(item, percent_scale)
@@ -42,9 +48,9 @@ def generate_one(background, item, rotate=True, scale=True, custom_rotate=None,
     composition_subset = superimpose(background_subset, item, mask_alpha)
 
     composition = background.copy()
-    composition[h_start:h_start + hs, w_start:w_start + ws, :] = composition_subset
+    composition[h_start : h_start + hs, w_start : w_start + ws, :] = composition_subset
 
-    labels = [h_start, w_start, h_start+hs, w_start+ws]
+    labels = [h_start, w_start, h_start + hs, w_start + ws]
 
     if not (save_name is None):
         cv2.imwrite(save_name, cv2.cvtColor(composition, cv2.COLOR_RGB2BGR))
@@ -78,13 +84,13 @@ def get_subset_shapes(img_extract, hs, ws):
     he, we, _ = img_extract.shape
     delta_h = he - hs
     delta_w = we - ws
-    h_start = int(rd.random() * delta_h)
-    w_start = int(rd.random() * delta_w)
-    return img_extract[h_start:h_start + hs, w_start:w_start + ws, :], h_start, w_start
+    h_start = int(random.random() * delta_h)
+    w_start = int(random.random() * delta_w)
+    return img_extract[h_start : h_start + hs, w_start : w_start + ws, :], h_start, w_start
 
 
 def reshape_percentage(img_base, percent):
-    intensity = 1 + abs(percent) * cst.FACT_RESIZE
+    intensity = 1 + abs(percent) * FACT_RESIZE
     h, w, _ = img_base.shape
     if percent < 0:
         h_dest, w_dest = int(h / intensity), int(w / intensity)
@@ -95,7 +101,7 @@ def reshape_percentage(img_base, percent):
 
 
 def rotate_percentage(img_base, percent):
-    angle = cst.MAX_ANGLE * percent
+    angle = MAX_ANGLE * percent
     return rotate_bound(img_base, angle)
 
 
@@ -108,22 +114,22 @@ sticker = preprocess_sticker(path_sticker)
 labels = []
 filenames = []
 for i in range(10):
-    folder = 'dataset/'
-    filename = 'image_'+str(i)+'.jpg'
+    folder = "dataset/"
+    filename = "image_" + str(i) + ".jpg"
     filenames.append(filename)
-    _, label = generate_one(background, sticker, to_print=False, save_name=folder+filename)
+    _, label = generate_one(background, sticker, to_print=False, save_name=folder + filename)
     labels.append(label)
 
 
-data = ET.Element('annotations')
+data = ET.Element("annotations")
 
 for i, [xmin, ymin, xmax, ymax] in enumerate(labels):
-    object = ET.SubElement(data, 'object')
-    sub_name = ET.SubElement(object, 'filename')
-    sub_xmin = ET.SubElement(object, 'xmin')
-    sub_ymin = ET.SubElement(object, 'ymin')
-    sub_xmax = ET.SubElement(object, 'xmax')
-    sub_ymax = ET.SubElement(object, 'ymax')
+    object = ET.SubElement(data, "object")
+    sub_name = ET.SubElement(object, "filename")
+    sub_xmin = ET.SubElement(object, "xmin")
+    sub_ymin = ET.SubElement(object, "ymin")
+    sub_xmax = ET.SubElement(object, "xmax")
+    sub_ymax = ET.SubElement(object, "ymax")
     sub_name.text = filenames[i]
     sub_xmin.text = str(xmin)
     sub_ymin.text = str(ymin)
-- 
GitLab