-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam_demo.py
44 lines (32 loc) · 1.41 KB
/
webcam_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
import time
import pydarknet
from pydarknet import Detector, Image
import cv2
if __name__ == "__main__":
# Optional statement to configure preferred GPU. Available only in GPU version.
# pydarknet.set_cuda_device(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"))
cap = cv2.VideoCapture(0)
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()
# 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("Elapsed Time:",end_time-start_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()