-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode1.py
More file actions
71 lines (54 loc) · 2.15 KB
/
code1.py
File metadata and controls
71 lines (54 loc) · 2.15 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
import cv2
import numpy as np
thres = 0.45 # Threshold to detect object
nms_threshold = 0.2 # Non-maximum suppression threshold
cap = cv2.VideoCapture(0) # Use 0 for the default camera
# Check if the camera opened successfully
if not cap.isOpened():
print("Error: Could not open the camera.")
exit()
# Load class names from coco.names file
classNames = []
classFile = 'coco.names'
with open(classFile, 'r') as f:
classNames = f.read().rstrip('\n').split('\n')
# Load configuration and weights for the model
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
# Video capture loop
while True:
success, img = cap.read()
# Check if frame was read successfully
if not success:
print("Error: Failed to read frame from camera.")
break
classIds, confs, bbox = net.detect(img, confThreshold=thres)
# Convert bbox and confs to list format
bbox = list(bbox)
confs = list(np.array(confs).reshape(1, -1)[0])
confs = list(map(float, confs))
# Flatten classIds for correct indexing
classIds = classIds.flatten()
# Perform Non-Maximum Suppression to remove overlapping boxes
indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)
if len(indices) > 0:
for i in indices.flatten():
box = bbox[i]
x, y, w, h = box[0], box[1], box[2], box[3]
# Use classIds[i] directly instead of classIds[i][0]
cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classIds[i] - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
# Display the output
cv2.imshow('Output', img)
# Press 'q' to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and destroy windows
cap.release()
cv2.destroyAllWindows()