'cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'forward'

Can somoeone tell me how to fix this error: cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'forward'

Overload resolution failed:

  • Can't convert object of type 'numpy.ndarray' to 'str' for 'outputName'
  • Can't parse 'outputBlobs'. Sequence item with index 0 has a wrong type
  • Can't parse 'outputBlobs'. Sequence item with index 0 has a wrong type
  • Can't parse 'outBlobNames'. Sequence item with index 0 has a wrong type
  • Can't parse 'outBlobNames'. Sequence item with index 0 has a wrong type
import cv2
import numpy as np

net = cv2.dnn.readNet('yolov3.weights','yolov3.cfg')  

#wyciągamy nazwy z coco file
classes = []
with open('coco.names','r') as f:
    classes = f.read().splitlines()

#ładowanie zdjęcia
img = cv2.imread('kite.jpg')
#img = cv2.resize(img, None, fx=0.7, fy=0.7)
height, width, _ = img.shape

blob = cv2.dnn.blobFromImage(img, 1/255, (416,416), (0,0,0), swapRB=True, crop=False) 
net.setInput(blob)  #set the input from the blob to the network

output_layers_names = net.getUnconnectedOutLayers() #Get the index of the output layers.
layerOutputs = net.forward(output_layers_names) #HERE IS THE ERROR !!!

boxes = []
confidences = []
class_ids = []

#first loop is used to extract all the informations from the layers output
#second loop is used to extract the information from each of the outputs
for output in layerOutputs:
    for detection in output:
        scores = detection[5:] #from 6 elemenets to the end
        class_ids = np.argmax(scores) #classes prediction
        class_ids =np.argmax(scores) #location of higer scores
        confidences= scores[class_ids] 
        if confidences > 0.5: 
            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(confidences)))
            class_ids.append(class_ids)

print(len(boxes))

cv2.imshow('kite.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Solution 1:[1]

Hey I's having the same error but then I uninstall opencv 4.5 and install 4.1.2. I recommend you to do the same

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 A.H Revel