为什么我的pycharm代码给出“列表索引超出范围”?我在跟踪youtuber

2024-09-25 00:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个人脸识别代码,起初我遵循Murtaza做的youtube video,它很有效,但当我尝试自己做的时候,我遇到了这个问题,无法解决它,下面是代码:

while True:
    success, img = webcam.read()
    imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
    imgSLocs = face_recognition.face_locations(imgS)
    imgSEncodes = face_recognition.face_encodings(imgS, imgSLocs)

for encode, loc in zip(imgSEncodes, imgSLocs):
    compare = face_recognition.compare_faces(refEncodes, encode)
    faceDist = face_recognition.face_distance(refEncodes, encode)
    print(faceDist)
    matchindex = np.argmin(faceDist)
    print (matchindex)
    if compare[matchindex]:   # in this line the error starts <------------
        name = refImgNames[matchindex].upper()
        print(name)

我所期望的是matchindex=0或1,因为我只有两个图像,但实际情况是matchindex = np.argmin(faceDist)对代码几乎没有影响,它不应该给我这些值中最小值的索引吗? 注意:可能有用:faceDist是一个复合数组,索引0中可能有128个值,索引1中可能有128个值。 我是Python代码的新手,所以请考虑基本错误的概率。 以下是完整的代码(如果有帮助):

import cv2
import numpy as np
import face_recognition
import os

path = 'refrence images'
imgList = os.listdir(f'{path}')
refImgs = []
refImgNames = []
print(imgList)
for i in imgList:
    image = cv2.imread(f'{path}/{i}')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    refImgs.append(image)


def imgEncoder(images):
    encodes = []
    for i in images:
        imgloc = face_recognition.face_locations(i)
        imgencode = face_recognition.face_encodings(i, imgloc)
        encodes.append(imgencode)
    return encodes


refEncodes = imgEncoder(refImgs)
print('encoding done Alhamdulillah')
webcam = cv2.VideoCapture(0)

while True:
    success, img = webcam.read()
    imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
    imgSLocs = face_recognition.face_locations(imgS)
    imgSEncodes = face_recognition.face_encodings(imgS, imgSLocs)

    for encode, loc in zip(imgSEncodes, imgSLocs):
        compare = face_recognition.compare_faces(refEncodes, encode)
        faceDist = face_recognition.face_distance(refEncodes, encode)
        print(faceDist)
        matchindex = np.argmin(faceDist)
        print (matchindex)
        if compare[matchindex]:   # in this line the error starts
            name = refImgNames[matchindex].upper()
            print(name)
            y1, x2, y2, x1 = loc
            y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
            cv2.rectangle(img, (x1, y1), (x2, y2), (0.255, 0), 2)
            cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (255.255, 0), cv2.FILLED)
            cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 1)
            cv2.imshow(1,img)
            cv2.waitKey(0)
    print('done')

you can see on left panel that I only have 2 images (1.png, 2.png)


Tags: 代码inimgcv2encodecomparefaceprint
1条回答
网友
1楼 · 发布于 2024-09-25 00:30:02
你会考虑使用深部吗?只需几行代码即可完成

#!pip install deepface
from deepface import DeepFace
DeepFace.stream(db_path = "C:/my_db")

在这里,您应该使用.jpg或.png扩展名将面部图像数据库存储在my_db文件夹中。流功能将访问您的网络摄像头,并实时运行人脸识别和人脸属性分析(年龄、性别、情绪)

相关问题 更多 >