C1W1 Lab Fix - IndexError: invalid index to scalar variable

I found a problem in the code that runs in server.ipynb where cvlib reports the following error from its internal code.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-ce82369bd1e4> in <module>
      1 for image_file in image_files:
----> 2     detect_and_draw_box(image_file)

<ipython-input-5-20bb683bd852> in detect_and_draw_box(filename, model, confidence)
     20 
     21     # Perform the object detection
---> 22     bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)
     23 
     24     # Print current image's filename

~/miniconda3/envs/mlep-w1-lab/lib/python3.7/site-packages/cvlib/object_detection.py in detect_common_objects(image, confidence, nms_thresh, model, enable_gpu)
    103     net.setInput(blob)
    104 
--> 105     outs = net.forward(get_output_layers(net))
    106 
    107     class_ids = []

~/miniconda3/envs/mlep-w1-lab/lib/python3.7/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

~/miniconda3/envs/mlep-w1-lab/lib/python3.7/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.

Rolling back to an older version of cvlib fixes the problem.

python3 -m pip install opencv-python-headless-4.3.0.38

2 Likes

Installing the version 0.2.5 of the cvlib package in jupyter notebook,
!pip install cvlib==0.2.5

1 Like

Thanks, it worked :slight_smile:

I had the same issue and this works, but I did also have to restart my kernel (using jupyter in vscode)…

pip install cvlib==0.2.5