2

I started to work on OpenCV and doing some basic examples. I'm trying to count the objects from image. For example you can see very basic and popular sample in below..

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

img = cv2.imread('cars.jpg')
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.axis('off')
plt.imshow(img1)
plt.show()

There isn't any problem untill this, but when I try to draw the frames, I'm getting a fault. This code is;

box, label, count = cv.detect_common_objects(img)
output = draw_bbox(img, box, label, count)

output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10,10))
plt.axis('off')
plt.imshow(output)
plt.show()

It appears a "IndexError" when it run this script.. Error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-14-df69657efc75> in <module>
----> 1 box, label, count = cv.detect_common_objects(img)
      2 output = draw_bbox(img, box, label, count)
      3 
      4 output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
      5 plt.figure(figsize=(10,10))

~\Anaconda3\lib\site-packages\cvlib\object_detection.py in detect_common_objects(image, confidence, nms_thresh, model, enable_gpu)
    133     net.setInput(blob)
    134 
--> 135     outs = net.forward(get_output_layers(net))
    136 
    137     class_ids = []

~\Anaconda3\lib\site-packages\cvlib\object_detection.py in get_output_layers(net)
     27     layer_names = net.getLayerNames()
     28 
---> 29     output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
     30 
     31     return output_layers

~\Anaconda3\lib\site-packages\cvlib\object_detection.py in <listcomp>(.0)
     27     layer_names = net.getLayerNames()
     28 
---> 29     output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
     30 
     31     return output_layers

IndexError: invalid index to scalar variable.

What do you think could be the problem? by the way, my versions are;

  • Python: 3.8.8 (conda env)
  • openCV: 4.0.1 (but when check from conda promt with cv2.version is 4.5.5.)
  • cvlib: 0.2.6
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Ernu
  • 43
  • 5
  • related: https://stackoverflow.com/questions/69756781/hi-i-have-error-related-to-object-detection-project (imo a bug in cvlib) – berak Jan 17 '22 at 13:41

1 Answers1

0

The specific location of the image may create that error. I mean the image cant be found. So, try to give the specific location of the image + the image name with its exact extension. ex: "cars.jpg" is different from "cars.jpeg". Also, the image may be invalid or corrupted. try to check the image separately. I tried the code you provided and it worked for me.

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

img = cv2.imread('/specific location/cars.jpg')
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.axis('off')
plt.imshow(img1)
plt.show()
Sami
  • 1
  • 1