代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# @Author : WuFanLong
# @Time : 2021/10/25 10:27
# @File : face_training.py
# @Software: PyCharm
# @Function:
import numpy as np
from PIL import Image
from tkinter import messagebox
import os
import cv2
class Call2:
@staticmethod
def call_back2():
# 人脸数据路径
path = 'Facedata'
# LBPHFaceRecognizer_create()为contrib中的函数
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier(r'.\model\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 ...')
messagebox.showinfo("提示", "正在训练面孔。 这将需要几秒钟。 请等待 ...")
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))))
messagebox.showinfo("提示", "{0} faces trained. Exiting Program".format(len(np.unique(ids))))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。