我的代码:
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'
代码是从我的网络摄像头获取图像,检测对象,然后在它们周围画一个框;但是我总是遇到错误。
The code is to get images from my webcam, detect objects, and then draw a box around them; however I keep getting thrown the Error.
您需要循环
label
和 conf
。然后画一个盒子,
片段:
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()