-
Notifications
You must be signed in to change notification settings - Fork 2
/
DetectorPreview.py
142 lines (99 loc) · 5.08 KB
/
DetectorPreview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import argparse
from time import time
import os
import cv2
import logging
import numpy as np
from RealtimeCapture import RealtimeCapture
logging.basicConfig()
logger = logging.getLogger("DetectorPreviewLog")
logger.setLevel(logging.INFO)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Preview videos using detectors.')
parser.add_argument('path', metavar='path', type=str, nargs=1,
help='path to video file')
parser.add_argument('detector', metavar='detector', type=str, nargs=1,
help='detector name: tfod, tfop, op, blob, hog, haar')
parser.add_argument('--scale', metavar='scale', type=float, nargs=1,
help='scale of video', default=[1])
parser.add_argument('--vdup', metavar='vdup', type=int, nargs=1,
help='vertical duplication of video', default=[1])
parser.add_argument('--hdup', metavar='hdup', type=int, nargs=1,
help='horizontal duplication of video', default=[1])
parser.add_argument('--preview', metavar='preview', type=bool, nargs=1,
help='detector preview of video', default=[False])
parser.add_argument('--url', metavar='url', type=str, nargs=1,
help='url to load from', default=[None])
# const=sum, default=max,
# help='sum the integers (default: find the max)')
args = parser.parse_args()
video_path = os.path.realpath(args.path[0])
detector_name = args.detector[0].lower()
video_scale = args.scale[0]
preview_detector = args.preview[0]
x_dup_count = args.hdup[0]
y_dup_count = args.vdup[0]
url = args.url[0]
if url is not None:
video_path = url
os.chdir(os.path.dirname(__file__))
logger.info("File Name: " + video_path)
logger.info("Detector Name: " + detector_name)
detector = None
if detector_name == "tfod":
from detectors.TFODDetector.TFODPersonDetector import TFODPersonDetector
detector = TFODPersonDetector(preview=preview_detector)
elif detector_name == "tfop":
from detectors.TFOPDetector.TFOPDetector import TFOPersonDetector
detector = TFOPersonDetector(preview=preview_detector)
elif detector_name == "blob":
from detectors.BlobPersonDetector.BlogPersonDetector import BlobPersonDetector
detector = BlobPersonDetector(preview=preview_detector)
elif detector_name == "hog":
from detectors.HogPersonDetector.HogPersonDetector import HogPersonDetector
detector = HogPersonDetector(preview=preview_detector)
elif detector_name == "haar":
from detectors.HaarPersonDetector.HaarPersonDetector import HaarPersonDetector
detector = HaarPersonDetector(preview=preview_detector)
elif detector_name == "mask":
from detectors.MaskDetector.MaskPersonDetector import MaskPersonDetector
detector = MaskPersonDetector(preview=preview_detector)
cap = RealtimeCapture(video_path)
cv2.namedWindow("Detector Output", cv2.WINDOW_FREERATIO)
# fgbg = cv2.createBackgroundSubtractorMOG2()
last_frame_time = 0
while True:
r, frame = cap.read()
if not r:
break
frame_start_time = time()
frame = cv2.resize(frame, (0, 0), fx=video_scale, fy=video_scale)
if frame is not None:
new_frame = np.zeros((int(frame.shape[0] * y_dup_count), int(frame.shape[1] * x_dup_count), 3),
dtype=frame.dtype)
for y_d in range(y_dup_count):
for x_d in range(x_dup_count):
new_frame[y_d * frame.shape[0]:(((y_d + 1) * frame.shape[0])),
x_d * frame.shape[1]:(((x_d + 1) * frame.shape[1])), :] = np.array(frame, copy=True)[:, :, :]
frame = new_frame
# fgmask = fgbg.apply(frame)
cv2.putText(frame, "Time: " + str(cap.video_frame_time), (20, 25), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0))
cv2.putText(frame, "Frame Time: " + str(last_frame_time), (20, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0))
person_detections = detector.detectPersons(frame, None)
for detection in person_detections:
cv2.rectangle(frame, (detection.person_bound[0], detection.person_bound[1]),
(detection.person_bound[2], detection.person_bound[3]), (0, 0, 255), 2)
if hasattr(detection, "tracked_points"):
tracked_points = detection.tracked_points
for name, tracked_point in tracked_points.items():
cv2.drawMarker(frame, (int(tracked_point[0]), int(tracked_point[1])), (0, 255, 255), markerSize=10)
if hasattr(detector, "draw_patches"):
frame = detector.draw_patches(frame)
cv2.imshow("Detector Output", frame)
# cv2.imshow("Mask", fgmask)
k = cv2.waitKey(1)
if k & 0xFF == ord("q"):
break
frame_end_time = time()
last_frame_time = frame_end_time - frame_start_time
logger.info("Last Frame Time: " + str(last_frame_time))