Untitled
user_9363972
python
2 years ago
1.8 kB
7
Indexable
import numpy as np
import cv2
min_confidence = 0.6
classes = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow',
'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
net = cv2.dnn.readNetFromCaffe(
"models/MobileNetSSD_deploy.prototxt.txt", "models/MobileNetSSD_deploy.caffemodel"
)
colors = np.random.uniform(0, 255, size=(len(classes), 3))
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
height, width = frame.shape[0], frame.shape[1]
blob = cv2.dnn.blobFromImage(cv2.resize(
frame, (300, 300)), 0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > min_confidence:
class_id = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * \
np.array([width, height, width, height])
(startX, startY, endX, endY) = box.astype('int')
text = "{:.2f}%".format(confidence * 100) + \
f"- {classes[class_id]}"
y = startY - 10 if startY - 10 > 10 else startY+10
print(startX, startY, endX, endY)
cv2.rectangle(frame, (startX, startY),
(endX, endY), colors[class_id], 2)
cv2.putText(frame, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, colors[class_id], 2)
print(classes[class_id])
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Editor is loading...
Leave a Comment