我遇到与简单对象检测相关的错误。
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
IndexError:标量变量的索引无效。
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] 用于 unconnected_indice 中的索引
bounding_box = all_bounding_boxes[索引]
如果 class_ids[索引] == 0: cv2.矩形(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)