函数内部异常处理后不存在语法错误

问题描述 投票:0回答:0

在 known faces 文件夹中有一组图像,但在本例中只使用了第一张图像。发生的问题是与正在比较的图像有关。目标是在函数内部根本未检测到面部时通过循环,从而导致错误,即索引错误。它的问题是在 except 函数之后我无法使用 face_recognition 库中的 compare faces 函数。

import time
import cv2
import os
import face_recognition
import numpy as np

    def faceRec(knownImages, unknownImages, unknownFacePath, knownFacePath):
        i = 0
        x = 0
        ki = os.path.join(knownFacePath,knownImages[0])
        refImage = face_recognition.load_image_file(ki)
        refImage = cv2.cvtColor(refImage,cv2.COLOR_BGR2RGB)
        encodedRefImage = face_recognition.face_encodings(refImage)[0]
        '''for knownImage in knownImages:
            ki = os.path.join(knownFacePath,knownImage)
            refImage = face_recognition.load_image_file(ki)
            refImage = cv2.cvtColor(refImage,cv2.COLOR_BGR2RGB)
            encodedRefImage = face_recognition.face_encodings(refImage)[0]
            x+=1
            print(f"X:{x}")
        '''
        for unknownImage in unknownImages:
            ui = os.path.join(unknownFacePath,unknownImage)
            checkImage = face_recognition.load_image_file(ui)
            checkImage = cv2.cvtColor(checkImage,cv2.COLOR_BGR2RGB)
            try:
                checkImage_encoding = face_recognition.face_encodings(checkImage)[0]
            except IndexError:
                print("Image Not Clear")
                pass
            else:
                return(face_recognition.compare_faces([encodedRefImage], checkImage_encoding)
                        
    
    
    unknownFacePath = "Unknown Faces/"
    knownFacePath = "Known Faces/"
    #Main Code
    if __name__ == '__main__':
        '''setup()'''
        discordPing.getTokenID("token.txt")
        knownImages = os.listdir(knownFacePath)
        unknownImages = imageSnap(unknownFacePath)
        ans = faceRec(knownImages, unknownImages, unknownFacePath, knownFacePath)   
                        
    

给出的错误是语法错误。

python list opencv exception face-recognition
© www.soinside.com 2019 - 2024. All rights reserved.