目标检测进阶:使用深度学习和 OpenCV 进行目标检测(2)

load our serialized model from disk

print(“[INFO] loading model…”)

net = cv2.dnn.readNetFromCaffe(prototxt, model_path)

加载输入图像并为图像构造一个输入blob

将大小调整为固定的300x300像素。

(注意:SSD模型的输入是300x300像素)

image = cv2.imread(image_name)

(h, w) = image.shape[:2]

blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843,

(300, 300), 127.5)

通过网络传递blob并获得检测结果和

预测

print(“[INFO] computing object detections…”)

net.setInput(blob)

detections = net.forward()

从磁盘加载模型。

读取图片。

提取高度和宽度(第 35 行),并从图像中计算一个 300 x 300 像素的 blob。

将blob放入神经网络。

计算输入的前向传递,将结果存储为 detections。

循环检测结果

for i in np.arange(0, detections.shape[2]):

提取与数据相关的置信度(即概率)

预测

confidence = detections[0, 0, i, 2]

通过确保“置信度”来过滤掉弱检测

大于最小置信度

if confidence > confidence_ta:

detections中提取类标签的索引,

然后计算物体边界框的 (x, y) 坐标

idx = int(detections[0, 0, i, 1])

box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])

(startX, startY, endX, endY) = box.astype(“int”)

显示预测

label = “{}: {:.2f}%”.format(CLASSES[idx], confidence * 100)

print(“[INFO] {}”.format(label))

cv2.rectangle(image, (startX, startY), (endX, endY),

COLORS[idx], 2)

y = startY - 15 if startY - 15 > 15 else startY + 15

cv2.putText(image, label, (startX, y),

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值