简体   繁体   English

使用 sklearn svm 进行面部识别的问题

[英]Issues with facial recognition with sklearn svm

I am working on a school project to make a facial recognition program using Python. I am using the face_recognition and scikit-learn libraries.我正在做一个学校项目,使用 Python 制作面部识别程序。我正在使用face_recognitionscikit-learn库。 However, I am facing some issues.但是,我面临一些问题。 Here is my code:这是我的代码:

"""
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()

As the code above suggests, my aim here is to supply multiple images of the same person to the model so that it gets better over time.正如上面的代码所暗示的,我的目标是向 model 提供同一个人的多张图像,以便它随着时间的推移变得更好。 So far, I am facing two major issues.到目前为止,我面临两个主要问题。

Issue 1: If I have only one picture of the same person in their corresponding directories, the classifier is able to predict the name of the person(s) visible in the camera frame.问题 1:如果我在相应目录中只有同一个人的一张照片,分类器能够预测相机画面中可见的人名。 However, if I add a second image to one of the directories (while keeping the other directories with only one image), the classifier predicts every face in the camera frame to be the person who had two images in his/her directory.但是,如果我将第二张图像添加到其中一个目录(同时让其他目录只包含一张图像),分类器会预测相机画面中的每张脸都是在他/她的目录中有两张图像的人。 For example, if person A has two images under his name in his directory while person B only has one, the classifier will predict person B to be person A (not only person B, the classifier will predict anyone to be person A).例如,如果 A 的目录中他的名字下有两张图像,而 B 只有一张,则分类器会将 B 预测为 A(不仅是 B,分类器还将预测任何人都是 A)。 What is causing this issue?是什么导致了这个问题? Having multiple images for the same person is a big reason I am using the svm classifier.拥有同一个人的多个图像是我使用 svm 分类器的一个重要原因。

Issue 2: If I show someone's face whose picture was not in the original training data directories, the classifier still randomly predicts this unknown person to be one the known persons.问题 2:如果我显示某人的脸,其照片不在原始训练数据目录中,分类器仍然随机预测这个未知人是已知人之一。 For example, if I have person A to C in my training directories, and I show a completely unknown person D, the classifier, for some reason, randomly predicts the unknown person to be either person A, B, or C. How should I deal with this?例如,如果我的训练目录中有 A 到 C,我显示一个完全未知的人 D,分类器出于某种原因随机预测未知人是 A、B 或 C。我应该如何处理它? How should I get the classifier to notify me in some way that the person currently in the camera frame is not known, so that I can appropriately handle this?我应该如何让分类器以某种方式通知我当前在相机画面中的人是未知的,以便我可以适当地处理这个问题?

Thanks!谢谢!

My oppinion is that both your issues are going from here:我的意见是你的两个问题都来自这里:

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

There is no tolerance treshold being set, so either it founds the proper image as the most similar first.. or then any other too.没有设置公差阈值,因此它要么先找到最相似的正确图像,要么再找到其他任何图像。

So, if you are obliged to use that sklearn.. I've found that所以,如果你不得不使用那个 sklearn.. 我发现

clf = svm.SVC(gamma="scale", tol=0.001)

has parametr tol .有参数tol Try to set it high or lower then default value (0.001).尝试将其设置为高于或低于默认值 (0.001)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM