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

cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\dnn\src\darknet\darknet_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.



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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")

Solution 4:[4]

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"

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 B200011011
Solution 2 kaankucuk
Solution 3 Mohit Verma
Solution 4 RAJESH CHANDRAS
Solution 5 Leonardo Alves Machado