Spaces:
Runtime error
Runtime error
Niv Sardi
commited on
Commit
·
e7640ca
1
Parent(s):
9996fa3
augmentor: bugfix, properly convert between bb and centroid
Browse files- python/augment.py +2 -1
- python/imtool.py +46 -16
python/augment.py
CHANGED
|
@@ -105,7 +105,8 @@ with pipeline.pool(processes=-1, seed=1) as pool:
|
|
| 105 |
alpha = cv2.split(batch_aug.heatmaps_aug[logo_idx])
|
| 106 |
try:
|
| 107 |
img, bb, (w, h) = imtool.mix_alpha(img, logo, alpha[0], random.random(), random.random())
|
| 108 |
-
|
|
|
|
| 109 |
except AssertionError as e:
|
| 110 |
print(f'couldnt process {i}, {j}: {e}')
|
| 111 |
|
|
|
|
| 105 |
alpha = cv2.split(batch_aug.heatmaps_aug[logo_idx])
|
| 106 |
try:
|
| 107 |
img, bb, (w, h) = imtool.mix_alpha(img, logo, alpha[0], random.random(), random.random())
|
| 108 |
+
c = bb.to_centroid((h, w, 1))
|
| 109 |
+
anotations.append(c.to_anotation(0))
|
| 110 |
except AssertionError as e:
|
| 111 |
print(f'couldnt process {i}, {j}: {e}')
|
| 112 |
|
python/imtool.py
CHANGED
|
@@ -19,10 +19,13 @@ class BoundingBox(NamedTuple):
|
|
| 19 |
h: float = 0.0
|
| 20 |
|
| 21 |
@classmethod
|
| 22 |
-
def from_centroid(cls, c):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
self = cls(x=
|
|
|
|
|
|
|
|
|
|
| 26 |
return self
|
| 27 |
|
| 28 |
@classmethod
|
|
@@ -30,26 +33,53 @@ class BoundingBox(NamedTuple):
|
|
| 30 |
self = cls(x=d['x'], y=d['y'], w=d['width'], h=d['height'])
|
| 31 |
return self
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
class Centroid(BoundingBox):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
def
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
bco = None
|
| 43 |
with open(filename, 'r') as f:
|
| 44 |
lines = f.readlines()
|
| 45 |
for l in lines:
|
| 46 |
(b, x,y,w,h) = [float(i) for i in l.split(' ')]
|
| 47 |
bco = b
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def coord_dict_to_point(c):
|
| 55 |
return coord_to_point(c['x'], c['y'], c['width'], c['heigh'])
|
|
|
|
| 19 |
h: float = 0.0
|
| 20 |
|
| 21 |
@classmethod
|
| 22 |
+
def from_centroid(cls, c, shape = (1,1,1)):
|
| 23 |
+
(h, w, c) = shape
|
| 24 |
+
print(cls, c, shape)
|
| 25 |
+
self = cls(x=math.floor(w*(c.x - c.w/2))
|
| 26 |
+
, y=math.floor(h*(c.y - c.h/2))
|
| 27 |
+
, w=math.ceil(w*c.w)
|
| 28 |
+
, h=math.ceil(h*c.h))
|
| 29 |
return self
|
| 30 |
|
| 31 |
@classmethod
|
|
|
|
| 33 |
self = cls(x=d['x'], y=d['y'], w=d['width'], h=d['height'])
|
| 34 |
return self
|
| 35 |
|
| 36 |
+
@property
|
| 37 |
+
def start(self):
|
| 38 |
+
return (self.x, self.y)
|
| 39 |
+
|
| 40 |
+
@property
|
| 41 |
+
def end(self):
|
| 42 |
+
return (self.x + self.w, self.y + self.h)
|
| 43 |
+
|
| 44 |
+
def to_centroid(self, shape = (1,1,1)):
|
| 45 |
+
(h, w, c) = shape
|
| 46 |
+
return Centroid(x=math.floor(self.x + self.w/2)/w
|
| 47 |
+
, y=math.floor(self.y + self.h/2)/h
|
| 48 |
+
, w=math.ceil(self.w)/w
|
| 49 |
+
, h=math.ceil(self.h)/h)
|
| 50 |
+
|
| 51 |
class Centroid(BoundingBox):
|
| 52 |
+
def to_bounding_box(self, shape = (1,1,1)):
|
| 53 |
+
(h, w, c) = shape
|
| 54 |
+
|
| 55 |
+
return BoundingBox(
|
| 56 |
+
x=math.floor(w*(self.x - self.w/2))
|
| 57 |
+
, y=math.floor(h*(self.y - self.h/2))
|
| 58 |
+
, w=math.ceil(w*self.w)
|
| 59 |
+
, h=math.ceil(h*self.h))
|
| 60 |
|
| 61 |
+
def to_anotation(self, id: int, shape=(1,1,1)):
|
| 62 |
+
(h, w, c) = shape
|
| 63 |
+
|
| 64 |
+
return f'{id} {self.x/w} {self.y/h} {self.w/w} {self.h/h}'
|
| 65 |
+
|
| 66 |
+
def read_marker(filename: str, Type: type):
|
| 67 |
+
ret = []
|
| 68 |
bco = None
|
| 69 |
with open(filename, 'r') as f:
|
| 70 |
lines = f.readlines()
|
| 71 |
for l in lines:
|
| 72 |
(b, x,y,w,h) = [float(i) for i in l.split(' ')]
|
| 73 |
bco = b
|
| 74 |
+
print(b, x,y,w,h)
|
| 75 |
+
ret.append(Type(x,y,w,h))
|
| 76 |
+
return bco, ret
|
| 77 |
+
|
| 78 |
+
def read_bounding_boxes(filename: str):
|
| 79 |
+
return read_marker(filename, BoundingBox)
|
| 80 |
+
|
| 81 |
+
def read_centroids(filename: str):
|
| 82 |
+
return read_marker(filename, Centroid)
|
| 83 |
|
| 84 |
def coord_dict_to_point(c):
|
| 85 |
return coord_to_point(c['x'], c['y'], c['width'], c['heigh'])
|