python实时目标检测_OpenCV+python实现实时目标检测功能

# loop over the detections

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

# extract the confidence (i.e., probability) associated with

# the prediction

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

# filter out weak detections by ensuring the `confidence` is

# greater than the minimum confidence

if confidence > args["confidence"]:

# extract the index of the class label from the

# `detections`, then compute the (x, y)-coordinates of

# the bounding box for the object

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")

# draw the prediction on the frame

label = "{}: {:.2f}%".format(CLASSES[idx],

confidence * 100)

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

COLORS[idx], 2)

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

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

cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)

在 detections 内循环,一个图像中可以检测到多个目标。因此我们需要检查置信度。如果置信度足够高(高于阈值),那么将在终端展示预测,并以文本和彩色边界框的形式对图像作出预测。

在 detections 内循环,首先我们提取 confidence 值,confidence = detections[0, 0, i, 2]。如果 confidence 高于最低阈值(if confidence > args["confidence"]:),那么提取类标签索引(idx = int(detections[0, 0, i, 1])),并计算检测到的目标的坐标(box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]))。然后,我们提取边界框的 (x, y) 坐标((startX, startY, endX, endY) = box.astype("int")),将用于绘制矩形和文本。接着构建一个文本 label,包含 CLASS 名称和 confidence(label = "{}: {:.2f}%".format(CLASSES[idx],confidence * 100))。还要使用类颜色和之前提取的 (x, y) 坐标在物体周围绘制彩色矩形(cv2.rectangle(frame, (startX, startY), (endX, endY),COLORS[idx], 2))。如果我们希望标签出现在矩形上方,但是如果没有空间,我们将在矩形顶部稍下的位置展示标签(y = startY - 15 if startY - 15 > 15 else startY + 15)。最后,我们使用刚才计算出的 y 值将彩色文本置于帧上(cv2.putText(frame, label, (startX, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2))。

7.帧捕捉循环剩余的步骤还包括:展示帧;检查 quit 键;更新 fps 计数器。

# show the output frame

cv2.imshow("Frame", frame)

key = cv2.waitKey(1) & 0xFF

# if the `q` key was pressed, break from the loop

if key == ord("q"):

break

# update the FPS counter

fps.update()

上述代码块简单明了,首先我们展示帧(cv2.imshow("Frame", frame)),然后找到特定按键(key = cv2.waitKey(1) & 0xFF),同时检查「q」键(代表「quit」)是否按下。如果已经按下,则我们退出帧捕捉循环(if key == ord("q"):break),最后更新 fps 计数器(fps.update())。

8.退出了循环(「q」键或视频流结束),我们还要处理以下。

# stop the timer and display FPS information

fps.stop()

print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))

print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

# do a bit of cleanup

cv2.destroyAllWindows()

vs.stop()

运行文件目录有以下文件:

到文件相应的目录下:cd D:\目标检测\object-detection执行命令:python real_time_object_detection.py --prototxt MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel

演示

这里我把演示视频上传到了B站,地址链接目标检测

补充

项目github地址object_detection链接。

本项目要用到MobileNetSSD_deploy.prototxt.txt和MobileNetSSD_deploy.caffemodel,可以去github上下载项目运行。

猜您喜欢

运用JMeter停止压力测试(根底篇)1.启动Jmeter双击图中所示文件呈现如下图所示即启动胜利2 新建..

导言:本文引见了一个在空间和尺度上全活泼特征交互(fully active feature interact..

网友评论

Copyright 2020 www.SoftYun.net 【软件云】 版权所有 软件发布

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 点此查看联系方式

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值