You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.9 KiB

import threading
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
6 months ago
import numpy as np
import subprocess
6 months ago
from gesture_recognition import start_recognition, stop_recognition, release_camera, reset_hand_detection
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 705
6 months ago
main_launched = False
status_label = None
6 months ago
root = None
popup_open = False
paused = False
ten_fingers_detected = False
6 months ago
def main():
6 months ago
"""显示欢迎屏幕"""
show_welcome_screen()
def set_window_position(window, width, height):
6 months ago
"""设置窗口位置居中"""
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width - width) // 2
y = (screen_height - height) // 2
window.geometry(f'{width}x{height}+{x}+{y}')
def show_welcome_screen():
6 months ago
"""显示欢迎界面"""
welcome = Tk()
welcome.title("欢迎使用")
set_window_position(welcome, WINDOW_WIDTH, WINDOW_HEIGHT)
bg_image = Image.open("1.png")
bg_image = bg_image.resize((WINDOW_WIDTH, WINDOW_HEIGHT), Image.Resampling.LANCZOS)
bg_image = ImageTk.PhotoImage(bg_image)
background_label = Label(welcome, image=bg_image)
6 months ago
background_label.image = bg_image
background_label.place(relwidth=1, relheight=1)
label = Label(welcome, text="欢迎使用手势识别系统", font=('Helvetica', 24, 'bold'), bg="lightblue", fg="white")
label.place(relx=0.5, rely=0.4, anchor=CENTER)
6 months ago
btn_continue = Button(welcome, text="继续", command=lambda: [welcome.destroy(), show_main_screen()], font=('Helvetica', 14), bg="yellow", fg="black", relief=SOLID)
btn_continue.place(relx=0.5, rely=0.6, anchor=CENTER)
welcome.mainloop()
def show_main_screen():
6 months ago
"""显示主界面"""
global window, canvas, root, paused, popup_open, status_label
window = Tk()
6 months ago
root = window
window.title("手势识别")
set_window_position(window, WINDOW_WIDTH, WINDOW_HEIGHT)
style = ttk.Style(window)
style.theme_use('clam')
frame_controls = Frame(window, bg='lightgray', pady=10)
frame_controls.pack(fill=X)
frame_video = Frame(window, bg='white')
frame_video.pack(fill=BOTH, expand=True)
canvas = Canvas(frame_video, bg='black')
canvas.pack(fill=BOTH, expand=True, padx=20, pady=20)
status_label = Label(frame_controls, text="等待开始...", font=('Helvetica', 14), bg='lightgray')
status_label.pack(side=LEFT, padx=(10, 20))
6 months ago
btn_start = ttk.Button(frame_controls, text="开始", command=lambda: start_thread(root, canvas, status_label))
btn_start.pack(side=LEFT, padx=10)
btn_stop = ttk.Button(frame_controls, text="停止", command=lambda: stop_recognition_with_label(status_label))
btn_stop.pack(side=LEFT, padx=10)
btn_exit = ttk.Button(frame_controls, text="退出", command=lambda: exit_program())
btn_exit.pack(side=LEFT, padx=10)
style = ttk.Style(window)
style.configure('TButton', font=('Helvetica', 14))
window.mainloop()
6 months ago
def start_thread(root, canvas, status_label):
6 months ago
"""启动识别线程"""
6 months ago
global paused, popup_open, ten_fingers_detected
if not paused:
status_label.config(text="正在识别...")
6 months ago
paused = False
popup_open = False
6 months ago
ten_fingers_detected = False
6 months ago
threading.Thread(target=lambda: start_recognition(callback=lambda fingers, img: root.after(0, update_canvas, canvas, fingers, img))).start()
def stop_recognition_with_label(status_label):
6 months ago
"""停止识别并更新状态标签"""
stop_recognition()
status_label.config(text="已停止")
def exit_program():
6 months ago
"""退出程序"""
global window
stop_recognition()
release_camera()
6 months ago
if window is not None:
window.destroy()
window = None
6 months ago
def update_canvas(canvas, fingers, img):
6 months ago
"""更新画布显示图像或处理手指数"""
6 months ago
if root is None or not root.winfo_exists():
return
6 months ago
if isinstance(img, np.ndarray):
6 months ago
img = Image.fromarray(img)
6 months ago
imgtk = ImageTk.PhotoImage(image=img)
canvas.create_image(0, 0, anchor=NW, image=imgtk)
canvas.image = imgtk
canvas.update()
6 months ago
handle_finger_detection(fingers)
6 months ago
def handle_finger_detection(finger_count):
"""处理检测到的手指数"""
6 months ago
global paused, popup_open, main_launched, ten_fingers_detected
6 months ago
if finger_count == 10 and not main_launched:
main_launched = True
6 months ago
ten_fingers_detected = True
6 months ago
launch_main_script()
6 months ago
elif finger_count == 5 and not ten_fingers_detected and not popup_open:
paused = True
popup_open = True
show_stop_recognition_window()
6 months ago
def launch_main_script():
"""启动 main.py 脚本"""
subprocess.Popen(['python', 'main.py'])
6 months ago
def show_stop_recognition_window():
"""显示停止识别确认窗口"""
def on_continue():
"""继续识别"""
6 months ago
global paused, popup_open, status_label
6 months ago
paused = False
6 months ago
popup_open = False
6 months ago
stop_window.destroy()
6 months ago
reset_hand_detection()
6 months ago
start_thread(root, canvas, status_label)
def on_stop():
"""停止识别"""
global popup_open
stop_recognition()
6 months ago
popup_open = False
6 months ago
stop_window.destroy()
6 months ago
if root is not None and root.winfo_exists():
6 months ago
stop_window = Toplevel(root)
stop_window.title("停止识别")
6 months ago
6 months ago
label = Label(stop_window, text="您竖起了五根手指,是否停止识别?", font=('Helvetica', 24, 'bold'))
label.pack(pady=20)
6 months ago
6 months ago
continue_button = Button(stop_window, text="继续识别", command=on_continue)
continue_button.pack(side=LEFT, padx=10, pady=10)
6 months ago
6 months ago
stop_button = Button(stop_window, text="停止识别", command=on_stop)
stop_button.pack(side=RIGHT, padx=10, pady=10)
6 months ago
stop_window.protocol("WM_DELETE_WINDOW", on_continue)
stop_window.mainloop()
6 months ago
if __name__ == "__main__":
main()