sipp11 5 years ago
parent
commit
ae3fa32138
  1. 19
      src/log.py
  2. 54
      src/main.py
  3. 36
      src/utils.py

19
src/log.py

@ -0,0 +1,19 @@
import logging
def init_logger():
logger = logging.getLogger() # get the root logger
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("[%(levelname)s] %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
trace_handler = logging.FileHandler("trace.log", "w")
trace_handler.setLevel(logging.INFO)
# trace_formatter = logging.Formatter("%(asctime)s[%(levelname)s],%(message)s")
trace_formatter = logging.Formatter("%(message)s")
trace_handler.setFormatter(trace_formatter)
logger.addHandler(trace_handler)

54
src/main.py

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

36
src/utils.py

@ -92,12 +92,12 @@ OBJ_LEAVING_COND = [
# ('duration_to_next', ) # ('duration_to_next', )
# ], # ],
[ [
('origin_id', 2), ("origin_id", 2),
('heading', ''), ("heading", ""),
('x', lambda y: (y - 112.6) / -0.2270), ("x", lambda y: (y - 112.6) / -0.2270),
('y', -1), ("y", -1),
('next_area', [5, 7]), ("next_area", [5, 7]),
('duration_to_next', 30 * 5.5) ("duration_to_next", 30 * 5.5),
], ],
[ [
("origin_id", 3), ("origin_id", 3),
@ -107,14 +107,14 @@ OBJ_LEAVING_COND = [
("next_area", [5]), ("next_area", [5]),
("duration_to_next", 30 * 4), ("duration_to_next", 30 * 4),
], ],
# [ [
# ("origin_id", 4), ("origin_id", 4),
# ("heading", "N"), ("heading", "S"),
# ("x", 175), ("x", 34),
# ("y", -1), ("y", -1),
# ("next_area", [5]), ("next_area", []),
# ("duration_to_next", 30 * 4), ("duration_to_next", 30 * 0),
# ], ],
[ [
("origin_id", 5), ("origin_id", 5),
("heading", "N"), ("heading", "N"),
@ -139,6 +139,14 @@ OBJ_LEAVING_COND = [
("next_area", [4]), ("next_area", [4]),
("duration_to_next", 30 * 4), ("duration_to_next", 30 * 4),
], ],
[
("origin_id", 11),
("heading", "S"),
("x", 870),
("y", -1),
("next_area", [7]),
("duration_to_next", 30 * 2),
],
# [ # [
# ('origin_id', ), # ('origin_id', ),
# ('heading', ''), # ('heading', ''),

Loading…
Cancel
Save