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()