4

I am trying to follow a simple object detection tutorial using cvlib, but i am consistently running into an IndexError and cannot find anyone else struggling with this issue. Below is the full code i basically copy and pasted from tutorials and the output. Any help is appreciated!

Code:

import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox, detect_common_objects
from numpy.lib.polynomial import poly

input = cv2.imread('cars1.jpg')

image = cv2.cvtColor(input,cv2.COLOR_BGR2RGB)

plt.axis('off')

box, label, count = cv.detect_common_objects(image)

output = draw_bbox(image, box, label, count)

plt.imshow(output)

plt.show()

Output

Traceback (most recent call last):   
File "/Users/zechariahtay/Desktop/ttt.py", 
line 19, in <module>
    box, label, count = cv.detect_common_objects(image)   
File "/usr/local/lib/python3.9/site-packages/cvlib/object_detection.py",
line 135, in detect_common_objects
    outs = net.forward(get_output_layers(net))   
File "/usr/local/lib/python3.9/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
"/usr/local/lib/python3.9/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
4
  • what tutorial? Always add URL in question (not in comment). Commented Oct 26, 2021 at 10:16
  • maybe you shouldn't convert to RGB if you use CV functions - they all may need BGR. You need to convert to RGB when you want to display it with plt Commented Oct 26, 2021 at 10:19
  • OR maybe you should use older Python 3.8 because some modules may not be ready to work with Python 3.9 Commented Oct 26, 2021 at 10:24
  • code works for me on Python 3.8, Linux Mint 20. I can't test it on 3.9 because I would have to compile tensorflow because I have old CPU and standard tensorflow can't work. Commented Oct 26, 2021 at 11:20

2 Answers 2

4

If you are using OpenCV > 4.5.3 it may be related to this bug introduced in v4.5.4. I had the same error and it was fixed when I downgraded (pip3 install opencv-python==4.5.3.56).

Sign up to request clarification or add additional context in comments.

Comments

0

There is no need to downgrade your OpenCV version. The later versions return a 1D array containing the indices of each layer. Just avoid indexing ([0]) as shown in the following:

output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.