Ai
1 Star 0 Fork 1

Leo/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
objects_dnn.py 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
import cv2
import numpy as np
model = cv2.dnn.readNetFromCaffe(
'objects_data/MobileNetSSD_deploy.prototxt',
'objects_data/MobileNetSSD_deploy.caffemodel')
blob_height = 300
color_scale = 1.0/127.5
average_color = (127.5, 127.5, 127.5)
confidence_threshold = 0.5
labels = ['airplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
'car', 'cat', 'chair', 'cow', 'dining table', 'dog',
'horse', 'motorbike', 'person', 'potted plant', 'sheep',
'sofa', 'train', 'TV or monitor']
cap = cv2.VideoCapture(0)
success, frame = cap.read()
while success:
h, w = frame.shape[:2]
aspect_ratio = w/h
# Detect objects in the frame.
blob_width = int(blob_height * aspect_ratio)
blob_size = (blob_width, blob_height)
blob = cv2.dnn.blobFromImage(
frame, scalefactor=color_scale, size=blob_size,
mean=average_color)
model.setInput(blob)
results = model.forward()
# Iterate over the detected objects.
for object in results[0, 0]:
confidence = object[2]
if confidence > confidence_threshold:
# Get the object's coordinates.
x0, y0, x1, y1 = (object[3:7] * [w, h, w, h]).astype(int)
# Get the classification result.
id = int(object[1])
label = labels[id - 1]
# Draw a blue rectangle around the object.
cv2.rectangle(frame, (x0, y0), (x1, y1),
(255, 0, 0), 2)
# Draw the classification result and confidence.
text = '%s (%.1f%%)' % (label, confidence * 100.0)
cv2.putText(frame, text, (x0, y0 - 20),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow('Objects', frame)
k = cv2.waitKey(1)
if k == 27: # Escape
break
success, frame = cap.read()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/akwkevin/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition.git
git@gitee.com:akwkevin/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition.git
akwkevin
Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition
Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition
master

搜索帮助