diff options
Diffstat (limited to 'megapixels/app/models/bbox.py')
| -rw-r--r-- | megapixels/app/models/bbox.py | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/megapixels/app/models/bbox.py b/megapixels/app/models/bbox.py index 40874691..608aaaf8 100644 --- a/megapixels/app/models/bbox.py +++ b/megapixels/app/models/bbox.py @@ -1,4 +1,5 @@ import math +import random from dlib import rectangle as dlib_rectangle import numpy as np @@ -127,9 +128,39 @@ class BBox: d = int(math.sqrt(math.pow(dcx, 2) + math.pow(dcy, 2))) return d + # ----------------------------------------------------------------- # Modify + def jitter(self, amt): + '''Jitters BBox in x,y,w,h values. Used for face feature extraction + :param amt: (float) percentage of BBox for maximum translation + :returns (BBox) + ''' + w = self._width + (self._width * random.uniform(-amt, amt)) + h = self._height + (self._height * random.uniform(-amt, amt)) + cx = self._cx + (self._cx * random.uniform(-amt, amt)) + cy = self._cy + (self._cy * random.uniform(-amt, amt)) + x1, y1 = np.clip((cx - w/2, cy - h/2), 0.0, 1.0) + x2, y2 = np.clip((cx + w/2, cy + h/2), 0.0, 1.0) + return BBox(x1, y1, x2, y2) + + def expand(self, per): + """Expands BBox by percentage + :param per: (float) percentage to expand 0.0 - 1.0 + :param dim: (int, int) image width, height + :returns (BBox) expanded + """ + # expand + dw, dh = [(self._width * per), (self._height * per)] + r = list(np.array(self._rect) + np.array([-dw, -dh, dw, dh])) + # threshold expanded rectangle + r[0] = max(r[0], 0.0) + r[1] = max(r[1], 0.0) + r[2] = min(r[2], 1.0) + r[3] = min(r[3], 1.0) + return BBox(*r) + def expand_dim(self, amt, bounds): """Expands BBox within dim :param box: (tuple) left, top, right, bottom @@ -170,7 +201,7 @@ class BBox: # print(adj) r = np.add(np.array(r), adj) - return BBox(*r) + return BBox(*r) # updats all BBox values # ----------------------------------------------------------------- @@ -221,6 +252,13 @@ class BBox: # Create from @classmethod + def from_xywh_norm(cls, x, y, w, h): + """Converts w, y, w, h to normalized BBox + :returns BBox + """ + return cls(x, y, x + w, y + h) + + @classmethod def from_xyxy_dim(cls, x1, y1, x2, y2, dim): """Converts x1, y1, w, h to BBox and normalizes :returns BBox |
