|
|
|
@ -5,17 +5,18 @@ time python src/_detector.py --input ~/Desktop/5min.mp4 -l
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
# import the necessary packages |
|
|
|
|
import numpy as np |
|
|
|
|
import time |
|
|
|
|
import argparse |
|
|
|
|
import pprint |
|
|
|
|
import numpy as np |
|
|
|
|
import imutils |
|
|
|
|
import time |
|
|
|
|
import cv2 |
|
|
|
|
import pprint |
|
|
|
|
from utils import ( |
|
|
|
|
check_if_inside_the_boxes, |
|
|
|
|
is_it_the_same_obj, |
|
|
|
|
box_distance, |
|
|
|
|
get_heading, |
|
|
|
|
get_avg_heading, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
pp = pprint.PrettyPrinter(indent=2) |
|
|
|
@ -25,6 +26,7 @@ OPENCV_OBJECT_TRACKERS = {"csrt": cv2.TrackerCSRT_create}
|
|
|
|
|
cv_trackers = cv2.MultiTracker_create() |
|
|
|
|
trackers = [] |
|
|
|
|
finished = [] |
|
|
|
|
W4A = {} # this stands for "wait for arrival [at ...]" |
|
|
|
|
|
|
|
|
|
# construct the argument parse and parse the arguments |
|
|
|
|
ap = argparse.ArgumentParser() |
|
|
|
@ -72,7 +74,7 @@ ln = net.getLayerNames()
|
|
|
|
|
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def detect_stuffs(net, frame): |
|
|
|
|
def detect_stuffs(frame): |
|
|
|
|
# construct a blob from the input frame and then perform a forward |
|
|
|
|
# pass of the YOLO object detector, giving us our bounding boxes |
|
|
|
|
# and associated probabilities |
|
|
|
@ -228,11 +230,25 @@ while True:
|
|
|
|
|
if trackers[idx]["still"] > 30 or x < 5 or x > 1250: |
|
|
|
|
untracking.append(trackers[idx]) |
|
|
|
|
|
|
|
|
|
# DRAW on FRAME |
|
|
|
|
tk = trackers[idx] |
|
|
|
|
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) |
|
|
|
|
# cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) |
|
|
|
|
color = [int(c) for c in COLORS[0]] |
|
|
|
|
# check if it's hit the first |
|
|
|
|
avg_heading = get_avg_heading(trackers[idx]["heading"]) |
|
|
|
|
print(f" ---> avg heading: ", avg_heading) |
|
|
|
|
if ( |
|
|
|
|
tk["first"]["id"] == 4 |
|
|
|
|
and avg_heading == "N" |
|
|
|
|
and (x > 140 or x + w > 175) |
|
|
|
|
): |
|
|
|
|
print("gone!") |
|
|
|
|
_nid = f"id_5" |
|
|
|
|
if _nid not in W4A: |
|
|
|
|
W4A[_nid] = {"objects": []} |
|
|
|
|
_expected_frame = 30 * 4 # at least 4 sec later |
|
|
|
|
has_this = [_ for _ in W4A[_nid]["objects"] if _[0]["id"] == tk["id"]] |
|
|
|
|
if not has_this: |
|
|
|
|
W4A[_nid]["objects"].append((tk, _frame_count, _expected_frame)) |
|
|
|
|
untracking.append(trackers[idx]) |
|
|
|
|
|
|
|
|
|
print( |
|
|
|
|
f"[{tk['id']}-{tk['type']}] (x,y)=({x},{y})" |
|
|
|
|
f" | still #{tk['still']} | distance: " |
|
|
|
@ -240,6 +256,10 @@ while True:
|
|
|
|
|
) |
|
|
|
|
_htxt = ",".join(trackers[idx]["heading"]) |
|
|
|
|
print(f" ------ heading: {_htxt}") |
|
|
|
|
|
|
|
|
|
# DRAW on FRAME |
|
|
|
|
color = [int(c) for c in COLORS[0]] |
|
|
|
|
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) |
|
|
|
|
cv2.putText( |
|
|
|
|
frame, |
|
|
|
|
f"{tk['id']} - {tk['type']}", |
|
|
|
@ -252,7 +272,7 @@ while True:
|
|
|
|
|
|
|
|
|
|
# only detect once a sec |
|
|
|
|
if _frame_count % 15 == 1: |
|
|
|
|
idxs, boxes, confidences, classIDs, start, end = detect_stuffs(net, frame) |
|
|
|
|
idxs, boxes, confidences, classIDs, start, end = detect_stuffs(frame) |
|
|
|
|
# loop over the indexes we are keeping |
|
|
|
|
for i in idxs.flatten(): |
|
|
|
|
# extract the bounding box coordinates |
|
|
|
@ -277,6 +297,20 @@ while True:
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
if not is_same: |
|
|
|
|
gid = None |
|
|
|
|
if found_at["id"] == 5: |
|
|
|
|
print(f"FOUND AT 5 ON {_frame_count}") |
|
|
|
|
_po = W4A["id_5"]["objects"] |
|
|
|
|
_po = [_ for _ in _po if _frame_count > _[2]] |
|
|
|
|
_po = sorted(_po, key=lambda kk: kk[2]) |
|
|
|
|
print(' ------ ', _po) |
|
|
|
|
if _po: |
|
|
|
|
gid = _po[0][0]["id"] |
|
|
|
|
# remove this id out of next W4A |
|
|
|
|
W4A["id_5"]["objects"] = [ |
|
|
|
|
_ for _ in W4A["id_5"]["objects"] if _[0]["id"] == gid |
|
|
|
|
] |
|
|
|
|
print(f" >> possibly this obj: ", _po) |
|
|
|
|
# add tracker to this obj |
|
|
|
|
# create a new object tracker for the bounding box and add it |
|
|
|
|
# to our multi-object tracker |
|
|
|
@ -286,7 +320,7 @@ while True:
|
|
|
|
|
bbox = (x, y, w, h) |
|
|
|
|
cv_trackers.add(_tracker, frame, bbox) |
|
|
|
|
t = { |
|
|
|
|
"id": tracker_counter, |
|
|
|
|
"id": tracker_counter if gid is None else gid, |
|
|
|
|
"type": _class, |
|
|
|
|
"curr_position": bbox, |
|
|
|
|
"heading": [], |
|
|
|
@ -295,7 +329,8 @@ while True:
|
|
|
|
|
"last_position": bbox, |
|
|
|
|
"still": 0, |
|
|
|
|
} |
|
|
|
|
tracker_counter += 1 |
|
|
|
|
if gid is None: |
|
|
|
|
tracker_counter += 1 |
|
|
|
|
trackers.append(t) |
|
|
|
|
print(f"trackers ADD - now total #{len(trackers)}") |
|
|
|
|
pp.pprint(t) |
|
|
|
|