【问题标题】:Image Classifire feature detector图像分类器特征检测器
【发布时间】:2021-06-01 11:59:26
【问题描述】:

我有一个代码,我可以从这个链接https://www.youtube.com/watch?v=nnH55-zD38I&t=1047s 但我收到一条错误消息:

line 62, in <module>
    cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)
IndexError: list index out of range
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-m8us58q4\opencv\modules\videoio\src\cap_msmf.cpp 
(438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

代码如下:

import cv2
import numpy as np 
import os

path = '#2 Image descriptor\Assets\Query'
orb = cv2.ORB_create(nfeatures=1000)

#IMPORT IMAGES 
images = []
classNames = []
myList = os.listdir(path)
print('Jumlah Classes = ', len(myList))
#print(myList)
for cl in myList:
    imgCur = cv2.imread(f'{path}/{cl}', 0)
    images.append(imgCur)
    classNames.append(os.path.splitext(cl)[0])
print (classNames)
   
def findDes(images) :
    desList = []
    for img in images :
        kp, des = orb.detectAndCompute(img, None)
        desList.append(des)
    return desList

def findID(img, desList, thres=15):
    kp2, des2 = orb.detectAndCompute(img, None)
    bf = cv2.BFMatcher()
    matchList = []
    finalVal = -1
    try :
        for des in desList:
            matches = bf.knnMatch(des, des2, k=2)
            good = []
            for m, n in matches:
                if m.distance < 0.7*n.distance:
                    good.append([m])
                matchList.append(len(good))
    except :
        pass
    #print(matchList)
    if len(matchList) != 0:
        if max(matchList) > thres:
            finalVal = matchList.index(max(matchList))
    return finalVal

desList = findDes(images)
print(len(desList))

cap = cv2.VideoCapture(0)
#cap = cv2.imread('#2 Image descriptor\Assets\Train\RE8.jpg')

while True:

    success, img2 = cap.read()
    imgOriginal = img2.copy()
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

    id = findID(img2, desList)
    if id != -1:
        cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)

    cv2.imshow('img2', imgOriginal)
    
    if cv2.waitKey(1) == ord('q'):
        break 

cap.release() #to make sure disable the camera from the code
cv2.destroyAllWindows

我认为错误在:

imgCur = cv2.imread(f'{path}/{cl}', 0)

【问题讨论】:

  • 请提供更多细节,说明应该发生什么以及为什么您认为指定的语句(第 15 行)是错误的根源。
  • 所以代码应该通过匹配查询文件中的图像来显示它是什么对象。然后当我尝试运行代码时,当我向凸轮显示完全相同的对象时,凸轮突然关闭,然后显示错误。我通过删除“f”来更改第 15 行,凸轮没有关闭,但程序没有显示它的对象名称:) 顺便说一句,对不起,我是这个领域的新手。 :)

标签: python opencv python-descriptors


【解决方案1】:

问题出在代码的这一部分:

    if id != -1:
        cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)

该错误基本上是在告诉您您正在尝试在列表中不存在的索引处从classNames 列表中索引id。例如,如果classNames 列表为空,则执行classNames[0] 将返回错误。您可以尝试像这样调试:

    if id != -1:
        if id < len(classNames):
            cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)
        else:
            print(classNames)
            print(f"Error: Attempted to index {id} from list of length {len(classNames)}.")

【讨论】:

  • 现在它显示这个:Error: Attempted to index 945 from list of length 3. 接下来我应该做什么? :D
  • @AlficoPermana 我相信您必须查看您的findID 函数,看看是否有您遗漏的任何错误。
猜你喜欢
  • 2012-12-16
  • 2013-03-18
  • 2023-03-20
  • 2010-11-20
  • 2014-07-05
  • 1970-01-01
  • 2018-06-27
相关资源
最近更新 更多