-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathobjedet.py
More file actions
57 lines (38 loc) · 1.88 KB
/
objedet.py
File metadata and controls
57 lines (38 loc) · 1.88 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
import numpy as np
import cv2
prototxt_path = 'MobileNetSSD_deploy.prototxt'
model_path = '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']
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:
ret, 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)
roundConfi = confidence * 100
predection_text = f"{classes[class_index]}: {roundConfi:.2f}%"
cv2.rectangle(image, (upper_left_x, upper_left_y), (lower_right_x, lower_right_y), colors[class_index], 3)
cv2.putText(image, predection_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", image)
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.release()