iArunava / YOLOv3-Object-Detection-with-OpenCV

This project implements a real-time image and video object detection classifier using pretrained yolov3 models.
MIT License
320 stars 173 forks source link

When I input a picture,it will push me an error,But if I input a video,it will work #3

Closed AnnaRose0226 closed 5 years ago

AnnaRose0226 commented 5 years ago

Error message: TypeError:Expected cv::UMat for argument 'img',I cann't find any resolvent,Can you help me.Thanks

iArunava commented 5 years ago

Try casting it with, cv2.UMat() and check if it works. This shouldn't happen, what is the image format?

Zyphre commented 5 years ago

Was having the same issue, tried casting as you suggested:

def show_image(img):
#     img = cv.UMat(img)
    cv.imshow('Image', img)
    cv.waitKey(0)

TypeError: Required argument 'ranges' (pos 2) not found

I'm reading the documentation on UMat() but I'm unsure if that's actually the culprit.

Strangely, I'm able to get it working In testing I merged the yolo_utils.py with yolo.py, and hardcoded the default flags to point to the paths.

I commented out the call to show_image()

        finally:
            img = infer_image(net, layer_names, height, width, img, colors, labels, FLAGS)
#           show_image(img)

Then added these lines to the end of of the script, using your existing functions.

img = cv2.imread(r"C:\Users\Username\Desktop\testImage.jpg")

#rewrite with fieldnames
blob = cv2.dnn.blobFromImage(image=img, scalefactor=1.0/255.0, size=(416,416), mean=None, swapRB=True, crop=False)

net.setInput(blob)

outs = net.forward(layer_names)

boxes, confidences, classids = generate_boxes_confidences_classids(outs,height,width,FLAGS.confidence)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, FLAGS.confidence, FLAGS.threshold)

#changed name from img to newimg for testing
newimg = draw_labels_and_boxes(img,boxes,confidences,classids,idxs,colors,labels)

cv2.imshow('img',newimg)

# save output just in case
processedImage = r"C:\Users\Username\Desktop\output.png"
cv2.imwrite(processedImage,newimg)
cv2.waitKey(10000)
cv2.destroyAllWindows()

Hopefully this is somewhat helpful to narrow down why it's behaving this way, really appreciate the work you've already done here!

iArunava commented 5 years ago

6 fixes this. Thanks @raysworld