python - IndexError:索引 14708 超出轴 0 的范围,大小为 295
问题描述
我正在尝试用 yolo 制作对象检测软件,但这个错误正在弹出,我很迷茫,请任何人帮助我!(代码不完整,如果这篇文章有任何错误,请见谅,因为我是新的 Stackoverflow)。教程来自这里
Traceback (most recent call last):
File "d:/opencv/objdetect_yolo.py", line 66, in <module>
findobj(output,img)
File "d:/opencv/objdetect_yolo.py", line 33, in findobj
cofidence = scores[classId]
IndexError: index 14708 is out of bounds for axis 0 with size 295
IndexError:索引 14708 超出轴 0 的范围,大小为 295
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
whT = 320
classespath = 'coco.names.txt'
classes = []
with open(classespath,'rt')as f:
classes = f.read().rstrip('\n').split('\n')
#print (classes)
#print(len(classes))
modelConfiguration = 'yolov3.cfg'
modelWeights = 'yolov3.weights'
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
def findobj(outputs,img):
hT, wT , cT = img.shape
bbox = []
classIds = []
confs = []
for output in outputs:
for det in outputs:
scores = det[5:]
classId = np.argmax(scores)
cofidence = scores[classId]
if float(0.5) < cofidence:
w,h = int(det[2]*wT),int(det[3]*hT)
x,y = int((det[0]*wT) - w/2), int((det[1]*hT) - h/2)
bbox.append([x,y,w,h])
classIds.append(classId)
confs.append(float(cofidence))
while True:
succes, img = cap.read()
blob = cv2.dnn.blobFromImage(img,1/255,(whT,whT),[0,0,0],1,crop=False)
net.setInput(blob)
layerNames = net.getLayerNames()
#print(layerNames)
outputNames = [layerNames[i[0]-1]for i in net.getUnconnectedOutLayers() ]
#print(outputNames)
#print(net.getUnconnectedOutLayers())
output = net.forward(outputNames)
findobj(output,img)
cv2.imshow("objdetect",img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
解决方案
您似乎遇到了问题,因为np.argmax
会给您最大元素的原始数字而不是索引。因此,如果您有一个 3x3 矩阵,则 argmax 函数会将矩阵视为 9x1 线而不是 3x3 正方形。
# The matrix:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
#will be treated as:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
该文档建议采用以下解决方案:
classId = np.unravel_index(np.argmax(scores, axis=None), scores.shape)
推荐阅读
- wordpress - 具有多个子块(例如列或选项卡)的开发块
- sql - PostgreSQL 中每个客户的最大计数
- javascript - 图像块 - 上传、调整大小和发布
- python - 状态空间模型中用户提供的初始状态
- huawei-mobile-services - HMS Nearby Service-BeaconManager app为什么报502或401状态码?
- python - 将非透明图像转换为透明 GIF 图像 PIL
- pandas - Pandas Stacked Bar 和绘图问题
- python - 附加深拷贝
- python - 使用python函数(def)返回所需的行/列
- tensorflow - 为什么 Keras 接受 model.evaluate 的批量大小选项?