-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcam.py
executable file
·72 lines (52 loc) · 2.06 KB
/
webcam.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
import cv2
import numpy as np
import time
net = cv2.dnn.readNet("../include/yolov3-tiny-head_final.weights", "../include/yolov3-tiny-head.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
scale = 0.00392
conf_threshold = 0.5
nms_threshold = 0.4
# define the frame properties
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]
capture = cv2.VideoCapture('/dev/video0')
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 416)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 416)
while True:
ret, frame = capture.read()
if ret:
height, width, channels = frame.shape
image = cv2.resize(frame, (416,416))
blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0, 0, 0), True)
net.setInput(blob)
outs=net.forward(output_layers)
confidences = []
boxes = []
for out in outs:
for detection in out:
confidence = detection[5]
if confidence > 0.35:
center_x = int(detection[0]*width)
center_y = int(detection[1]*height)
w = int(detection[2]*width)
h = int(detection[3]*height)
x = center_x - w/2
y = center_y - h/2
confidences.append(float(confidence))
boxes.append([x, y, w, h])
# apply non-max suppression
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
# go through the detections remaining
# after nms and draw bounding box
for i in indices:
i = i[0]
x,y,w,h = boxes[i]
confidence = confidences[i]
color = colors[1]
cv2.rectangle(frame,(x,y),(x+w,y+h),color,2)
cv2.putText(frame,str(round(confidence,2)),(x,y-10), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()