Image Classifire feature detector

179 Views Asked by At

I have a code that I learn from this link https://www.youtube.com/watch?v=nnH55-zD38I&t=1047s but I got an error that said:

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

Here is the code:

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

I think the error at:

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

There are 1 best solutions below

2
On

The problem is at this part of your code:

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

The error is basically telling you that you are trying to index id from the classNames list at an index that doesn't exist in the list. For example, if the classNames list is empty, doing classNames[0] will return the error. You can try debugging like so:

    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)}.")