第一:安装opencv库 第二:下载文件haarcascade_frontalface_alt.xml与程序放在同目录下,第三,安装opencv-contrib-python库 第四。安装pandas
openpyxl opencv 等库,第五。建入一个names.xlsx文件,里面有两列,分别是动态人脸识别(一)新建的,拼音缩写字母,(第一列)第二列,是汉字的名字。
import cv2
import os
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import pandas as pd
from datetime import datetime
def draw_chinese_text_on_image(image, text, position, font_path, font_size, color):
# 将OpenCV图像转换为PIL图像
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘制的对象
draw = ImageDraw.Draw(pil_image)
# 加载字体,确保字体文件路径正确,并且字体支持中文
font = ImageFont.truetype(font_path, font_size)
# 在图像上绘制文本
draw.text(position, text, fill=color, font=font)
# 将PIL图像转换回OpenCV格式
opencv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
return opencv_image
def take_photo(path):
capture = cv2.VideoCapture(0)
# 加载人脸特径数据
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
filename = 1
flag_write = False
while True:
flag, frame = capture.read()
if not flag:
break
gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, minNeighbors=5)
for x, y, w, h in faces:
if flag_write: