在Python中进行实时摄像头物体检测时出错

0 投票
1 回答
63 浏览
提问于 2025-04-14 17:14

我有以下代码:

import cv2
import cvlib as cv
import cvlib.object_detection
from gtts import gTTS
from playsound import playsound


video = cv2.VideoCapture(0)

while (True):
    retu, frame0=video.read()
    bbox, label, conf = cv.detect_common_objects(frame0)
    output_image= cvlib.object_detection.draw_bbox(frame0, bbox, label, conf)

    cv2.imshow('Cam', output_image)
    if cv2.waitKey(1) & 0xFF==ord('f'):
        break
video.release()
cv2.destroyAllWindows()

但是我一直遇到这个错误:

Traceback (most recent call last):
  File "C:\Users\wptassembly\PycharmProjects\imageLibrary\.venv\cam.py", line 36, in <module>
    bbox, label, conf = cv.detect_common_objects(frame)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wptassembly\PycharmProjects\imageLibrary\.venv\Lib\site-packages\cvlib\object_detection.py", line 125, in detect_common_objects
    net = cv2.dnn.readNet(weights_file_abs_path, config_file_abs_path)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\darknet\darknet_io.cpp:705: error: (-215:Assertion failed) separator_index < line.size() in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'

这段代码是用来从我的摄像头获取图像,检测物体,然后在物体周围画一个框;可是我总是收到这个错误。

1 个回答

-1

这段代码是用来从我的摄像头获取图像,检测物体,然后在物体周围画一个框;但是我总是遇到错误。

你需要对 labelconf 进行循环处理。然后再画框。

代码片段:

while (True):
    retu, frame0=video.read()
    bbox, label, conf = cv.detect_common_objects(frame0)
    output_image= cvlib.object_detection.draw_bbox(frame0, bbox, label, conf)

    for l, c in zip(label, conf):
        print(f"Detected object: {l} with confidence level of {c}\n")
     
    output_image = draw_bbox(img, bbox, label, conf)

    cv2.imshow('Cam', output_image)
    if cv2.waitKey(1) & 0xFF==ord('f'):
        break
video.release()
cv2.destroyAllWindows()

撰写回答