-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (34 loc) · 1.82 KB
/
main.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
import cv2
import numpy as np
image_path = 'helmet2.png'
prototxt_path = 'models/MobileNetSSD_deploy.prototxt'
model_path = 'models/MobileNetSSD_deploy.caffemodel'
min_confidence = 0.2
classes = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor", "helmet", "Helmet"]
np.random.seed(543210)
colors = np.random.uniform(0, 255, size=(len(classes), 3))
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
cap = cv2.VideoCapture(0)
while True:
_, image = cap.read()
height, width = image.shape[0], image.shape[1]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007, (300, 300), 130)
net.setInput(blob)
detected_objects = net.forward()
for i in range(detected_objects.shape[2]):
confidence = detected_objects[0][0][i][2]
if confidence > min_confidence:
class_index = int(detected_objects[0, 0, i, 1])
upper_left_x = int(detected_objects[0, 0, i, 3] * width)
upper_left_y = int(detected_objects[0, 0, i, 4] * height)
lower_right_x = int(detected_objects[0, 0, i, 5] * width)
lower_right_y = int(detected_objects[0, 0, i, 6] * height)
prediction_text = f"{classes[class_index]}: {confidence:.2f}%"
cv2.rectangle(image, (upper_left_x, upper_left_y), (lower_right_x, lower_right_y), colors[class_index], 3)
cv2.putText(image, prediction_text, (upper_left_x, upper_left_y - 15 if upper_left_y > 30 else upper_left_y + 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, colors[class_index], 2)
cv2.imshow("Detected Objects", image)
if cv2.waitKey(5) & 0xFF == 27: # Press 'Esc' to exit
break
cv2.destroyAllWindows()
cap.release()