如何对图像进行人脸识别并确定其中的人脸是否已知?

2024-09-30 01:37:49 发布

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

目前,我正在制作一个人脸识别程序,其中我有一个已知的人脸文件夹,程序可以访问该文件夹来识别未识别图像中的已知人脸。我在一个图像上测试这个程序,它应该在所有的面上返回“Unknown”,但是我在所有面上获取矩形,然后将图像保存到image_output文件夹时遇到了问题。在

文件夹结构。在

已知图像: 我的项目//已知的面//“这里的所有面”

输出文件夹: 我的项目//图像输出//

我有一个脸谱.jpg在我的程序所在的目录中。在我的代码中,我非常确定我试图在每个面周围绘制矩形并将它们的名称显示在矩形下方的方法有问题。(几乎在程序的底部。)

我的代码

我只有这一个程序,这是一个修改的THIS段代码从制造商的脸识别模块。在

这是一张_识别.py:

from PIL import Image
import face_recognition
import cv2

# Load Unidentified faces
image = face_recognition.load_image_file("faces.jpg")

# face1
face1_image = face_recognition.load_image_file("KNOWN_FACES//face1.jpg")
face1_face_encoding = face_recognition.face_encodings(face1_image)[0]

# face2
face2_image = face_recognition.load_image_file("KNOWN_FACES//face2.jpg")
face2_face_encoding = face_recognition.face_encodings(face2_image)[0]

# face3
face3_image = face_recognition.load_image_file("KNOWN_FACES//face3.jpg")
face3_face_encoding = face_recognition.face_encodings(face3_image)[0]

# face4
face4_image = face_recognition.load_image_file("KNOWN_FACES//face4.jpg")
face4_face_encoding = face_recognition.face_encodings(face4_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    face1_face_encoding,
    face2_face_encoding,
    face3_face_encoding,
    face4_face_encoding
]
known_face_names = [
    "FACE1",
    "FACE2",
    "FACE3",
    "FACE4"
]

# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
face_locations = face_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

# Then compare faces with known faces, and save an image that contains
# rectangles around EACH face.

# Find all the face encodings in the image.
face_encodings = face_recognition.face_encodings(image, face_locations)

face_names = []
for face_encoding in face_encodings:
    # See if the face is a match for the known face(s)
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
    name = "Unknown"

    # If a match was found in known_face_encodings, just use the first one.
    if True in matches:
        first_match_index = matches.index(True)
        name = known_face_names[first_match_index]

    face_names.append(name)

    # Return final image.
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the image was scaled to 1/5 size.
        top *= 5
        right *= 5
        bottom *= 5
        left *= 5

        # Draw a box around the face.
        cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face.
        # See, this is where I think I messed up.
        # DOESN'T WORK (I think).
        cv2.rectangle(image, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(image, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)


# Finally, save the image to image_output folder.
final_image = Image.fromarray(image)
final_image.save("image_output/FACE_REC_PICTURE.png")

Tags: theinimage程序cv2encodingfacejpg
1条回答
网友
1楼 · 发布于 2024-09-30 01:37:49

你在画矩形之前忘了调整图像的大小

#resize image TOO
image = cv2.resize(image,(0,0),fx=5,fy=5)
for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the image was scaled to 1/5 size.
        top *= 5
        right *= 5
        bottom *= 5
        left *= 5



        # Draw a box around the face.
        cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)


        cv2.rectangle(image, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(image, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

除此之外,你还有两条这条线

^{pr2}$

在你的剧本里。一般来说,我不想提这个,但是这个函数很慢,所以你不想重复。在

依我的拙见,根据我的经验,你这样做是因为用人脸识别库查找人脸位置非常慢。如果我可以干预

查看Haar cascades—以找到正面和侧面的位置。在

它做同样的工作,但是你需要将它集成到你的程序中,这根本不是问题。基本上是一样的

face_recognition.face_locations(image)

而人脸识别库可以找到高达60秒的人脸位置。。haarcascades虽然精度和可靠性稍差一点,但最多可以达到1s。您不需要重新缩放图像和使用坐标

相关问题 更多 >

    热门问题