当前位置: 首页 > ds >正文

识别人脸人名,只是窗口的中文乱码待解决

通过face_recognition功能将文件夹下面的图片,以图片名为名字进行储存

然后读取新图片与现有文件夹下面的照片进行对比,达到识别人脸人名的目标

face_recognition 使用说明

https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md

import face_recognition
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import cv2
import os# This is an example of running face recognition on a single image
# and drawing a box around each person that was identified.# 自动从sampe_images文件夹加载所有图片
known_face_encodings = []
known_face_names = []
images_path = "sampe_images"# 遍历文件夹中的所有图片
for image_file in os.listdir(images_path):try:# 跳过非图片文件if not image_file.lower().endswith(('.png', '.jpg', '.jpeg')):continue# 加载图片并获取人脸编码image_path = os.path.join(images_path, image_file)face_image = face_recognition.load_image_file(image_path)face_encoding = face_recognition.face_encodings(face_image)# 确保检测到了人脸if len(face_encoding) > 0:known_face_encodings.append(face_encoding[0])# 使用文件名(不包含扩展名)作为人名name = os.path.splitext(image_file)[0]# 确保中文名称可以正确显示try:name = name.encode('utf-8').decode('utf-8')except UnicodeError:name = name.encode('gbk').decode('utf-8')known_face_names.append(name)print(f"成功加载图片: {name}")except Exception as e:print(f"处理图片 {image_file} 时出错: {str(e)}")continue# Load an image with an unknown face
unknown_image = face_recognition.load_image_file("f:/ldh.png")# Find all the faces and face encodings in the unknown image
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library
# See http://pillow.readthedocs.io/ for more about PIL/Pillow
pil_image = Image.fromarray(unknown_image)
# Create a Pillow ImageDraw Draw instance to draw with
draw = ImageDraw.Draw(pil_image)# Loop through each face found in the unknown image
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):# See if the face is a match for the known face(s)matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# Or instead, use the known face with the smallest distance to the new faceface_distances = face_recognition.face_distance(known_face_encodings, face_encoding)best_match_index = np.argmin(face_distances)if matches[best_match_index]:name = known_face_names[best_match_index]# 使用OpenCV绘制人脸框cv2.rectangle(unknown_image, (left, top), (right, bottom), (0, 0, 255), 2)# 使用PIL绘制中文文本img_pil = Image.fromarray(unknown_image)draw = ImageDraw.Draw(img_pil)# 加载中文字体(请确保系统中有这个字体,或者使用其他可用的中文字体)try:font = ImageFont.truetype("simhei.ttf", 20)  # 使用黑体except:try:font = ImageFont.truetype("simsun.ttc", 20)  # 尝试使用宋体except:font = ImageFont.load_default()  # 如果都没有,使用默认字体# 绘制文本背景text_bbox = draw.textbbox((left, bottom + 5), name, font=font)text_width = text_bbox[2] - text_bbox[0]text_height = text_bbox[3] - text_bbox[1]draw.rectangle(((left, bottom), (left + text_width, bottom + text_height + 10)), fill=(0, 0, 255))# 绘制文本draw.text((left, bottom + 5), name, font=font, fill=(255, 255, 255))# 转换回OpenCV格式unknown_image = np.array(img_pil)# 创建窗口并显示结果
image_height, image_width = unknown_image.shape[:2]
window_title = "人脸识别结果".encode('gbk').decode(encoding='gbk')
cv2.namedWindow(window_title, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_title, image_width, image_height)
cv2.imshow(window_title, cv2.cvtColor(unknown_image, cv2.COLOR_RGB2BGR))
cv2.waitKey(0)
cv2.destroyAllWindows()# You can also save a copy of the new image to disk if you want by uncommenting this line
# pil_image.save("image_with_boxes.jpg")

http://www.xdnf.cn/news/5751.html

相关文章:

  • 数据库实验报告 SQL SERVER 2008的基本操作 1
  • 调出事件查看器界面的4种方法
  • 从规划到完善,原型标注图全流程设计
  • 国产化芯片ZCC3790--同步升降压控制器的全新选择, 替代LT3790
  • 接口和抽象类的区别
  • uniapp-商城-54-后台 新增商品1
  • A Survey of Learning from Rewards:从训练到应用的全面剖析
  • 计算机网络|| 路由器和交换机的配置
  • 运用数组和矩阵对数据进行存取和运算——NumPy模块 之四
  • Excel表的导入与导出
  • RAGFlow 初步尝试 (01)
  • 基于HTTP头部字段的SQL注入:SQLi-labs第17-20关
  • OpenCV4.8 开发实战系列专栏之 49 二值图像分析 -轮廓外接矩形
  • 我用Deepseek + 亮数据爬虫神器 1小时做出輿情分析器
  • 一文了解JavaScript对象
  • Kotlin与Ktor构建Android后端API
  • RWA开发全解析:技术架构、合规路径与未来趋势
  • Matlab 汽车制动纵向动力学模型和PID控制
  • Webpack中Compiler详解以及自定义loader和plugin详解
  • 用python清除PDF文件中的水印(Adobe Acrobat 无法删除)
  • 机架式服务器是什么?机架式/塔式/刀片式三大服务器类型区别与选型全解析
  • vue3+flask+sqlite前后端项目实战
  • 谱聚类,大模型
  • uniapp 复刻 keep 跑步运动轨迹 (获取当前经纬度信息)
  • Java实现MCP server,配合DeepSeek和达梦数据库,实现基于企业数据库的智能问答
  • 在Windows 境下,将Redis和Nginx注册为服务。
  • uniapp使用npm下载
  • 《数字人 :生成之旅》
  • 第二十五节:轮廓检测-轮廓特征 (面积、周长、边界框等)
  • 前端面试宝典---webpack面试题