我开始研究 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(conda 环境)
- openCV:4.0.1(但是当使用 cv2 从 conda promt 检查时。版本是 4.5.5。)
- 简历库:0.2.6