数组的Python真值不明确(面部识别的If语句)

2024-06-17 12:43:16 发布

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

我有一个人脸识别文件,它在一个文件夹中搜索“已知”人脸,然后在另一个文件夹中搜索这些人脸

要搜索已知人脸的文件夹,请执行以下操作:

my_dir = './img/known/'
encoding_for_file = []
for i in os.listdir(my_dir):
    image = my_dir + i
    image = face_recognition.load_image_file(image)
    image_encoding = face_recognition.face_encodings(image)
    encoding_for_file.append(image_encoding[0])

然后,代码在文件夹中搜索一张脸,如果找到,则显示图像

for i in range (1, 8+1):

    #Construct the picture name and print it
    file_name = f"./img/unknown/{str(i).zfill(5)}.jpg"
    print(file_name)

    #Load the file
    newPic = face_recognition.load_image_file(file_name)

    #Search every detected face
    for face_encoding in face_recognition.face_encodings(newPic):


        results = face_recognition.compare_faces([image_encoding], face_encoding)

        #If match, show it
        if results[0] == True:
            img = Image.open(file_name)
            img.show()

但是,我在下面的行中得到一个“数组的真值不明确”错误:

if results[0] == True:

它建议使用a.any()或a.all()——我没有使用过

有什么建议吗


Tags: nameinimage文件夹imgformydir