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
"""
# 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": []}

36
src/utils.py

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

Loading…
Cancel
Save