Open-Cv dnn error for python while using Yolov3. Using open-cv ver(4.2.0)

Question:

cv2.error: OpenCV(4.2.0) C:projectsopencv-pythonopencvmodulesdnnsrcdarknetdarknet_io.cpp:677: error: (-212:Parsing error) Unknown layer type: in function ‘cv::dnn::darknet::ReadDarknetFromCfgStream

Code:


import cv2
import numpy as np

# Load Yolo
path =r"D:yolov3-coco"
weight = path+r"yolov3.weights"
cfg = path+r"yolov3.cfg"
net = cv2.dnn.readNetFromDarknet(weight ,cfg )
classes = []
with open(path+"coco.txt", "r") as f:
    classes = [line.strip() for line in f.readlines()]
print(cfg)
print(weight)
print(classes)



cv2.destroyAllWindows()

I have already used the command net= cv2.dnn.readNet(weights,cfg) but it did not work i have also gone to https://pjreddie.com/media/files/yolov3.weights and downloaded the weights and config files and have put them in a folder called yolov3-coco.

Asked By: Dzeko

||

Answers:

Maybe yolo3-coco cfg and weight do not match so Unknown layer type error.

Answered By: kaankucuk

You seem to be passing *.weights first and *.cfg later. If takes *.cfg first, then the darknet weights file. Reference for readNetFromDarknet,

https://docs.opencv.org/master/d6/d0f/group__dnn.html#gafde362956af949cce087f3f25c6aff0d

This problem is similar to,

YOLO V3 Video Stream Object Detection

Answered By: B200011011

Path for yolov3.weights,yolov3.cfg is not proper so you can pass path directly where they are stored

net = cv.dnn.readNetFromDarknet("yolov3-coco/yolov3.cfg", "yolov3-coco/yolov3.weights")

Answered By: Mohit Verma

For me it were the wrong files. I was trying it in google colab with version 2.5.0 of tensorflow. The following files worked:

!wget "https://pjreddie.com/media/files/yolov3.weights"

!wget "https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg"

!wget "https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names"

Answered By: RAJESH CHANDRAS

try to use OpenCV --version 5. My error was solved with this.

Answered By: Mubahsir

ı had trouble problem like this. Just check your code path and make sure where cfg,weights are,open main folder and work under the main folder.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.