|
|
|
@ -4,10 +4,8 @@ time python src/_detector.py --input ~/Desktop/5min.mp4 -o output.mp4
|
|
|
|
|
time python src/_detector.py --input ~/Desktop/5min.mp4 -l |
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
# import the necessary packages |
|
|
|
|
import sys |
|
|
|
|
import time |
|
|
|
|
import logging |
|
|
|
|
import argparse |
|
|
|
|
import pprint |
|
|
|
|
import numpy as np |
|
|
|
@ -22,8 +20,10 @@ from utils import (
|
|
|
|
|
OBJ_LEAVING_COND, |
|
|
|
|
DONTCARE, |
|
|
|
|
) |
|
|
|
|
from log import init_logger, logging |
|
|
|
|
|
|
|
|
|
init_logger() |
|
|
|
|
|
|
|
|
|
logging.basicConfig(filename="trace.log", filemode="w", level=logging.DEBUG) |
|
|
|
|
pp = pprint.PrettyPrinter(indent=2) |
|
|
|
|
# tracking |
|
|
|
|
OPENCV_OBJECT_TRACKERS = {"csrt": cv2.TrackerCSRT_create} |
|
|
|
@ -73,7 +73,7 @@ configPath = "../../syncthing/dropbox/tracking-obj/mytrain.cfg"
|
|
|
|
|
|
|
|
|
|
# load our YOLO object detector trained on COCO dataset (80 classes) |
|
|
|
|
# and determine only the *output* layer names that we need from YOLO |
|
|
|
|
print("[INFO] loading YOLO from disk...") |
|
|
|
|
logging.debug("[INFO] loading YOLO from disk...") |
|
|
|
|
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath) |
|
|
|
|
ln = net.getLayerNames() |
|
|
|
|
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] |
|
|
|
@ -168,10 +168,11 @@ except:
|
|
|
|
|
_frame_count = 0 |
|
|
|
|
tracker_counter = 1 |
|
|
|
|
|
|
|
|
|
logging.debug("INIT") |
|
|
|
|
|
|
|
|
|
for xx in range(3996): |
|
|
|
|
(grabbed, frame) = vs.read() |
|
|
|
|
_frame_count += 1 |
|
|
|
|
# for xx in range(3996): |
|
|
|
|
# (grabbed, frame) = vs.read() |
|
|
|
|
# _frame_count += 1 |
|
|
|
|
|
|
|
|
|
# loop over frames from the video file stream |
|
|
|
|
while True: |
|
|
|
@ -223,11 +224,17 @@ while True:
|
|
|
|
|
box = boxes[idx] |
|
|
|
|
(x, y, w, h) = [int(v) for v in box] |
|
|
|
|
|
|
|
|
|
logging.info(f"{_frame_count},{trackers[idx]['id']},{trackers[idx]['type']},POSITION,{x},{y},{w},{h}") |
|
|
|
|
|
|
|
|
|
# check if size is growing? if more than twice then, untrack it |
|
|
|
|
# TODO: make it static bound should be better |
|
|
|
|
ow, oh = trackers[idx]["size"] |
|
|
|
|
if ow / w > 2 or oh / h > 2: |
|
|
|
|
print(f" {tk['id']} GROW_TOO_BIG") |
|
|
|
|
trackers[idx]["status"] = "grow-too-big" |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},GROW_TOO_BIG,{x},{y},{w},{h}" |
|
|
|
|
) |
|
|
|
|
untracking.append(trackers[idx]) |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
@ -255,10 +262,16 @@ while True:
|
|
|
|
|
# trackers[idx]["heading"] = trackers[idx]["heading"][:20] |
|
|
|
|
|
|
|
|
|
if trackers[idx]["still"] > 30 or x < 5 or x > 1250: |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},LEFT,{x},{y},{w},{h},STILL" |
|
|
|
|
) |
|
|
|
|
untracking.append(trackers[idx]) |
|
|
|
|
|
|
|
|
|
if trackers[idx]["still"] > 10 and len(trackers[idx]["heading"]) < 5: |
|
|
|
|
print(f" {tk['id']} LEFT - short-life") |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},LEFT,{x},{y},{w},{h},SHORT_LIFE" |
|
|
|
|
) |
|
|
|
|
trackers[idx]["status"] = "short-life" |
|
|
|
|
untracking.append(trackers[idx]) |
|
|
|
|
|
|
|
|
@ -275,6 +288,9 @@ while True:
|
|
|
|
|
print(DONTCARE_IDS, tk["origin"]["id"], dc_id, dc_dict["heading"]) |
|
|
|
|
if avg_heading in dc_dict["heading"]: |
|
|
|
|
print(f" {tk['id']} DONT-CARE condition") |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},LEFT,{x},{y},{w},{h},WRONG_DETECTION" |
|
|
|
|
) |
|
|
|
|
trackers[idx]["status"] = "dont-care" |
|
|
|
|
untracking.append(trackers[idx]) |
|
|
|
|
|
|
|
|
@ -296,11 +312,19 @@ while True:
|
|
|
|
|
# print(f" --- {len(_po)} candicate: picked id={gid}") |
|
|
|
|
# else: |
|
|
|
|
# print(f" --- no candidate") |
|
|
|
|
print(f" {gid} RE-ENTERS") |
|
|
|
|
print( |
|
|
|
|
f" {gid} RE-ENTERS (origin: {trackers[idx]['origin']['id']})" |
|
|
|
|
) |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},RE-ENTER,{x},{y},{w},{h}" |
|
|
|
|
) |
|
|
|
|
elif tk["id"] == -1 and h_count >= 15: |
|
|
|
|
# assign an id |
|
|
|
|
trackers[idx]["id"] = tracker_counter |
|
|
|
|
tracker_counter += 1 |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},ENTER,{x},{y},{w},{h}" |
|
|
|
|
) |
|
|
|
|
print(f" {trackers[idx]['id']} ENTERS (& ID assigned)") |
|
|
|
|
|
|
|
|
|
for lvng_cnd in OBJ_LEAVING_COND: |
|
|
|
@ -337,9 +361,23 @@ while True:
|
|
|
|
|
|
|
|
|
|
# print("MATCH COND") |
|
|
|
|
# pp.pprint(cond) |
|
|
|
|
if not cond["next_area"]: |
|
|
|
|
# left the frame |
|
|
|
|
print(f" {tk['id']} LEFT from frame") |
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},LEFT,{x},{y},{w},{h}" |
|
|
|
|
) |
|
|
|
|
untracking.append(tk) |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
_nid = f"id_{cond['next_area'][0]}" |
|
|
|
|
# print(f"#{tk['id']} origin:#{_origin} to#{_nid}", end="") |
|
|
|
|
print(f" {tk['id']} LEFT from {_origin} -> {_nid}") |
|
|
|
|
|
|
|
|
|
logging.info( |
|
|
|
|
f"{_frame_count},{tk['id']},{tk['type']},WAIT,{x},{y},{w},{h},SWITCH_ZONE:{_origin}:{_nid}" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if _nid not in W4A: |
|
|
|
|
# print(f">>add AREA {_nid} to W4A", end="") |
|
|
|
|
W4A[_nid] = {"objects": []} |
|
|
|
|