sipp11 5 years ago
parent
commit
33e0c4324e
  1. 54
      src/main.py
  2. 21
      src/utils.py

54
src/main.py

@ -184,6 +184,7 @@ while True:
(success, boxes) = cv_trackers.update(frame)
# print("success", success)
# print("boxes", boxes)
print(f"[{_frame_count:08d}] ::")
untracking = []
# loop over the bounding boxes and draw then on the frame
@ -231,7 +232,7 @@ while True:
if (cond["heading"] in "NE" and x + w > cond["x"]) or (
cond["heading"] in "SW" and x < cond["x"]
):
print("REACH condition: X", x, cond["x"])
# print("REACH condition: X", x, cond["x"])
REACH_condition = True
elif cond["y"] != -1:
@ -239,7 +240,7 @@ while True:
if (cond["heading"] in "NE" and x + w > cond["y"]) or (
cond["heading"] in "SW" and x < cond["y"]
):
print("REACH condition: Y")
# print("REACH condition: Y")
REACH_condition = True
if not REACH_condition:
continue
@ -248,9 +249,10 @@ while True:
# pp.pprint(cond)
# TODO: should be a loop here if next has > 1
_nid = f"id_{cond['next_area'][0]}"
print(f"#{tk['id']} origin:#{_origin} to#{_nid}", end="")
# print(f"#{tk['id']} origin:#{_origin} to#{_nid}", end="")
print(f" {tk['id']} LEFT from {_origin} -> {_nid}")
if _nid not in W4A:
print(f">>add AREA {_nid} to W4A", end="")
# print(f">>add AREA {_nid} to W4A", end="")
W4A[_nid] = {"objects": []}
# put this object to W4A for next area if doesn't exist
has_this = [_ for _ in W4A[_nid]["objects"] if _[0]["id"] == tk["id"]]
@ -260,7 +262,7 @@ while True:
_expected_frame = _frame_count + cond["duration_to_next"]
W4A[_nid]["objects"].append((tk, _frame_count, _expected_frame))
untracking.append(tk)
print(f'>>GONE - W#{len(W4A[_nid]["objects"])}')
# print(f'>>GONE - W#{len(W4A[_nid]["objects"])}')
# print(f' {_nid} objs: ')
# pp.pprint(W4A[_nid]["objects"])
# print(f' {_nid} untracking: ', [_['id'] for _ in untracking])
@ -303,8 +305,9 @@ while True:
for _trckr in trackers:
__tkr = OPENCV_OBJECT_TRACKERS["csrt"]()
cv_trackers.add(__tkr, frame, tuple(_trckr["curr_position"]))
print(f"=== AFTER CLEANUP ---- UNTRACKING ===")
print(f" cv #{len(cv_trackers.getObjects())} trackers #{len(trackers)}")
# print(f"=== AFTER CLEANUP ---- UNTRACKING ===")
# print(f" cv #{len(cv_trackers.getObjects())} trackers #{len(trackers)}")
print(f" Total #{len(cv_trackers.getObjects())}")
# only detect once a sec
if _frame_count % 15 == 1:
@ -346,9 +349,9 @@ while True:
W4A[_area_id]["objects"] = [
_ for _ in W4A[_area_id]["objects"] if _[0]["id"] != gid
]
print(f" --- {len(_po)} candicate: picked id={gid}")
else:
print(f" --- no candidate")
# print(f" --- {len(_po)} candicate: picked id={gid}")
# else:
# print(f" --- no candidate")
# create tracker and add to multi-object tracker
_tracker = OPENCV_OBJECT_TRACKERS["csrt"]()
@ -365,14 +368,38 @@ while True:
"still": 0,
}
if gid is None:
print(f" {t['id']} ENTERS")
tracker_counter += 1
else:
print(f" {t['id']} RE-ENTERS")
trackers.append(t)
print(f"trackers ADDED - now total #{len(trackers)}")
print(f" total #{len(trackers)}")
pp.pprint(t)
# print(f" i -> {i} ({x},{y}), {w},{h} ({x + w},{y + h})")
_what = ",".join([LABELS[c] for c in classIDs])
print(f"[{_frame_count:08d}] :: {_what}")
# _what = ",".join([LABELS[c] for c in classIDs])
# print(f"[{_frame_count:08d}] :: {_what}")
# CLEANUP
cleanups = []
for k in W4A:
for _o in W4A[k]["objects"]:
print(f" CLEANUP: {_o[0]['id']} - {_o[1]} - {_o[2]}", end="\r\n")
if _o[2] < _frame_count:
continue
wf = _o[2] - _o[1]
wf = wf if wf < 30 * 6 else 180
if _o[2] - wf > _frame_count:
# get rid of this
cleanups.append(_o)
# remove this id out of next W4A
W4A[k]["objects"] = [
_ for _ in W4A[_area_id]["objects"] if _[0]["id"] != _o[0]["id"]
]
for obj, _s, _e in cleanups:
print(
f" {obj['id']} CLEANED UP should found at {_e - _frame_count} ago"
)
if args["live"]:
cv2.imshow("Frame", frame)
@ -405,7 +432,6 @@ while True:
# write the output frame to disk
writer.write(frame)
# TODO: cleanup W4A that is WAY over due -- it's unknown anyway..
# TODO: find a way to get rid of IMPOSSIBLE obj for example
# suddenly appear on area 7 then move up NORTH... -- this is incorrectly detected indeed.

21
src/utils.py

@ -185,12 +185,12 @@ def is_it_the_same_obj(x1, y1, w1, h1, i1, j1, w2, h2, **kwargs):
object and of course, dimension too.
"""
_id = kwargs.get("id", None)
if _id:
print(" :: check against id:", _id, end="")
# if _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")
# print(" same 1st coords")
return True
DIMENSION_SHIFT = 0.15
@ -199,14 +199,13 @@ def is_it_the_same_obj(x1, y1, w1, h1, i1, j1, w2, h2, **kwargs):
c_dff_x, c_dff_y = abs(cx2 - cx1), abs(cy2 - cy1)
w_shift, h_shift = w1 * DIMENSION_SHIFT, h1 * DIMENSION_SHIFT
print(" ::SAME:: shift", end="")
print(f" | SHIFT w:{w_shift},h:{h_shift}", end="")
print(f" | centroid {c_dff_x}, {c_dff_y}", end="")
# print(" ::SAME:: shift", end="")
# print(f" | SHIFT w:{w_shift},h:{h_shift}", end="")
# print(f" | centroid {c_dff_x}, {c_dff_y}", end="")
if c_dff_x > w_shift and c_dff_y > h_shift:
# print(" ::SAME:: shift too much already -- NOT THE SAME")
return False
first_w_smaller = i1 > x1 and (w1 - w2) > (i1 - x1)
# if one inside the other
if i1 > x1 and (w1 - w2) > (i1 - x1) and j1 > y1 and h1 - h2 > j1 - y1:
# one is inside the other
@ -228,12 +227,12 @@ def is_it_the_same_obj(x1, y1, w1, h1, i1, j1, w2, h2, **kwargs):
# if size is larger than 20%, then it's not the same thing
# print(" ::SAME:: size")
if abs(size2 - size1) / size1 > 0.45:
print(f" sz {size1}, {size2}, diff%{abs(size2 - size1)/size1}", end="")
print(" ^^ too diff in size -- NOT THE SAME", end="")
# print(f" sz {size1}, {size2}, diff%{abs(size2 - size1)/size1}", end="")
# print(" ^^ too diff in size -- NOT THE SAME", end="")
return False
print(" ::SAME:: last")
print("")
# print(" ::SAME:: last")
# print("")
return True

Loading…
Cancel
Save