【问题标题】:how to do live camera object detection in python如何在python中进行实时相机对象检测
【发布时间】:2020-10-27 05:13:43
【问题描述】:

我有代码:

import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im = cv2.imread(r'C:\Users\james\OneDrive\Desktop\logos\normal.png')
bbox, label, conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()

它检测到图片中的大多数对象,但我希望能够从实时视频中制作它,我尝试使用 cap = cv2.VideoCapture(0) 但我无法让它工作。

【问题讨论】:

  • 您必须提供更多关于它不工作的方式的信息。它会抛出错误吗?你得到一个完全黑色的图像吗?你试过视频文件吗?你的设置是什么?什么硬件?
  • 嗨,我应该更具体地说明这个问题,我希望它是现场直播的,但我想如果我真的需要的话,我可以使用视频文件,这个代码上限 = cv2。 VideoCapture(0) while(True): ret, frame = cap.read() ## im = cv2.imread(r'C:\Users\james\OneDrive\Desktop\logos\imagetodetect.jpg') bbox, label, conf = cv.detect_common_objects(frame) output_image = draw_bbox(frame, bbox, label, conf) plt.imshow(output_image) plt.show() plt.clf() 当我运行它时它只显示一帧然后结束程序, 谢谢。我正在使用 3.5.0 python
  • srry 格式化无法正确格式化
  • drive.google.com/file/d/1dk9lP5iHGb0kzOK79ofmQuJDZ5HqvubN/… 驱动器中有文件可以查看,因此格式正确
  • 感谢您的澄清。您能否将您的第一条评论放在后代问题中?

标签: python image matplotlib detection cv2


【解决方案1】:

典型的程序会执行每一行,直到完成,然后退出。您的代码要求它打开一个流并显示一个框架。然后,因为它已经完成了你要求它做的所有事情,所以它退出了。

在您在 cmets 中提供的代码中,您在 .show() 之后立即使用 matplotlib 的 .clf(),这将清除该数字并且可能是您的问题的原因。你应该改用cv2.imshow()

如果您希望它保持打开状态,您需要将显示代码放入while True 循环中,如getting started part of the docs 所示。将他们的代码与你的结合起来,你会得到类似的东西(但我没有测试过):

import numpy as np
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Detect objects and draw on screen
    bbox, label, conf = cv.detect_common_objects(frame)
    output_image = draw_bbox(im, bbox, label, conf)

    cv2.imshow('output',output_image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

对于网络摄像头,这将一直持续到您按 q 中断程序。

请注意,这可能不会顺利运行,因为对象检测模型通常在计算上相当昂贵。但这是另一个问题的挑战。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多