我有一个简单的对象检测错误。
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
IndexError: invalid index to scalar variable.
import cv2.cv2 as cv
import numpy as np
# Load Yolo
net = cv.dnn.readNet('yolov3.weights','yolov3.cfg')
classes = []
with open ("coco.names","r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
otputlayers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
#Loading image
img = cv.imread("room_ser.jpg")
cv.imshow("Image",img)
cv.waitKey(0)
cv.destroyAllWindows()
getUnconnectedOutLayers()
返回一个整数,而不是一个可迭代对象。相反,使用
outputlayers = [layer_names[i-1] for i in net.getUnconnectedOutLayers()]
这里的例子是不正确的。关于该方法的更多信息可以在cv2文档中找到。
错误本身(IndexError
)告诉您,您正在尝试索引某个标量。
这个索引问题,因为它不能迭代
unconnected_indices = neural_network.getUnconnectedOutLayers()
output_names = [layer_names[index - 1] for index in unconnected_indice
bounding_box = all_bounding_boxes[index]
if class_ids[index] == 0:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
class_with_confidence = 'PERSON' + str(int(confidence_values[index] * 100)) + '%'
cv2.putText(img, class_with_confidence, (x, y-10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, (255, 0, 0), 1)