Loading YOLO: invalid index to scalar variable
Asked Answered
G

2

9

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?

Gauguin answered 4/11, 2021 at 3:47 Comment(1)
Please post the full traceback error.Tica
C
14

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.

Caulis answered 8/11, 2021 at 9:14 Comment(0)
A
7

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.

Asel answered 18/6, 2022 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.