I was trying object detection for the first time and got this error. I read various posts and saw many videos but couldn't find any solution to it. Can some please help me to solve this error.
- I have only installed open cv using pip command.
- All the training and testing was done on google colab.
- I downloaded the yolov3_training_last.weights yolov3_testing.cfg from drive and pasted in the same folder in which code was present.
- I ran the code on atom and installed script as its package.
Error -
Traceback (most recent call last): File "D:\test\New folder\Object_Detection.py", line 5, in 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: error: (-212:Parsing error) Failed to parse NetParameter file: yolov3_testing.cfg in function 'cv::dnn::dnn4_v20200609::readNetFromDar
Code -
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()