python - python中的Face_recognition列表超出范围错误
问题描述
海家伙基于我问的最后一个问题,我没有得到正确的答案。
我想用 face_recognition 库写一个程序。其目的是将一张新面孔的图像片段保存到一个文件夹中,并更新该文件夹,以免将同一张面孔保存两次。我希望程序在输入视频中遇到新面孔时将图像保存到文件夹中。但目前它似乎不起作用。有时它会将同一张脸的整个剪辑保存到我不想要的文件夹中。任何人都可以帮助我使用此代码。我根据我在 youtube 上看到的 #Murtaza 的教程修改了这段代码。
我仍然收到此错误
Traceback (most recent call last):
File "C:/Users/CHIJINDU/AppData/Roaming/JetBrains/PyCharmEdu2020.1/scratches/KRecUnknownFace2.py", line 26, in <module>
encodelistKnown = find_encodings(images)
File "C:/Users/CHIJINDU/AppData/Roaming/JetBrains/PyCharmEdu2020.1/scratches/KRecUnknownFace2.py", line 21, in find_encodings
encode = face_recognition.face_encodings(img)[0]
IndexError: list index out of range
这是下面的改进代码。
import face_recognition
import cv2
import os
import numpy as np
path = r"C:\Users\CHIJINDU\Desktop\KArtIntel"
images = []
class_names= []
myList = os.listdir(path)
for cl in myList:
curImg= cv2.imread(f'{path}\{cl}')
images.append(curImg)
class_names.append(os.path.splitext(cl)[0])
print(class_names)
def find_encodings(images):
encodelist = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
#encode= face_recognition.face_encodings(img)[0]
encodelist.append(encode)
return encodelist
encodelistKnown = find_encodings(images)
print(len(encodelistKnown))
cap = cv2.VideoCapture(r"C:\Users\CHIJINDU\Desktop\Elastic.mp4")
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS =cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faceCurFrame= face_recognition.face_locations(imgS)
encodesCurFrame= face_recognition.face_encodings(imgS, faceCurFrame)
for encodeFace, faceloc in zip(encodesCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodelistKnown,encodeFace)
faceDis = face_recognition.face_distance(encodelistKnown,encodeFace)
matchIndex= np.argmin(faceDis)
i=0
if encodesCurFrame not in encodelistKnown:
newImg= cv2.imwrite(r'C:\Users\CHIJINDU\Desktop\KArtIntel\KUDOS-J14{index}.jpg'.format(index=i), img)
images.append((newImg))
#fps = int(video_capture.get(cv2.CAP_PROP_FPS))
#print(fps)
i+=1
# Display the resulting image
cv2.imshow('Video', img)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
cap.release()
cv2.destroyAllWindows()
解决方案
随机戴维斯是正确的......我更改了代码部分以包括故障转移并且我工作了。也可以尝试使用附件,以非常容易找到的面孔查看不同的行为
import face_recognition
import cv2
import os
import numpy as np
path = r"C:\\Users\\shawn.ramirez\\Pictures\\Scans"
images = []
class_names= []
myList = os.listdir(path)
for cl in myList:
curImg= cv2.imread(f'{path}\{cl}')
images.append(curImg)
class_names.append(os.path.splitext(cl)[0])
print(class_names)
def find_encodings(images):
encodelist = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
try:
encode = face_recognition.face_encodings(img)[0]
#encode= face_recognition.face_encodings(img)[0]
encodelist.append(encode)
return encodelist
except:
error = []
print("found no faces")
return error
encodelistKnown = find_encodings(images)
print(len(encodelistKnown))
cap = cv2.VideoCapture(r"C:\\Users\\shawn.ramirez\\Pictures\\Camera Roll\\sample.mp4")
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS =cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faceCurFrame= face_recognition.face_locations(imgS)
encodesCurFrame= face_recognition.face_encodings(imgS, faceCurFrame)
for encodeFace, faceloc in zip(encodesCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodelistKnown,encodeFace)
faceDis = face_recognition.face_distance(encodelistKnown,encodeFace)
matchIndex= np.argmin(faceDis)
i=0
if encodesCurFrame not in encodelistKnown:
newImg= cv2.imwrite(r'C:\Users\CHIJINDU\Desktop\KArtIntel\KUDOS-J14{index}.jpg'.format(index=i), img)
images.append((newImg))
#fps = int(video_capture.get(cv2.CAP_PROP_FPS))
#print(fps)
i+=1
# Display the resulting image
cv2.imshow('Video', img)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
cap.release()
cv2.destroyAllWindows()
推荐阅读
- python - Discord.py 大众禁令
- reactjs - 背景图像仅在重新加载页面后显示
- activemq-artemis - 用于灾难恢复的镜像与 HA 数据复制
- php - 使用嵌套的 JSON 对象循环 JSON 数组
- java - 如何更改图层列表中项目的大小?
- python - 为什么我在 adfuller 测试中得到 p 值 0.00000?
- drake - drake gurobi 无法处理二次约束
- image - 如何将 svg 图像放在 Xamarin.Android 应用程序的屏幕中?
- pagination - 我可以在没有 apollo / react-query 的情况下将 React 无限滚动与 GraphQL 和 datoCMS 一起使用吗?
- c++ - WinHTTP 服务器能否像 Winsock 一样在单独的线程中处理客户端