python 生成图片人脸识别器

python 生成图片人脸识别器

第一步导入引用

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import face_recognition

第二步制作窗体

root = Tk()
root.title('照片人脸识别') 
path = StringVar()
path2 = StringVar()
Label(root,text = "第一张照片地址:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = printcoords).grid(row = 0, column = 3)   
Label(root,text = "第二张照片地址:").grid(row = 1, column = 0)
Entry(root, textvariable = path2).grid(row = 1, column = 1)
Button(root, text = "路径选择", command = printcoords2).grid(row = 1, column = 3)   
Button(root, text = "识别", command =shibie ).grid(row = 2, column = 3)   

root.mainloop()

第三步写方法

#判断获取的是否是图片
def printcoords():
        File = filedialog.askopenfilename(parent=root)
        if(File.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
            path.set(File)
        else:
            messagebox.showinfo("提示","请选择正确的图片格式!")
#判断获取的是否是图片
def printcoords2():
        File2 = filedialog.askopenfilename(parent=root)
        if(File2.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
            path2.set(File2)
        else:
            messagebox.showinfo("提示","请选择正确的图片格式!")
 #识别图片人脸
def shibie():

  biden_image = face_recognition.load_image_file(path.get())#图片一
  unknown_image = face_recognition.load_image_file(path2.get())#图片二
  try:
     biden_face_encoding = face_recognition.face_encodings(biden_image)[0]#获取图片中的面孔
     unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]#获取图片中的面孔
  except IndexError:
    messagebox.showinfo("提示","未找到人脸。")
#已知面孔信息
  known_faces = [
    biden_face_encoding
  ]

  results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
  if format(results[0])==True:
       messagebox.showinfo("提示","两张照片中的脸是同一个人。")
  else:
       messagebox.showinfo("提示","两张照片中的脸不是同一个人!")

完整代码

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import face_recognition
root = Tk()
root.title('照片人脸识别') 
path = StringVar()
path2 = StringVar()

def printcoords():
        File = filedialog.askopenfilename(parent=root)
        if(File.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
            path.set(File)
        else:
            messagebox.showinfo("提示","请选择正确的图片格式!")

def printcoords2():
        File2 = filedialog.askopenfilename(parent=root)
        if(File2.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
            path2.set(File2)
        else:
            messagebox.showinfo("提示","请选择正确的图片格式!")
def shibie():

  biden_image = face_recognition.load_image_file(path.get())#图片一
  unknown_image = face_recognition.load_image_file(path2.get())#图片二
  try:
     biden_face_encoding = face_recognition.face_encodings(biden_image)[0]#获取图片中的面孔
     unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]#获取图片中的面孔
  except IndexError:
    messagebox.showinfo("提示","未找到人脸。")
#已知面孔信息
  known_faces = [
    biden_face_encoding
  ]

  results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
  if format(results[0])==True:
       messagebox.showinfo("提示","两张照片中的脸是同一个人。")
  else:
       messagebox.showinfo("提示","两张照片中的脸不是同一个人!")
Label(root,text = "第一张照片地址:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = printcoords).grid(row = 0, column = 3)   
Label(root,text = "第二张照片地址:").grid(row = 1, column = 0)
Entry(root, textvariable = path2).grid(row = 1, column = 1)
Button(root, text = "路径选择", command = printcoords2).grid(row = 1, column = 3)   
Button(root, text = "识别", command =shibie ).grid(row = 2, column = 3)   

root.mainloop()

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【项目介绍】 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到94.5分,放心下载使用! 该资源适合计算机相关专业(如人工智能、通信工程、自动化、软件工程等)的在校学生、老师或者企业员工下载,适合小白学习或者实际项目借鉴参考! 当然也可作为毕业设计、课程设计、课程作业、项目初期立项演示等。如果基础还行,可以在此代码基础之上做改动以实现更多功能。 人脸识别考勤系统 本项目是一个基于 Python人脸识别考勤系统,主要使用了 `dlib`、`opencv`、`tkinter`、`PIL`、`pymysql` 等库,并使用了 KNN 算法进行人脸识别。 环境安装 在运行本项目之前,请确保已经安装了以下环境和库: - Python - PyCharm - dlib(需要 Cmake、Visual Basic、C++ 依赖环境) - opencv 库 - tkinter 库 - pymysql - PIL - threading - time 功能介绍 本项目具有以下功能: - 打开系统按钮:打开摄像头捕获人脸。 - 注册按钮:将人脸、学号、姓名信息存储到数据库中。 - 查看缺勤名单:将没有识别签到的学生学号和姓名导出到打开的名单界面。 - 是否开始训练:点击“是”按钮进行人脸模型的训练。 - 帮助按钮:查看使用软件的注意事项。 注意事项 在使用本项目之前,请注意以下事项: 1. 请先打开系统。 2. 注册时请对准人脸,光线充足。 3. 训练时禁止进行其他操作。 文件说明 本项目包含以下文件: - `recognition.py`:主文件。 - `db.py`:数据库文件。 - `help_tip.py`:帮助窗口文件。 - `precamera.py`:预处理文件。 - `late_name.py`:缺勤名单文件。 - `trained_knn_model.clf`:KNN 算法模型文件。 - `knn_examples` 文件夹:包含了训练集和测试集,仅供学习交流探讨使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值