人脸识别(数据训练)

系列文章目录

人脸采集
数据训练



请添加图片描述

前言

在我们的人脸采集 的文章中我们将人脸数据采集,到后我么是不可可以直接运用到人脸识别训练里面的要经过过通的数据训练生成数据文件看。,也就是yml文件才可以被人脸数据所识别。

一、主要思路

  • 引入我们的第三方库
import os
import cv2 as cv
import numpy as np
from PIL import Image

  • 建立一个列表
# 存储人脸数据
    face_data = []
    # 存储姓名:
    face_ids = []
  • 遍历上个文件采集的图片
# 存储的图片信息
    imaegPaths = [os.path.join(path, f) for f in os.listdir(path)]
  • 加载分类器
# 加载分类器
    face_d = cv.CascadeClassifier(r"C:\Users\HONOR\AppData\Local\Programs\Python\Python37\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml")

和上一个分类器的位置一致

  • 写入数据以及id
for imagePath in imaegPaths:
        PIL_Img = Image.open(imagePath).convert("L")
        img_np = np.array(PIL_Img, "uint8")

        if os.path.split(imagePath)[-1].split(".")[-1] != 'jpg':
            continue
        id = int(os.path.split(imagePath)[-1].split(".")[1])
        faces = face_d.detectMultiScale(img_np)
        for x, y, w, h in faces:
            face_ids.append(id)
            face_data.append(img_np[y:y + h, x:x + w])
            # 打印脸部特征
            print("id:", id)
            print("fs:", face_data)

完整的代码

# -*- coding = utf-8 -*-
# -*-@Time : 2022/8/18 17:33  
# -*-@Author : 于金龙 
# -*-@File : 数据训练.py
# -*-@software : PyCharm
import os

import cv2 as cv
import numpy as np
from PIL import Image


def getxunlainshuju(path):
    # 存储人脸数据
    face_data = []
    # 存储姓名:
    face_ids = []
    # 存储的图片信息
    imaegPaths = [os.path.join(path, f) for f in os.listdir(path)]
    # 加载分类器
    face_d = cv.CascadeClassifier(r"C:\Users\HONOR\AppData\Local\Programs\Python\Python37\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml")

    for imagePath in imaegPaths:
        PIL_Img = Image.open(imagePath).convert("L")
        img_np = np.array(PIL_Img, "uint8")

        if os.path.split(imagePath)[-1].split(".")[-1] != 'jpg':
            continue
        id = int(os.path.split(imagePath)[-1].split(".")[1])
        faces = face_d.detectMultiScale(img_np)
        for x, y, w, h in faces:
            face_ids.append(id)
            face_data.append(img_np[y:y + h, x:x + w])
            # 打印脸部特征
            print("id:", id)
            print("fs:", face_data)
    return face_data, face_ids


if __name__ == '__main__':
    faces, ids = getxunlainshuju(r'facedata/')
    recognzer = cv.face.LBPHFaceRecognizer_create()
    recognzer.train(faces, np.array(ids))
    recognzer.write("yujinlong232.yml")

总结

以上内容我对人脸采集部分的理解,希望对大家有用,下面是我写的一个python文件
数据训练的完整代码文件(大家有兴趣可以去下载玩一下)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿龙的代码在报错

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值