1 Star 4 Fork 0

上海市虹口区青少年活动中心/树莓派人脸识别防近视系统Facial Recognition Using RaspberryPi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
train.py 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
徐则宇 提交于 2020-12-19 14:04 . first commit
#coding=utf8
import numpy as np
from PIL import Image
import os
import cv2
# 人脸数据路径
path = 'Facedata'
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)]
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))))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/HKQQSNHDZX/facial-recognition.git
git@gitee.com:HKQQSNHDZX/facial-recognition.git
HKQQSNHDZX
facial-recognition
树莓派人脸识别防近视系统Facial Recognition Using RaspberryPi
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385