-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_test.py
53 lines (40 loc) · 1.84 KB
/
video_test.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
import cv2
# Load the pre-trained MobileNet SSD model
net = cv2.dnn.readNetFromCaffe("model/MobileNetSSD_deploy.prototxt", "model/MobileNetSSD_deploy.caffemodel")
# List of class labels
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
cap = cv2.VideoCapture(0)
while True:
# Read frame from webcam
ret, frame = cap.read()
# Resize the frame and preprocess for object detection
frame_resized = cv2.resize(frame, (300, 300))
blob = cv2.dnn.blobFromImage(frame_resized, 0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
# Log the relevant information about detected objects
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
# Filter out weak detections
if confidence > 0.5:
class_id = int(detections[0, 0, i, 1])
label = CLASSES[class_id]
# Extract coordinates of the bounding box
box = detections[0, 0, i, 3:7] * 300
(startX, startY, endX, endY) = box.astype("int")
# Log the detected object and its confidence score
print(f"Detected {label} with confidence score: {confidence}")
# Draw box around the detected object
cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.putText(frame, label, (startX, startY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display the output frame
cv2.imshow("Object Detection", frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()