OpenCv之简单的人脸识别项目(特征标注页面)-特征标注页面完整代码

时间:2024-06-08 07:31:10
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

        # 加载dlib的人脸检测器和预测器
        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)

        # 转换为PIL格式并显示
        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()