【问题标题】:Why am I getting this error for YOLO webcam object detection in python?为什么在 python 中进行 YOLO 网络摄像头对象检测时会出现此错误?
【发布时间】:2020-10-08 10:42:56
【问题描述】:

我正在尝试使用 OpenCV 和 Yolov3 在 python 中进行对象检测。但由于某种原因,我收到了这个错误:

cv2.error: OpenCV(4.3.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6cwppm05\opencv\modules\dnn\src\darknet\darknet_io.cpp:601: error: (-215:Assertion failed) separator_index < line.size() in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'

这是代码:

import cv2
import numpy as np
net = cv2.dnn.readNet('yolov3.cfg', 'yolov3.weights')
classes = []
with open('coco.names', 'r') as f:
    classes = f.read().splitlines()
cap = cv2.VideoCapture(0)
while True:
    _, img = cap.read()
    height, width, _ = img.shape
    blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0, 0, 0), swapRB=True, crop=False)
    net.setInput(blob)
    output_layers_names = net.getUnconnectedOutLayersNames()
    layerOutputs = net.forward(output_layers_names)
    boxes = []
    confidence = []
    class_ids = []
    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            if confidence > 0.5:
                center_x = int(detection[0]*width)
                center_y = int(detection[1]*height)
                w = int(detection[2]*width)
                h = int(detection[3]*height)
                x = int(center_x - w/2)
                y = int(center_y - h/2)
                boxes.append([x, y, w, h])
                confidence.append((float(confidence)))
                class_ids.append(class_id)
    indexes = cv2.dnn.NMSBoxes(boxes, confidence, 0.5, 0.4)
    font = cv2.FONT_HERSHEY_PLAIN
    colors = np.random.uniform(0, 255, size=(len(boxes), 3))
    if len(indexes)>0:
        for i in indexes.flatten():
            x,y,w,h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidence[i], 2))
            color = colors[i]
            cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
            cv2.putText(img, label + '' + confidence, (x, y+20), font, 2, (255, 255, 255), 2)
    cv2.imshow("Video", img)
    if cv2.waitKey(0) & 0xFF == ord('q'):
        break

我是 OpenCV 和 Python 的新手,有人可以帮帮我吗?

【问题讨论】:

    标签: python opencv yolo


    【解决方案1】:

    根据您遇到的错误,我建议您在第 3 行尝试:

    net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
    

    并确保 .cfg 和 .weights 文件位于您运行 Python 脚本的同一目录中。

    Check the docs

    还要确保您的 cfg 文件是正确的。 This 应该是这个样子/

    【讨论】:

    • 老兄.. 非常感谢您提供实际的 cfg 文件。我真的不知道该怎么感谢你。相机会打开,但不会自行更新。它不像一个真正的摄像机。它的滞后性如此之大,以至于它甚至无法移动。你知道如何解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-09
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多