8

Getting an error for IndexError: invalid index to scalar variable on the yolo_layers line.

network = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
layers = network.getLayerNames()
yolo_layers = [layers[i[0] - 1] for i in network.getUnconnectedOutLayers()]

This code won't work on my Jupyter notebook but will run fine on google collab. No idea why. Could be my python version?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
abe
  • 103
  • 1
  • 4

2 Answers2

13

It's may caused by the different versions of cv2. The version of cv2 module with CUDA support will give you a 2-D array when calling network.getUnconnectedOutLayers(). However, the version without CUDA support will give a 1-D array.

You may try to take the brackets out which closing the index 0.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Kent
  • 154
  • 1
  • 5
6

For CPU versions:

network = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
layers = network.getLayerNames()
yolo_layers = [layers[i - 1] for i in network.getUnconnectedOutLayers()]

returns: ['yolo_82', 'yolo_94', 'yolo_106']

Tested on OpenCV version 4.5.5.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87