人脸五官标注

1.导入必要的包

dlib这个包可以在浏览器下载,然后解压

import tkinter as tk
from tkinter import filedialog
import cv2
from PIL import Image, ImageTk
import dlib
import subprocess

2.创建窗口

窗口的名字跟大小都可以自己设置

win = tk.Tk()
win.title("人脸识别")
win.geometry("1000x500")

3.界面美化

创建了两个框架,一个用于放置按钮,另一个用于放置图片。按钮框架位于窗口的顶部,图片框架位于按钮框架的下方。两个框架都有一定的空白空间,使得界面更加美观

# 创建一个Frame用于放置按钮
button_frame = tk.Frame(win)
button_frame.pack(side="top", fill="x", padx=10, pady=10)

# 创建一个Frame用于放置图片
image_frame = tk.Frame(win)
image_frame.pack(side="top", fill="both", expand=True, padx=10, pady=10)

4.设置窗口显示图片

# 使用 grid 来布局图片
def layout_images():
    # 清除原有的图片
    for label in image_labels:
        label.destroy()
    image_labels.clear()

    # 计算每张图片的显示尺寸
    img_width = int(win.winfo_width() / 4) - 20  # 20是列间距的总和
    img_height = 200  # 可以根据需要调整

    for i, path in enumerate(selected_image_paths):
        img = cv2.imread(path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        detector = dlib.get_frontal_face_detector()
        predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")  # 确保该文件存在
        faces = detector(gray, 1)

        if len(faces) > 0:
            landmarks = predictor(gray, faces[0])
            for n in range(0, 68):
                x = landmarks.part(n).x
                y = landmarks.part(n).y
                cv2.circle(img, (x, y), 1, (255, 0, 0), -1)

        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_pil = Image.fromarray(img_rgb)
        img_pil = img_pil.resize((img_width, img_height))  # 调整图片大小
        img_tk = ImageTk.PhotoImage(image=img_pil)

        # 创建一个新的标签来显示图片,并使用 grid 来布局
        image_label = tk.Label(image_frame, image=img_tk)
        image_label.image = img_tk  # 保持对图像的引用
        image_label.grid(row=0, column=i, padx=10, pady=10)
        image_labels.append(image_label)

5.设置窗口最大显示图片数量

def select_image():
    global selected_image_paths

    # 限制选择的图片数量
    if len(selected_image_paths) < 4:
        selected_path = filedialog.askopenfilename()

        if selected_path:
            selected_image_paths.append(selected_path)
            layout_images()

6.设置关闭窗口

def Esc():
    win.destroy()

7.连接主窗口

这里因为这只是一个大的项目中的一部分 ,所以你这里可以把函数改为pass,程序即可正常使用

def zhu():
    subprocess.Popen(["python", "main.py"])
    win. Destroy()

8.设置按钮和循环

button_select = tk.Button(button_frame, text="选择图片", font=my_font, command=select_image, fg='blue')
button_select.grid(row=0, column=0, padx=10, pady=10)


esc = tk.Button(button_frame, text='返回系统', font=my_font, command=zhu, fg='blue')
esc.grid(row=0, column=1, padx=10, pady=10)


esc2 = tk.Button(button_frame, text='退出系统', font=my_font, command=Esc, fg='blue')
esc2.grid(row=0, column=2, padx=10, pady=10)


win.mainloop()

源码

import tkinter as tk
from tkinter import filedialog
import cv2
from PIL import Image, ImageTk
import dlib
import subprocess


# 初始化变量
selected_image_paths = []  # 存储图片路径
image_labels = []  # 存储已显示的图片标签
my_font = ("Times New Roman", 20)

# 创建主窗口
win = tk.Tk()
win.title("人脸识别")
win.geometry("1000x500")

# 创建一个Frame用于放置按钮
button_frame = tk.Frame(win)
button_frame.pack(side="top", fill="x", padx=10, pady=10)

# 创建一个Frame用于放置图片
image_frame = tk.Frame(win)
image_frame.pack(side="top", fill="both", expand=True, padx=10, pady=10)

# 使用 grid 来布局图片
def layout_images():
    # 清除原有的图片
    for label in image_labels:
        label.destroy()
    image_labels.clear()

    # 计算每张图片的显示尺寸
    img_width = int(win.winfo_width() / 4) - 20  # 20是列间距的总和
    img_height = 200  # 可以根据需要调整

    for i, path in enumerate(selected_image_paths):
        img = cv2.imread(path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        detector = dlib.get_frontal_face_detector()
        predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")  # 确保该文件存在
        faces = detector(gray, 1)

        if len(faces) > 0:
            landmarks = predictor(gray, faces[0])
            for n in range(0, 68):
                x = landmarks.part(n).x
                y = landmarks.part(n).y
                cv2.circle(img, (x, y), 1, (255, 0, 0), -1)

        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_pil = Image.fromarray(img_rgb)
        img_pil = img_pil.resize((img_width, img_height))  # 调整图片大小
        img_tk = ImageTk.PhotoImage(image=img_pil)

        # 创建一个新的标签来显示图片,并使用 grid 来布局
        image_label = tk.Label(image_frame, image=img_tk)
        image_label.image = img_tk  # 保持对图像的引用
        image_label.grid(row=0, column=i, padx=10, pady=10)
        image_labels.append(image_label)

def select_image():
    global selected_image_paths

    # 限制选择的图片数量
    if len(selected_image_paths) < 4:
        selected_path = filedialog.askopenfilename()

        if selected_path:
            selected_image_paths.append(selected_path)
            layout_images()

def Esc():
    win.destroy()

def zhu():
    subprocess.Popen(["python", "main.py"])
    win.destroy()



# 创建按钮并使用 grid 来布局
button_select = tk.Button(button_frame, text="选择图片", font=my_font, command=select_image, fg='blue')
button_select.grid(row=0, column=0, padx=10, pady=10)


esc = tk.Button(button_frame, text='返回系统', font=my_font, command=zhu, fg='blue')
esc.grid(row=0, column=1, padx=10, pady=10)


esc2 = tk.Button(button_frame, text='退出系统', font=my_font, command=Esc, fg='blue')
esc2.grid(row=0, column=2, padx=10, pady=10)


win.mainloop()
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值