【问题标题】:How to do face recognition on images and determining if the faces in it is known?如何对图像进行人脸识别并确定其中的人脸是否已知?
【发布时间】:2019-07-17 16:25:27
【问题描述】:

目前,我正在制作一个人脸识别程序,其中有一个 KNOWN_FACES 文件夹,程序可以访问该文件夹以识别未知图像中的已知人脸。我在应该在所有面上返回 'Unknown' 的图像上测试程序,但是我无法在所有面上获取矩形,然后将图像保存在 image_output 文件夹。

文件夹结构。

已知图像: my_project//KNOWN_FACES//“这里的所有面孔”

输出文件夹: my_project//image_output//

我的程序所在的目录中有一个 faces.jpg。在我的代码中,我很确定我尝试在每个面周围绘制矩形并在矩形正下方显示它们的名称的方式有问题。 (几乎在程序的底部。)

我的代码

我只有这个程序,它是对face_recognition模块制造商THIS的一段代码的修改。

这里是 image_face_recognition.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")

【问题讨论】:

  • 到底是什么问题?
  • @Martin 问题是我试图在一张未识别的人脸图像中围绕每个人脸绘制一个矩形。如果找到面孔,则在矩形下方绘制带有 text 的矩形,如果不是已知面孔则显示“未知”或如果它是已知面孔则显示“名称”,然后导出带有矩形的原始图像适用于图像。
  • 我完全知道你想做什么,但我不知道是什么问题?矩形是否在其他地方绘制?没有什么被认可的吗?矩形没问题,但文本在外面?有什么错误吗?
  • 请用 opencv 加载图像。 Face_recognition 是很棒的库,但您不希望它打开图像或识别面部位置。识别面部位置真是太慢了。您可能希望对正面和侧面使用 Haar 识别。 (我做了这个项目,所以我想你会在某个时候到达那里)
  • 我想我遇到了你的问题。你认为你已经重新调整了图像。可能很好,但如果你想重新缩放它,除了坐标,你还需要重新缩放图像。所以在绘制矩形之前,调整图像大小 -> cv2.resize(img,(0,0),fx=5,fy=5)

标签: python opencv face-recognition


【解决方案1】:

在绘制矩形之前忘记调整图像大小

#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)

除此之外,你有两倍的这条线

face_locations = face_recognition.face_locations(image)

在您的脚本中。一般来说,我不会提及,但这是一个相当慢的功能,所以你不想做两次。

以我的拙见, 并且根据我的经验,您这样做重新缩放,因为使用 Face_recognition 库查找面部位置非常慢。如果我可以干预,

查看Haar cascades - 查找正面和侧面位置。

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

face_recognition.face_locations(image)

虽然 face_recognition 库最多可以找到 60 秒的人脸位置。但 Haar 级联虽然准确性和可靠性稍差一些,但最多可以找到 1 秒。您无需重新缩放图像并使用坐标播放

【讨论】:

  • 非常感谢!我知道了。我一定会尝试 Haar 识别。
猜你喜欢
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 2011-12-14
  • 1970-01-01
  • 2017-06-07
  • 2013-04-26
  • 2021-10-30
相关资源
最近更新 更多