import threading from tkinter import * from tkinter import ttk from PIL import Image, ImageTk from gesture_recognition import start_recognition, stop_recognition, release_camera, keep_running WINDOW_WIDTH = 800 WINDOW_HEIGHT = 705 def main(): show_welcome_screen() def set_window_position(window, width, height): 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(): 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) 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) 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(): global window, canvas window = Tk() 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)) btn_start = ttk.Button(frame_controls, text="开始", command=lambda: start_thread(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() def start_thread(canvas, status_label): global keep_running if not keep_running: status_label.config(text="正在识别...") threading.Thread(target=lambda: start_recognition(callback=lambda img: update_canvas(canvas, img))).start() def stop_recognition_with_label(status_label): stop_recognition() status_label.config(text="已停止") def exit_program(): global window stop_recognition() release_camera() window.destroy() def update_canvas(canvas, img): img = Image.fromarray(img) imgtk = ImageTk.PhotoImage(image=img) canvas.create_image(320, 240, image=imgtk) canvas.image = imgtk canvas.update() if __name__ == "__main__": main()