• python版opencv人脸训练与人脸识别


    1.人脸识别准备

    使用的两个opencv

    D:\python2023>pip list |findstr opencv
    opencv-contrib-python     4.8.1.78
    opencv-python             4.8.1.78
    
    • 1
    • 2
    • 3

    数据集使用前一篇Javacv的数据集,网上随便找的60张图片,只是都挪到了D:\face目录下方便遍历

    D:\face\1 30张刘德华图片
    D:\face\2 30张刘亦菲图片

    2.人脸识别模型训练

    # -*- coding: utf-8 -*-
    import os
    
    import cv2
    import numpy as np
    
    recognizer = cv2.face.LBPHFaceRecognizer().create() # Fisher需要reshape
    classifier = cv2.CascadeClassifier('E:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
    def load_dataset(dataset_path):
        images=[]
        labels=[]
        for root,dirs,files in os.walk(dataset_path):
            for file in files:
                images.append(cv2.imread(os.path.join(root, file),cv2.IMREAD_GRAYSCALE))
                labels.append(int(os.path.basename(root)))
        return images,labels
    if __name__ == '__main__':
        images,labels = load_dataset('D:\\face')
        recognizer.train(images,np.array(labels))
        recognizer.save('face_model.xml')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.人脸识别推理预测

    # -*- coding: utf-8 -*-
    import os
    
    import cv2
    
    
    def face_detect(image):
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        classifier = cv2.CascadeClassifier('E:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
        faces = classifier.detectMultiScale(gray, 1.2, 5)
        if (len(faces) == 0):
            return None, None
        (x, y, w, h) = faces[0]
        return gray[y:y + w, x:x + h], faces[0]
    
    
    def draw_rectangle(img, rect):
        (x, y, w, h) = rect
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
    
    
    def draw_text(img, text, x, y):
        cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (128, 128, 0), 2)
    
    
    def predict(image):
        image_copy = image.copy()
        face, rect = face_detect(image_copy)
        tuple = recognizer.predict(face)
        print(tuple)
        draw_rectangle(image_copy, rect)
        draw_text(image_copy, str(tuple[0]), rect[0], rect[1])
        return image_copy
    
    
    if __name__ == '__main__':
        recognizer = cv2.face.LBPHFaceRecognizer().create()  # Fisher需要reshape
        recognizer.read("face_model.xml")
        for root, dirs, files in os.walk('D:\\face\\2'):
            for file in files:
                file_path = os.path.join(root, file)
                predict_image = predict(cv2.imread(file_path))
                cv2.imshow('result', predict_image)
                cv2.waitKey(1000)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    总结

    代码逻辑基本同Javacv,但更简洁,这里训练出来模型准确度也高于Javacv (可能是参数不一致导致的)

  • 相关阅读:
    MySQL索引原理及慢查询优化
    ERROR (-1005)]: Host not enough! nebula无法注册元数据
    校园综合服务平台V3.9.2 源码修复大部分已知BUG
    c盘哪些文件可以删除?正确答案在这里!
    铜死亡+铁死亡,搭配WGCNA+单细胞+分型+实验
    紫光同创FPGA实现图像去雾 基于暗通道先验算法 纯verilog代码加速 提供2套工程源码和技术支持
    NASA教学纳卫星发射计划-41批
    在Ubuntu14.0系统中安装arm-2009q3.tar.bz2交叉编译器
    4.1 探索LyScript漏洞挖掘插件
    涨知识!Python 的异常信息还能这样展现
  • 原文地址:https://blog.csdn.net/qq_39506978/article/details/134023948