本文主要是介绍人脸五官标注,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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).xy = landmarks.part(n).ycv2.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).xy = landmarks.part(n).ycv2.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()
这篇关于人脸五官标注的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!