arunponnusamy / cvlib

A simple, high level, easy to use, open source Computer Vision library for Python.
http://arunponnusamy.com/cvlib/
MIT License
656 stars 126 forks source link

Unable to execute object_detection #46

Open tomgit-com opened 2 years ago

tomgit-com commented 2 years ago

As of rather recently I am unable to use object_detection, whereas everything else seems to work rather fine. It boils down to that error message:

Traceback (most recent call last):#################################################################################### |
  File "detector_mine.py", line 35, in <module>
    bbox, label, conf = cv.detect_common_objects(rect_img, confidence=0.75, enable_gpu=True')
  File "/home/user/.local/lib/python3.8/site-packages/cvlib/object_detection.py", line 135, in detect_common_objects
    outs = net.forward(get_output_layers(net))
  File "/home/user/.local/lib/python3.8/site-packages/cvlib/object_detection.py", line 29, in get_output_layers
    output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
  File "/home/user/.local/lib/python3.8/site-packages/cvlib/object_detection.py", line 29, in <listcomp>
    output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
IndexError: invalid index to scalar variable.

It runs fine on my old Win10/11 installation from a couple of months ago, however accessing the same script from a WSL machine on the exact same PC results in the error above. I'm also getting the same from a new macOS setup.

claudia-hm commented 2 years ago

Hello, I am using ubuntu and I run to the same error

tomgit-com commented 2 years ago

I have found a temporary workaround.

If you change in line 29 output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

to (removing the typechange from list to value) output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

in the object_detection.py script (you'll find the location in the error message) it should work again.

Small edit: I've forked the library and added the fix there. Feel free to add it to the main project, I'll remove the fork later =)

mihaipanait commented 2 years ago

Besides the change mentioned by @tomgit-com I also had to comment out i = i[0] on line 245: https://github.com/arunponnusamy/cvlib/blob/52a31866f17270d55079b0ab7d56c44c601c79a1/cvlib/object_detection.py#L245

    for i in indices:
        #i = i[0]
        box = boxes[i]
tomgit-com commented 2 years ago

I've also noticed that issues, but it happened rather lately. I don't know if there've been some changes on the pretrained models

arunponnusamy commented 2 years ago

I think there are some changes in the latest OpenCV version. Try using with older opencv version for now. Will fix this soon.

motskin commented 2 years ago

with openCV 4.5.3.56 works, But with 4.5.4.58, 4.5.4.60(latest) doesn't work.

Greg-Kojadinovich commented 2 years ago

Hello, this issue still exists, I ran into it today. I followed tomgit-com and mihaipanait's comments and it worked. Thank you for the direction.