IndexError: list index out of range (Object Detection)
IndexError: list index out of range (Object Detection)
import cv2
thres = 0.45
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)
classNames= []
classFile = 'coco.names'
with open(classFile,'rt') as f:
classNames = f.read().rstrip('n').split('n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success,img = cap.read()
classIds, confs, bbox = net.detect(img,confThreshold=thres)
print(classIds,bbox)
if len(classIds) != 0:
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.imshow("Output",img)
cv2.waitKey(1)
错误信息:
line 31, in <module>
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
IndexError: list index out of range
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
为什么这个列表索引超出范围,我该如何解决?
'coco.names' , 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' 和 'frozen_inference_graph.pb' 包括在以下 link 中:
https://mega.nz/file/UIlQ1L7Y#ePLIQMSlhwo6ab46f1GgYli9nNw6EFIE6mb33-GZnHs
您的 classNames
构建不正确,这就是您 index 超出范围 .
的原因
例如,您得到的 class ID 为 47(这是一个杯子),但是 classNames 中没有条目 46(classid -1)。
所以修复方法是:
with open(classFile,'rt') as f:
classNames=[line.rstrip() for line in f]
下面是上述修复前后的演示:
import cv2
thres = 0.45
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)
classNames= []
classFile = 'coco.names'
with open(classFile,'rt') as f:
classNames = f.read().rstrip('n').split('n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success,img = cap.read()
classIds, confs, bbox = net.detect(img,confThreshold=thres)
print(classIds,bbox)
if len(classIds) != 0:
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.imshow("Output",img)
cv2.waitKey(1)
错误信息:
line 31, in <module>
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
IndexError: list index out of range
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
为什么这个列表索引超出范围,我该如何解决?
'coco.names' , 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' 和 'frozen_inference_graph.pb' 包括在以下 link 中: https://mega.nz/file/UIlQ1L7Y#ePLIQMSlhwo6ab46f1GgYli9nNw6EFIE6mb33-GZnHs
您的 classNames
构建不正确,这就是您 index 超出范围 .
例如,您得到的 class ID 为 47(这是一个杯子),但是 classNames 中没有条目 46(classid -1)。
所以修复方法是:
with open(classFile,'rt') as f:
classNames=[line.rstrip() for line in f]
下面是上述修复前后的演示: