前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于OpenCV for Python入门-face_recognition实现人脸识别

关于OpenCV for Python入门-face_recognition实现人脸识别

作者头像
python与大数据分析
发布2022-05-19 12:22:59
3910
发布2022-05-19 12:22:59
举报
文章被收录于专栏:python与大数据分析

face_recognition是世界上最简洁的人脸识别库,你可以使用Python和命令行工具提取、识别、操作人脸。

face_recognition的人脸识别是基于业内领先的C++开源库 dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率。但对小孩和亚洲人脸的识别准确率尚待提升。

face_recognition可以产生很多有趣的应用。

官方原文代码见:https://github.com/ageitgey/face_recognition/blob/master/examples/face_recognition_knn.py

face_recognition包括三个部分的代码

1、训练数据集

2、预测数据集

3、输出标签

训练数据集要遵循一定的数据格式,可以是以文件夹名称为标识的一个个图片,也可以是以文件名为标识的单个图片,当然前者每个人的图片越多,训练越充分。

训练数据集首先要检测出人脸,多个或零个均非合法的人脸

然后将图片二进制传入X,图片标识传入y,进行训练

训练图片是使用sklearn的KNN近邻分类器(KNeighborsClassifier)进行训练的,这个近邻的个数可以调节。

训练完成后写入模型文件,写入模型文件的好处是一次训练,后续可以直接使用,毕竟训练的时间过长,而用户可以实时添加人脸,难点在于如何增量进行训练?还没想到好办法。

预测过程中最大的困惑是neighbors的返回值,以及对返回值的处理,尤其是distance,这个distance关系到预测的准确与否,无论如何knn都会返回最近的距离和标签,但这个标签正确与否就不知道了,所以阈值设置很重要,我这边设置的是0.5。

最后是在图片上标注出人脸矩形和识别出的人物标签。

我这边是用的ORL数据集,以及从网上找到刘德华、成龙和我的照片。

代码语言:javascript
复制
import math
from sklearn import neighbors
import os
import os.path
import pickle
from PIL import Image, ImageDraw
import face_recognition
from face_recognition.face_recognition_cli import image_files_in_folder

ALLOWED_EXTENSIONS = {'bmp','png', 'jpg', 'jpeg'}

# 对指定的训练图片文件夹进行训练
def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False):
    """
    Trains a k-nearest neighbors classifier for face recognition.
    :param train_dir: directory that contains a sub-directory for each known person, with its name.
     (View in source code to see train_dir example tree structure)
     Structure:
        <train_dir>/
        ├── <person1>/
        │   ├── <somename1>.jpeg
        │   ├── <somename2>.jpeg
        │   ├── ...
        ├── <person2>/
        │   ├── <somename1>.jpeg
        │   └── <somename2>.jpeg
        └── ...
    :param model_save_path: (optional) path to save model on disk
    :param n_neighbors: (optional) number of neighbors to weigh in classification. Chosen automatically if not specified
    :param knn_algo: (optional) underlying data structure to support knn.default is ball_tree
    :param verbose: verbosity of training
    :return: returns knn classifier that was trained on the given data.
    """
    X = []
    y = []

    # 循环获取训练集图片
    for class_dir in os.listdir(train_dir):
        if not os.path.isdir(os.path.join(train_dir, class_dir)):
            continue
        # 遍历当前任务的每一张图片
        # image_files_in_folder,这个地方是获取文件夹下的所有图片文件,可以修改其中的图片类型
        # 默认是jpg|jpeg|png,后来追加了bmp
        print('training picture of {}'.format(class_dir))
        for img_path in image_files_in_folder(os.path.join(train_dir, class_dir)):
            # 加载图片文件,其实是numpy数组
            image = face_recognition.load_image_file(img_path)
            # 获取人脸检测框
            face_bounding_boxes = face_recognition.face_locations(image)
            # 多个人物或者0个人物不处理
            if len(face_bounding_boxes) != 1:
                # If there are no people (or too many people) in a training image, skip the image.
                if verbose:
                    print("Image {} not suitable for training: {}".format(img_path, "Didn't find a face" if len(face_bounding_boxes) < 1 else "Found more than one face"))
            else:

                # Add face encoding for current image to the training set
                X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0])
                y.append(class_dir)

    # 设置KNN分类器的近邻数
    # Determine how many neighbors to use for weighting in the KNN classifier
    if n_neighbors is None:
        # n_neighbors = int(round(math.sqrt(len(X))))
        n_neighbors = 3
        if verbose:
            print("Chose n_neighbors automatically:", n_neighbors)

    # 创建KNN分类器,并进行训练
    knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance')
    knn_clf.fit(X, y)

    # 保存KNN训练结果
    if model_save_path is not None:
        with open(model_save_path, 'wb') as f:
            pickle.dump(knn_clf, f)

    return knn_clf

# 对指定的预测图片进行预测
def predict(X_img_path, knn_clf=None, model_path=None, distance_threshold=0.5):
    """
    Recognizes faces in given image using a trained KNN classifier
    :param X_img_path: path to image to be recognized
    :param knn_clf: (optional) a knn classifier object. if not specified, model_save_path must be specified.
    :param model_path: (optional) path to a pickled knn classifier. if not specified, model_save_path must be knn_clf.
    :param distance_threshold: (optional) distance threshold for face classification. the larger it is, the more chance
           of mis-classifying an unknown person as a known one.
    :return: a list of names and face locations for the recognized faces in the image: [(name, bounding box), ...].
        For faces of unrecognized persons, the name 'unknown' will be returned.
    """
    # 校验当前文件类型
    if not os.path.isfile(X_img_path) or os.path.splitext(X_img_path)[1][1:] not in ALLOWED_EXTENSIONS:
        raise Exception("Invalid image path: {}".format(X_img_path))

    # 校验当前模型文件和knn模型,两个不能全空
    if knn_clf is None and model_path is None:
        raise Exception("Must supply knn classifier either thourgh knn_clf or model_path")

    # 加载训练好的KNN模型
    if knn_clf is None:
        with open(model_path, 'rb') as f:
            knn_clf = pickle.load(f)

    # 加载图片,获取人脸检测框
    X_img = face_recognition.load_image_file(X_img_path)
    X_face_locations = face_recognition.face_locations(X_img)
    # print('predict {}'.format(X_img_path))
    # 如果未找到人脸,返回[]
    if len(X_face_locations) == 0:
        return []

    # 对测试图片进行编码转换,转换为numpy数组
    faces_encodings = face_recognition.face_encodings(X_img, known_face_locations=X_face_locations)

    # 通过KNN模型找到最佳匹配的人脸
    closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=3)
    # # 返回值indices:第0列元素为参考点的索引,后面是(n_neighbors - 1)个与之最近的点的索引
    # # 返回值distances:第0列元素为与自身的距离(为0),后面是(n_neighbors - 1)个与之最近的点与参考点的距离
    # closest_distances= [[0.34997745 0.3750366  0.37819395]]
    # closest_distances= [[ 5 12 11]]
    # for i in closest_distances:
    #     print('closest_distances=',i)
    are_matches = []
    # are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]
    for i in range(len(X_face_locations)):
        are_matches.append( closest_distances[0][i][0] <= distance_threshold)
        #print('predict value=', closest_distances[0][i][0])
    # print('knn_clf.predict(faces_encodings)=',knn_clf.predict(faces_encodings))
    # 预测分类
    return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]

# 在图片上输出预测标签
def show_prediction_labels_on_image(img_path, predictions):
    """
    Shows the face recognition results visually.
    :param img_path: path to image to be recognized
    :param predictions: results of the predict function
    :return:
    """
    # 打开图片,获取句柄
    pil_image = Image.open(img_path).convert("RGB")
    draw = ImageDraw.Draw(pil_image)

    # 对预测结果进行遍历
    for name, (top, right, bottom, left) in predictions:
        # 在人脸周边画矩形框
        draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))

        # There's a bug in Pillow where it blows up with non-UTF-8 text
        # when using the default bitmap font
        name = name.encode("UTF-8")

        # 在人脸地图输出标签
        text_width, text_height = draw.textsize(name)
        draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
        draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))

    # Remove the drawing library from memory as per the Pillow docs
    del draw

    # Display the resulting image
    pil_image.show()


if __name__ == "__main__":
    # 指定训练集路径和预测集路径
    train_dir=r'C:\Python\Pycharm\docxprocess\picture\ORL'
    test_dir=r'C:\Python\Pycharm\docxprocess\picture\predict'
    # 第一步,通过KNN分类器进行训练,并存储模型文件
    print("Training KNN classifier...")
    classifier = train(train_dir, model_save_path="trained_knn_model.clf", n_neighbors=3)
    print("Training complete!")

    # 第二步,使用训练分类器,对未知图片进行预测
    for image_file in os.listdir(test_dir):
        full_file_path = os.path.join(test_dir, image_file)
        print("Looking for faces in {}".format(image_file))

        # 使用训练模型进行预测
        predictions = predict(full_file_path, model_path="trained_knn_model.clf")

        # 输出结果
        for name, (top, right, bottom, left) in predictions:
            print("- Found {} at ({}, {})".format(name, left, top))

        # 图片上显示输出结果
        show_prediction_labels_on_image(os.path.join(test_dir, image_file), predictions)

人物1的识别,分类准确,距离为0

人物2的识别,分类准确,距离为0

合照的识别,里面包括周星驰、刘德华、李连杰、成龙,但周星驰和李连杰不在训练集内,周星驰未识别出来,刘德华和成龙识别正确,但李连杰识别出的最近距离甚至比刘德华本人还接近。

刘德华本人的识别,分类准确,距离为0.35左右

刘亦菲的识别,分类准确,距离为0.68左右

本人的识别,分类准确,距离为0.42左右,我只有两张训练照片,一张是身份证,一张是正照。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-04-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python与大数据分析 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档