diff options
| author | adamhrv <adam@ahprojects.com> | 2018-12-05 12:00:15 +0100 |
|---|---|---|
| committer | adamhrv <adam@ahprojects.com> | 2018-12-05 12:00:15 +0100 |
| commit | 90abf459d1df1f21960c1d653a1f936d1ec30256 (patch) | |
| tree | facab8e9bac6c56e69c369c2140cdbea218a01df /megapixels/app/models | |
| parent | 0529d4cd1618016319e995c37aa118bf8c2d501b (diff) | |
.
Diffstat (limited to 'megapixels/app/models')
| -rw-r--r-- | megapixels/app/models/bbox.py | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/megapixels/app/models/bbox.py b/megapixels/app/models/bbox.py index 41b67416..ccdf229e 100644 --- a/megapixels/app/models/bbox.py +++ b/megapixels/app/models/bbox.py @@ -1,6 +1,9 @@ +import math + from dlib import rectangle as dlib_rectangle import numpy as np + class BBoxPoint: def __init__(self, x, y): @@ -105,7 +108,12 @@ class BBox: # # Utils # def constrain(self, dim): - + def distance(self, b): + a = self + dcx = self._cx - b.cx + dcy = self._cy - b.cy + d = int(math.sqrt(math.pow(dcx, 2) + math.pow(dcy, 2))) + return d # ----------------------------------------------------------------- # Modify @@ -117,26 +125,40 @@ class BBox: :returns (BBox) in pixel dimensions """ # expand - rect_exp = list( (np.array(self._rect) + np.array([-amt, -amt, amt, amt])).astype('int')) + r = list( (np.array(self._rect) + np.array([-amt, -amt, amt, amt])).astype('int')) # outliers oob = list(range(4)) - oob[0] = min(rect_exp[0], 0) - oob[1] = min(rect_exp[1], 0) - oob[2] = dim[0] - max(rect_exp[2], 2) - oob[3] = dim[1] - max(rect_exp[3], 3) + oob[0] = min(r[0], 0) + oob[1] = min(r[1], 0) + oob[2] = dim[0] - r[2] + oob[3] = dim[1] - r[3] oob = np.array(oob) oob[oob > 0] = 0 - # amount + # absolute amount oob = np.absolute(oob) - # threshold - rect_exp[0] = max(rect_exp[0], 0) - rect_exp[1] = max(rect_exp[1], 0) - rect_exp[2] = min(rect_exp[2], dim[0]) - rect_exp[3] = min(rect_exp[3], dim[1]) + # threshold expanded rectangle + r[0] = max(r[0], 0) + r[1] = max(r[1], 0) + r[2] = min(r[2], dim[0]) + r[3] = min(r[3], dim[1]) # redistribute oob amounts oob = np.array([-oob[2], -oob[3], oob[0], oob[1]]) - rect_exp = np.add(np.array(rect_exp), oob) - return BBox(*rect_exp) + r = np.add(np.array(r), oob) + # find overage + oob[0] = min(r[0], 0) + oob[1] = min(r[1], 0) + oob[2] = dim[0] - r[2] + oob[3] = dim[1] - r[3] + oob = np.array(oob) + oob[oob > 0] = 0 + oob = np.absolute(oob) + if np.array(oob).any(): + m = np.max(oob) + adj = np.array([m, m, -m, -m]) + # print(adj) + r = np.add(np.array(r), adj) + + return BBox(*r) # ----------------------------------------------------------------- @@ -199,6 +221,13 @@ class BBox: return cls(*rect) @classmethod + def from_xyxy(cls, x1, y1, x2, y2): + """Converts x1, y1, x2, y2 to BBox + same as constructure but zprovided for conveniene + """ + return cls(x1, y1, x2, y2) + + @classmethod def from_xywh(cls, x, y, w, h): """Converts x1, y1, w, h to BBox :param rect: (list) x1, y1, w, h |
