-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam_code.py
More file actions
112 lines (80 loc) · 3.38 KB
/
webcam_code.py
File metadata and controls
112 lines (80 loc) · 3.38 KB
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
import numpy as np
import time
import cv2
import os
import PySimpleGUI as sg
y_path = r'yolo-coco'
sg.theme('LightGreen')
gui_confidence = .5 # initial settings
gui_threshold = .3 # initial settings
camera_number = 0 # if you have more than 1 camera, change this variable to choose which is used
labelsPath = os.path.sep.join([y_path, "coco.names"])
LABELS = open(labelsPath).read().strip().split("\n")
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8")
weightsPath = os.path.sep.join([y_path, "yolov3.weights"])
configPath = os.path.sep.join([y_path, "yolov3.cfg"])
sg.popup_quick_message('Loading YOLO weights from disk.... one moment...', background_color='red', text_color='white')
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
W, H = None, None
win_started = False
cap = cv2.VideoCapture(camera_number) # initialize the capture device
while True:
grabbed, frame = cap.read()
if not grabbed:
break
if not W or not H:
(H, W) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > gui_confidence:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, gui_confidence, gui_threshold)
if len(idxs) > 0:
for i in idxs.flatten():
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
color = [int(c) for c in COLORS[classIDs[i]]]
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
text = "{}: {:.4f}".format(LABELS[classIDs[i]],
confidences[i])
cv2.putText(frame, text, (x, y - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
imgbytes = cv2.imencode('.png', frame)[1].tobytes()
# ---------------------------- THE GUI ----------------------------
if not win_started:
win_started = True
layout = [
[sg.Text('Object Detection', size=(30, 1))],
[sg.Image(data=imgbytes, key='_IMAGE_')],
[sg.Exit()]
]
win = sg.Window('Object Detection Webcam ', layout, default_element_size=(14, 1), text_justification='right', auto_size_text=False, finalize=True)
image_elem = win['_IMAGE_']
else:
image_elem.update(data=imgbytes)
event, values = win.read(timeout=0)
if event is None or event == 'Exit':
break
print("[INFO] cleaning up...")
win.close()