import os
import tkinter as tk
from tkinter import filedialog
import face_recognition
class StudentSignInApp:
def __init__(self, master):
self.master = master
master.title("学生课堂签到软件")
self.student_images = {}
self.load_images()
self.label_instruction = tk.Label(master, text="请选择要识别的图片:")
self.label_instruction.pack()
self.button_load_image = tk.Button(master, text="加载图片", command=self.load_image)
self.button_load_image.pack()
self.canvas_image = tk.Canvas(master, width=400, height=400)
self.canvas_image.pack()
self.label_result = tk.Label(master, text="")
self.label_result.pack()
self.button_quit = tk.Button(master, text="退出", command=master.quit)
self.button_quit.pack()
def load_images(self):
for filename in os.listdir("student_images"):
name, ext = os.path.splitext(filename)
if ext.lower() in [".jpg", ".png"]:
image_path = os.path.join("student_images", filename)
image = face_recognition.load_image_file(image_path)
encoding = face_recognition.face_encodings(image)[0]
self.student_images[name] = encoding
def load_image(self):
file_path = filedialog.askopenfilename()
image = face_recognition.load_image_file(file_path)
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(list(self.student_images.values()), face_encoding)
if True in matches:
index = matches.index(True)
name = list(self.student_images.keys())[index]
self.label_result.configure(text="恭喜,%s已成功签到!" % name)
else: self.label_result.configure(text="对不起,未识别出人脸,请重试。")
root = tk.Tk()
app = StudentSignInApp(root)
root.mainloop()
无论识别的是那个人,最后显示出来签到的始终是第一个人?