opencv 用于对象检测的标量变量索引无效

k5hmc34c  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(172)

我开始工作的OpenCV和做一些基本的例子。我试图计数的对象从图像。例如,你可以看到非常基本和流行的样本在下面..

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()

在此之前没有任何问题,但是当我试图画框架时,我得到了一个错误。

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()

运行此脚本时出现“IndexError”。错误:

---------------------------------------------------------------------------
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.

你觉得可能是什么问题?顺便说一句,我的版本是;

  • Python:3.8.8(压缩语言环境)
  • openCV:4.0.1(但当使用cv 2.版本从conda promt进行检查时为4.5.5。)
  • 库文件:0.2.6
4smxwvx5

4smxwvx51#

图像的特定位置可能会产生该错误。我的意思是图像找不到。所以,尝试给予图像的特定位置+图像名称及其确切的扩展名。例如:“cars.jpg”是从“cars.jpg”不同。另外,该图像可能无效或损坏。尝试单独检查图像。我尝试了您提供的代码,它为我工作。

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()

相关问题