【问题标题】:Issues with facial recognition with sklearn svm使用 sklearn svm 进行面部识别的问题
【发布时间】:2022-11-17 03:07:22
【问题描述】:

我正在做一个学校项目,用 Python 制作面部识别程序。我正在使用 face_recognitionscikit-learn 库。但是,我面临一些问题。这是我的代码:

"""
Structure:
        <Data>/
            <person_1>/
                <person_1_face-1>.jpg
                <person_1_face-2>.jpg
                .
                .
                <person_1_face-n>.jpg
           <person_2>/
                <person_2_face-1>.jpg
                <person_2_face-2>.jpg
                .
                .
                <person_2_face-n>.jpg
            .
            .
            <person_n>/
                <person_n_face-1>.jpg
                <person_n_face-2>.jpg
                .
                .
                <person_n_face-n>.jpg
"""
import os

import cv2
import face_recognition
import numpy as np
from sklearn import svm

IMG_DATA_DIR = "Data"
class_names = []
encodings = []
image_dirs = os.listdir(IMG_DATA_DIR)

# Loop through each person in the training directory
for img_dir in image_dirs:
    img_files = os.listdir(f"{IMG_DATA_DIR}/{img_dir}")

    # Loop through each training image for the current person
    for img_file in img_files:
        # Get the face encodings for the face in each image file
        img = face_recognition.load_image_file(f"{IMG_DATA_DIR}/{img_dir}/{img_file}")
        class_names.append(os.path.splitext(img_dir)[0])

        img_encoding = face_recognition.face_encodings(img)[0]
        encodings.append(img_encoding)

clf = svm.SVC(gamma="scale")
clf.fit(encodings, class_names)


# Initializing webcam
camera = cv2.VideoCapture(0)

process_this_frame = True

while True:
    success, img = camera.read()

    if process_this_frame:
        img_small = cv2.resize(img, (0, 0), None, 0.50, 0.50)
        img_small = cv2.cvtColor(img_small, cv2.COLOR_BGR2RGB)

        camera_faces_loc = face_recognition.face_locations(img_small)
        camera_encodings = face_recognition.face_encodings(img_small, camera_faces_loc)

        face_names = []
        for encoding in camera_encodings:
            # loop through each face encodings visible in the camera frame
            # predict the names of the faces currently visible in the frame using clf.predict
            name = clf.predict([encoding])
            print(name)

            face_names.extend(name)

    process_this_frame = not process_this_frame

    for (top, right, bottom, left), name in zip(camera_faces_loc, face_names):
        top *= 2
        right *= 2
        bottom *= 2
        left *= 2

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

    cv2.imshow("WebCam", img)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()

正如上面的代码所暗示的,我的目标是向模型提供同一个人的多张图像,以便它随着时间的推移变得更好。到目前为止,我面临两个主要问题。

问题 1:如果我在相应目录中只有同一个人的一张照片,则分类器能够预测相机画面中可见的人名。但是,如果我将第二张图片添加到其中一个目录(同时让其他目录只包含一张图片),分类器会预测每一张脸在相机框架中是在他/她的目录中有两张图像的人。例如,如果 A 在他的目录中有两张图像,而 B 只有一张,则分类器将预测 B 为 A(不仅是 B,分类器还将预测任何人成为A)。是什么导致了这个问题?拥有同一个人的多个图像是我使用 svm 分类器的一个重要原因。

问题 2:如果我显示某人的脸,其照片不在原始训练数据目录中,分类器仍会随机预测这个未知人是已知人之一。例如,如果我的训练目录中有 A 到 C,我显示了一个完全未知的人 D,分类器出于某种原因随机预测未知人是 A、B 或 C。我应该如何处理它?我应该如何让分类器以某种方式通知我当前在相机画面中的人是未知的,以便我可以适当地处理这个问题?

谢谢!

【问题讨论】:

    标签: python scikit-learn svm face-recognition


    【解决方案1】:

    我的意见是你的两个问题都来自这里:

    face_names = []
    for encoding in camera_encodings:
        name = clf.predict([encoding])
        print(name)
        face_names.extend(name)
    

    没有设置容差阈值,所以它要么先找到最相似的正确图像,要么再找到其他任何图像

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 2012-07-01
      • 2019-06-08
      • 2012-10-23
      • 2018-08-20
      • 2015-05-21
      • 1970-01-01
      • 2018-04-12
      • 2015-04-24
      相关资源
      最近更新 更多