-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_demo.py
54 lines (37 loc) · 1.68 KB
/
video_demo.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
import time
from pydarknet import Detector, Image
import cv2
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Process a video.')
parser.add_argument('path', metavar='video_path', type=str,
help='Path to source video')
args = parser.parse_args()
print("Source Path:", args.path)
cap = cv2.VideoCapture(args.path)
average_time = 0
net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0,
bytes("cfg/coco.data", encoding="utf-8"))
while True:
r, frame = cap.read()
if r:
start_time = time.time()
# Only measure the time taken by YOLO and API Call overhead
dark_frame = Image(frame)
results = net.detect(dark_frame)
del dark_frame
end_time = time.time()
average_time = average_time * 0.8 + (end_time-start_time) * 0.2
# Frames per second can be calculated as 1 frame divided by time required to process 1 frame
fps = 1 / (end_time - start_time)
print("FPS: ", fps)
print("Total Time:", end_time-start_time, ":", average_time)
for cat, score, bounds in results:
x, y, w, h = bounds
cv2.rectangle(frame, (int(x-w/2),int(y-h/2)),(int(x+w/2),int(y+h/2)),(255,0,0))
cv2.putText(frame, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0))
cv2.imshow("preview", frame)
k = cv2.waitKey(1)
if k == 0xFF & ord("q"):
break
cap.release()