sipp11 5 years ago
parent
commit
277c1deb60
  1. 57
      src/utils.py

57
src/utils.py

@ -1,5 +1,16 @@
import collections import collections
import math import math
from collections import namedtuple
Rectangle = namedtuple("Rectangle", "xmin ymin xmax ymax")
def area(a, b):
# returns None if rectangles don't intersect
dx = min(a.xmax, b.xmax) - max(a.xmin, b.xmin)
dy = min(a.ymax, b.ymax) - max(a.ymin, b.ymin)
if (dx >= 0) and (dy >= 0):
return dx * dy
# detecting area # detecting area
AREAS = [ AREAS = [
@ -40,7 +51,7 @@ AREAS = [
], ],
[ [
("id", 7), ("id", 7),
("area", ((555, 170), (720, 295))), ("area", ((555, 170), (700, 295))),
("target", ["person", "wheelchair", "bicycle"]), ("target", ["person", "wheelchair", "bicycle"]),
("next", [4]), ("next", [4]),
], ],
@ -96,12 +107,12 @@ OBJ_LEAVING_COND = [
("duration_to_next", 30 * 4), ("duration_to_next", 30 * 4),
], ],
[ [
('origin_id', 5), ("origin_id", 5),
('heading', 'N'), ("heading", "N"),
('x', 715), ("x", 715),
('y', -1), ("y", -1),
('next_area', [11]), ("next_area", [11]),
('duration_to_next', 30 * 3) ("duration_to_next", 30 * 3),
], ],
# [ # [
# ('origin_id', ), # ('origin_id', ),
@ -174,23 +185,30 @@ def is_it_the_same_obj(x1, y1, w1, h1, i1, j1, w2, h2, **kwargs):
object and of course, dimension too. object and of course, dimension too.
""" """
_id = kwargs.get("id", None) _id = kwargs.get("id", None)
# if _id: if _id:
# print(" :: check against id:", _id) print(" :: check against id:", _id, end="")
# if first coords are pretty much the same spot, then they are the same
if abs(x1 - i1)/x1 < 0.05 and abs(y1 - j1)/y1 < 0.05:
print(" same 1st coords")
return True
DIMENSION_SHIFT = 0.15 DIMENSION_SHIFT = 0.15
# we have to use centroid !! from the experience # we have to use centroid !! from the experience
cx1, cy1, cx2, cy2 = x1 + w1 / 2, y1 + h1 / 2, i1 + w2 / 2, j1 + h2 / 2 cx1, cy1, cx2, cy2 = x1 + w1 / 2, y1 + h1 / 2, i1 + w2 / 2, j1 + h2 / 2
c_dff_x, c_dff_y = abs(cx2 - cx1), abs(cy2 - cy1) c_dff_x, c_dff_y = abs(cx2 - cx1), abs(cy2 - cy1)
w_shift, h_shift = w1 * DIMENSION_SHIFT, h1 * DIMENSION_SHIFT w_shift, h_shift = w1 * DIMENSION_SHIFT, h1 * DIMENSION_SHIFT
# print(" ::SAME:: shift") print(" ::SAME:: shift", end="")
# print(f" ---> SHIFT --> w:{w_shift}, h:{h_shift}") print(f" | SHIFT w:{w_shift},h:{h_shift}", end="")
# print(f" ---> centroid {c_dff_x}, {c_dff_y}") print(f" | centroid {c_dff_x}, {c_dff_y}", end="")
if c_dff_x > w_shift and c_dff_y > h_shift: if c_dff_x > w_shift and c_dff_y > h_shift:
# print(" ::SAME:: shift too much already -- NOT THE SAME") # print(" ::SAME:: shift too much already -- NOT THE SAME")
return False return False
first_w_smaller = i1 > x1 and (w1 - w2) > (i1 - x1)
# if one inside the other # if one inside the other
if i1 > x1 and (w1 - w2) > i1 - x1 and j1 > y1 and h1 - h2 > j1 - y1: if i1 > x1 and (w1 - w2) > (i1 - x1) and j1 > y1 and h1 - h2 > j1 - y1:
# one is inside the other # one is inside the other
# print(" ::SAME:: new one inside existing tracker") # print(" ::SAME:: new one inside existing tracker")
return True return True
@ -199,16 +217,23 @@ def is_it_the_same_obj(x1, y1, w1, h1, i1, j1, w2, h2, **kwargs):
# print(" ::SAME:: existing tracker inside new tracker") # print(" ::SAME:: existing tracker inside new tracker")
return True return True
# if it's 90% overlapped then, assumed it's the same
# ra = Rectangle(x1, y1, x1 + w1, y1 + h1)
# rb = Rectangle(i1, j1, i1 + w2, j1 + w2)
# print(f'overlapped area: {area(ra, rb)} 1:{w1*h1:.1f} 1:{w2*h2:.1f}')
# print(f'||one {x1},{y1},{w1},{h1} two {i1},{j1},{w2},{h2}||')
# if it's not inside the other, then we can use "size" if it's different # if it's not inside the other, then we can use "size" if it's different
size1, size2 = w1 * h1, w2 * h2 size1, size2 = w1 * h1, w2 * h2
# if size is larger than 20%, then it's not the same thing # if size is larger than 20%, then it's not the same thing
# print(f" ---> size {size1}, {size2}, diff % : {abs(size2 - size1)/size1}")
# print(" ::SAME:: size") # print(" ::SAME:: size")
if abs(size2 - size1) / size1 > 0.45: if abs(size2 - size1) / size1 > 0.45:
# print(" ::SAME:: too diff in size -- NOT THE SAME") print(f" sz {size1}, {size2}, diff%{abs(size2 - size1)/size1}", end="")
print(" ^^ too diff in size -- NOT THE SAME", end="")
return False return False
# print(" ::SAME:: last") print(" ::SAME:: last")
print("")
return True return True

Loading…
Cancel
Save