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.
Answers:
Maybe yolo3-coco cfg and weight do not match so Unknown layer type error.
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,
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")
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"
try to use OpenCV --version 5
. My error was solved with this.
ı 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.
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.
Maybe yolo3-coco cfg and weight do not match so Unknown layer type error.
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,
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")
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"
try to use OpenCV --version 5
. My error was solved with this.
ı 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.