opencv的yolo报错:IndexError: invalid index to scalar variable

Posted zhangphil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv的yolo报错:IndexError: invalid index to scalar variable相关的知识,希望对你有一定的参考价值。

opencv的yolo报错:IndexError: invalid index to scalar variable,Python

几年前写的老代码工程:

https://zhangphil.blog.csdn.net/article/details/102960822https://zhangphil.blog.csdn.net/article/details/102960822

 

重新运行后报错:

Traceback (most recent call last):
  File "D:\\code\\python\\yolo\\main.py", line 45, in <module>
    outputs = net.forward(get_output_layers(net))
                          ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\\code\\python\\yolo\\main.py", line 9, in get_output_layers
    output_layers = [layer_names[arr[0] - 1] for arr in net.getUnconnectedOutLayers()]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\\code\\python\\yolo\\main.py", line 9, in <listcomp>
    output_layers = [layer_names[arr[0] - 1] for arr in net.getUnconnectedOutLayers()]
                                 ~~~^^^
IndexError: invalid index to scalar variable.

 

原因是opencv版本升级。解决方案,把第9行代码:

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

 

修改成:

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

然后重新运行成功。

 

以上是关于opencv的yolo报错:IndexError: invalid index to scalar variable的主要内容,如果未能解决你的问题,请参考以下文章

配置YOLO2(ubuntu16.04+cuda8.0+opencv3.1.0)

OpenCV中使用YOLO对象检测

opencv学习-OpenCV与YOLO之间的关系

OpenCV DNN之YOLO实时对象检测

Opencv人数统计 yolo kcf人头跟踪 人数统计 KCF目标跟踪 YOLO目标跟踪

物体检测实战:使用 OpenCV 进行 YOLO 对象检测