1 Star 4 Fork 0

Li_Xibat/Face_find

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
face_training.py 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
Li_Xibat 提交于 2023-11-13 20:53 +08:00 . change
# 人脸训练
import numpy as np
from PIL import Image
import os
import cv2
# 人脸数据路径
path = 'Facelxj'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def getImagesAndLabels(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)] # join函数的作用?
faceSamples = []
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img, 'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x, y, w, h) in faces:
faceSamples.append(img_numpy[y:y + h, x: x + w])
ids.append(id)
return faceSamples, ids
print('Training faces. It will take a few seconds. Wait ...')
faces, ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
recognizer.write(r'face_trainer/trainer.yml')
print("{0} faces trained. Exiting Program".format(len(np.unique(ids))))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/LiXibat/face_find.git
git@gitee.com:LiXibat/face_find.git
LiXibat
face_find
Face_find
master

搜索帮助