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.

Bezal-el
  • 33
  • 1
  • 2
  • 8

6 Answers6

2

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"

  • This worked for me. Looks like the cfg and weights can be incompatible with each other if downloaded from different projects. Thanks Rajesh! – Piyush Shandilya Aug 18 '23 at 11:03
0

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

kaankucuk
  • 19
  • 6
  • I downloaded it from a prescribe link and i checked it , can you prescribe an links that you may know of – Bezal-el May 23 '20 at 14:48
0

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

B200011011
  • 3,798
  • 22
  • 33
  • net = cv2.dnn.readNetFromDarknet(cfg ,weight ) 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::ReadDarknetFromCfgStreamI get same error – Bezal-el May 23 '20 at 14:42
  • Changing to `net = cv2.dnn.readNetFromDarknet(cfg, weight)` and file path works on my machine. I have tested with https://pjreddie.com/darknet/yolo/, yolov3 weights and config without any error on OpenCV 4.3.0. – B200011011 May 23 '20 at 16:10
  • This change did not seem to have effect on my computer i think my version of opencv may be incompatible or the library may be incomplete . I made sure to download the latest version of open-cv but i see no avail. So i have used tensorflow lite interpreter for the yolov3 instead – Bezal-el Aug 02 '20 at 16:36
0

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

Mohit Verma
  • 56
  • 1
  • 6
0

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

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Mubahsir
  • 45
  • 9
0

ı 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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '22 at 05:41