Issue
The following code is throwing indexError list index out of range I tried googling it and everybody is talking about range() errors and calling an index that does not exist. I followed the following tutorial https://www.youtube.com/watch?v=sz25xxF_AVE&ab_channel=Murtaza%27sWorkshop-RoboticsandAI letter for letter and still got the error starting to feel like it is a problem from my python or something. My python version is 3.7
the code
import cv2
import numpy as np
import face_recognition
import os
import pickle
path = 'known_faces'
images = []
classNames = []
myList = os.listdir(path)
print(myList)
for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
classNames.append(os.path.splitext(cl)[0])
print(classNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
print('encode complete')
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
imgS = cv2.resize(img,(0,0),None,0.25,0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodeCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)
for encodeFace,faceLoc in zip(encodeCurFrame, facesCurFrame):
matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
print(faceDis)
matchIndex = np.argmin(faceDis)
if matches[matchIndex]:
name = classNames[matchIndex].upper()
print(name)
I known that I am not putting [0]
at the end of some lines, but that also throws the same error, and now I've reached a dead end
Solution
this error said that -> in a loop -> you try to access an item, which does not exist - cause it's out of range.
if matches[matchIndex]:
or
name = classNames[matchIndex].upper()
in this case, match is a list, and the index is the position of an element in the list.
if 1 case is wrong then:
matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
matchIndex = np.argmin(faceDis)
values of matches are higher than match index (cause of the error)
analogic to the 2nd case.
Answered By - Piotr Żak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.