import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import dlib
import cv2
import subprocess
import os
win = tk.Tk()
win.title('欢迎')
win.geometry('780x650')
image = Image.open("8.gif")
image = image.resize((780, 650))
photo1 = ImageTk.PhotoImage(image)
canvas = tk.Label(win, image=photo1)
canvas.pack()
file_path = None
image_label_original = None
image_label_annotated = None
def xz():
global file_path, image_label_original
file_path = filedialog.askopenfilename(title="选择图片",
filetypes=(("图片文件", "*.png *.jpg *.jpeg *.bmp"),
("所有文件", "*.*")))
if file_path:
image = Image.open(file_path)
image = image.resize((370, 450))
photo = ImageTk.PhotoImage(image)
if image_label_original:
image_label_original.destroy()
image_label_original = tk.Label(win, image=photo)
image_label_original.image = photo
image_label_original.place(x=10, y=100)
def bz():
global file_path, image_label_annotated
if file_path:
predictor_path = "bz.dat"
if not os.path.exists(predictor_path):
print(f"文件不存在:{predictor_path}")
return
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
img = cv2.imread(file_path)
if img is None:
print("无法加载图片,请检查文件路径或文件完整性。")
return
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face)
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(img, (x, y), 4, (255, 0, 0), -1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = Image.fromarray(img)
image = image.resize((370, 450))
photo = ImageTk.PhotoImage(image)
if image_label_annotated:
image_label_annotated.destroy()
image_label_annotated = tk.Label(win, image=photo)
image_label_annotated.image = photo
image_label_annotated.place(x=400, y=100)
def close():
subprocess.Popen(["python", "登录页面.py"])
win.destroy()
image = Image.open("A.gif")
photo2 = ImageTk.PhotoImage(image)
bt1 = tk.Button(win, image=photo2, width=198, height=32,command=xz)
bt1.place(x=40, y=30)
image = Image.open("F1.gif")
photo3 = ImageTk.PhotoImage(image)
bt2 = tk.Button(win, image=photo3, width=198, height=32,command=bz)
bt2.place(x=285, y=30)
image = Image.open("B.gif")
photo4 = ImageTk.PhotoImage(image)
bt3 = tk.Button(win, image=photo4, width=198, height=32,command=close)
bt3.place(x=530, y=30)
win.mainloop()