【问题标题】:(-212:Parsing error) in Object Detection using yolo, google colab and opencv(-212:Parsing error) 使用 yolo、google colab 和 opencv 进行对象检测
【发布时间】:2020-12-16 23:46:03
【问题描述】:

我第一次尝试对象检测并收到此错误。我阅读了各种帖子并观看了许多视频,但找不到任何解决方案。有人可以帮我解决这个错误吗?

  • 我只使用 pip 命令安装了 open cv。
  • 所有的训练和测试都是在 google colab 上完成的。
  • 我从以下位置下载了 yolov3_training_last.weights yolov3_testing.cfg 驱动器并粘贴到存在代码的同一文件夹中。
  • 我在 atom 上运行代码并将脚本作为其包安装。

错误 -

Traceback(最近一次调用最后一次): 文件“D:\test\New folder\Object_Detection.py”,第 5 行,在 net = cv2.dnn.readNet('yolov3_training_last.weights', 'yolov3_testing.cfg') cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\opencv\modules\dnn\src\darknet\darknet_importer.cpp:207: 错误: (-212:Parsing error) 无法解析 NetParameter 文件: yolov3_testing.cfg in function 'cv::dnn::dnn4_v20200609::readNetFromDar

代码 -

import cv2
import numpy as np

#net = cv2.dnn.readNet('D:\\test\\New folder\\yolov3_training_last.weights', 'D:\\test\\New folder\\yolov3_testing.cfg')
net = cv2.dnn.readNet('yolov3_training_last.weights', 'yolov3_testing.cfg')

classes = []
#with open("D:\\test\\New folder\\classes.txt", "r") as f:
with open("classes.txt", "r") as f:
    classes = f.read().splitlines()

#cap = cv2.VideoCapture('D:\\test\\New folder\\test1.mp4')
cap = cv2.VideoCapture('test1.mp4')
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(100, 3))

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 = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.2:
                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])
                confidences.append((float(confidence)))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.4)

    if len(indexes)>0:
        for i in indexes.flatten():
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidences[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('Image', img)
    key = cv2.waitKey(1)
    if key==27:
        break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv google-colaboratory object-detection yolo


    【解决方案1】:

    yolov3 的 cfg 文件有问题,导致无法解析它以收集可行的信息。

    尝试再次运行代码并适当地保存模型。在 colab 中再次运行保存的权重并检查它是否有效。如果它在那里工作,它也应该在原子中。另外,你确定这些是函数 cv2.dnn.readNet() 的正确参数吗?

    【讨论】:

      【解决方案2】:

      试试这样:

      net=cv2.dnn.readNetFromDarknet("yolov3_custom.cfg",r"Downloads\yolov3_custom_6000.weights")
      

      并确保在您的本地系统中安装了暗网。

      【讨论】:

        猜你喜欢
        • 2020-10-01
        • 2020-02-11
        • 1970-01-01
        • 2016-03-24
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-16
        相关资源
        最近更新 更多