OpenCV python 人脸识别(分类)

OpenCV python 人脸识别(分类)

人脸图片
OpenCV python 人脸识别(分类)_第1张图片
OpenCV python 人脸识别(分类)_第2张图片
资源图片:face.zip

import cv2
import numpy as np


def resize_img(path):
    """导入图片、调整图片尺寸至相同大小"""

    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (250, 350))

    return img


def main():

    # 1.导入人脸训练图片
    img_list = [resize_img("./face/a0.jpg"),
                resize_img("./face/a1.jpg"),
                resize_img("./face/a2.jpg"),
                resize_img("./face/a3.jpg"),
                resize_img("./face/b0.jpg"),
                resize_img("./face/b1.jpg"),
                resize_img("./face/b2.jpg"),
                resize_img("./face/b3.jpg")]

    # 2.设置图片标签类型
    labels = [0, 0, 0, 0, 1, 1, 1, 1]

    # 3.创建识别对象
    # recognizer = cv2.face.EigenFaceRecognizer_create()
    # recognizer = cv2.face.LBPHFaceRecognizer_create()
    recognizer = cv2.face.FisherFaceRecognizer_create()

    # 4.训练图片
    recognizer.train(img_list, np.array(labels))

    # 5.导入预测图片并预测
    predict_image = resize_img("./face/b4.jpg")
    label, confidence = recognizer.predict(predict_image)

    # 6.显示预测结果
    print("预测类型:", label)
    print("置信度:", confidence)


if __name__ == '__main__':
    main()

结果:
在这里插入图片描述

你可能感兴趣的:(Opencv-python)