• 人脸识别三部曲


    首先看目录结构

    引用文121本

    opencv
    │   采集图片.py  
    │    训练模型.py
    │   人脸识别.py
    │
    └───trainer
    │   │   trainer.yml
    │   
    └───data
    │   └───00_Wang
    │       │   0_00001.jpg
    │       │   0_00002.jpg
    │       │   ...
    │       
    │   └───01_Liu
    │       │   1_00001.jpg
    │       │   1_00001.jpg
    │       │   ...
    │    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    图像信息采集 采集图片.py

    开始运行时,输入待录入的人脸姓名。 按下s键后,开始录入人脸图像,录入两百张后,结束程序

    import cv2
    import os
    "采集数据"
    
    def face_collecting(path):
        Num = 20  # 采集两百张图片
        file_num = len(os.listdir(path))
    
        name = input('input name:\n')
        name_dir = os.path.join(path, str(file_num).zfill(2) + "_" + name)
        os.makedirs(name_dir)
    
        print("按下s键开始录入人脸信息!!!")
    
        cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
        face_detector = cv2.CascadeClassifier(
            'haarcascade_frontalface_alt2.xml')
    
        count = 0
        break1 = 0
        while cap.isOpened():
            ret, frame = cap.read()
            if ret is True:
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                faces = face_detector.detectMultiScale(gray, 1.3, 5)
    
                for (x, y, w, h) in faces:
                    cv2.rectangle(frame, (x, y), (x + w, y + w), (255, 0, 0))
    
                    k = cv2.waitKey(1) & 0xFF  # 按键判断
    
                    if k == ord('s') and count < Num:  # 保存
                        count += 1
                        cv2.imwrite(name_dir + "/" + str(file_num) + "_" + str(count).zfill(5) + ".jpg", gray[y:y+h,x:x+h])
                        print("success to save  " + str(file_num) + "_" + str(count).zfill(5) + ".jpg")
    
                    elif count >= Num or k == ord(' '):  # 200张照片
                        break1 = 1
                        break
                if break1 :
                    break
    
                cv2.imshow('image', frame)
    
            else:
                break
    
        cap.release()
        cv2.destroyAllWindows()
    
    if __name__ == '__main__':
        path = "./data/"
        face_collecting(path)
        print('PyCharm')
    
    
    • 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
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    模型训练 训练模型.py

    import os
    import cv2
    import numpy as np
    from PIL import Image
    " 训练模型.py "
    path = "./data/"
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    detector = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
    
    def get_images_and_labels(path):
        image_paths = []
        name_dirs = [os.path.join(path, f) for f in os.listdir(path)]
        for i in range(0, len(name_dirs) ):
            print("name_dirs[{0}] : ".format(i) , name_dirs[i])
            image_paths += [os.path.join(name_dirs[i], f) for f in os.listdir(name_dirs[i])]
    
        face_samples = []
        ids = []
    
        for image_path in image_paths:
            img = Image.open(image_path).convert('L')
            img_np = np.array(img, 'uint8')
            if os.path.split(image_path)[-1].split(".")[-1] != 'jpg':
                continue
    
            id = int((os.path.split(image_path)[-1].split(".")[0])[0])
            faces = detector.detectMultiScale(img_np)
    
            for (x, y, w, h) in faces:
                face_samples.append(img_np[y:y + h, x:x + w])
                ids.append(id)
        return face_samples, ids
    
    faces, ids = get_images_and_labels(path)
    recognizer.train(faces, np.array(ids))
    recognizer.save('trainer/trainer.yml')
    
    • 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

    人脸识别 人脸识别.py

    import cv2
    import os
    "人脸识别.py "
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    recognizer.read('trainer/trainer.yml')
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
    font = cv2.FONT_HERSHEY_SIMPLEX
    idnum = 0
    
    cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    cam.set(6, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
    minW = 0.1 * cam.get(3)
    minH = 0.1 * cam.get(4)
    
    
    path = "./data/"
    names = []
    for name in os.listdir(path):
        names.append(name.split("_")[1])
        print(names)
    
    
    while True:
        ret, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(int(minW), int(minH))
        )
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
            idnum, confidence = recognizer.predict(gray[y:y + h, x:x + w])
    
            if confidence < 80:
                idum = names[idnum-1]
                confidence = "{0}%".format(round(100 - confidence))
            else:
                idum = "unknown"
                confidence = "{0}%".format(round(100 - confidence))
    
            cv2.putText(img, str(idum), (x + 5, y - 5), font, 1, (0, 0, 255), 1)
            cv2.putText(img, str(confidence), (x + 5, y + h - 5), font, 1, (0, 0, 0), 1)
    
            cv2.imshow('camera', img)
    
        k = cv2.waitKey(1) & 0xFF  # 按键判断
        if k == ord(' '):  # 退出
            break
    
    cam.release()
    cv2.destroyAllWindows()
    
    • 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
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    效果

    在这里插入图片描述

  • 相关阅读:
    前端培训丁鹿学堂:js中箭头函数的面试相关总结
    Java项目:ssm毕业论文管理系统
    最新漏洞:Spring Framework远程代码执行漏洞
    Autosar基本概念详细介绍
    LeetCode每日一题:1462. 课程表 IV(2023.9.12 C++)
    Docker容器技术之容器间通信
    数据资产入表-数据治理-标签设计标准
    linux系统部署Elasticsearch集群
    八股文-TCP的四次挥手
    G1D23-RAGA&报名蓝桥&Attackg&安装cuda&torch
  • 原文地址:https://blog.csdn.net/weixin_46118768/article/details/132869405