parent dc7b69e610
commit 3450e97dad

@ -4,12 +4,16 @@ from tkinter import ttk
from PIL import Image, ImageTk from PIL import Image, ImageTk
import numpy as np import numpy as np
import subprocess import subprocess
from gesture_recognition import start_recognition, stop_recognition, release_camera, keep_running, reset_hand_detection from gesture_recognition import start_recognition, stop_recognition, release_camera, reset_hand_detection
WINDOW_WIDTH = 800 WINDOW_WIDTH = 800
WINDOW_HEIGHT = 705 WINDOW_HEIGHT = 705
main_launched = False main_launched = False
status_label = None status_label = None
root = None
popup_open = False
paused = False
ten_fingers_detected = False
def main(): def main():
"""显示欢迎屏幕""" """显示欢迎屏幕"""
@ -83,11 +87,12 @@ def show_main_screen():
def start_thread(root, canvas, status_label): def start_thread(root, canvas, status_label):
"""启动识别线程""" """启动识别线程"""
global keep_running, paused, popup_open global paused, popup_open, ten_fingers_detected
if not keep_running: if not paused:
status_label.config(text="正在识别...") status_label.config(text="正在识别...")
paused = False paused = False
popup_open = False popup_open = False
ten_fingers_detected = False
threading.Thread(target=lambda: start_recognition(callback=lambda fingers, img: root.after(0, update_canvas, canvas, fingers, img))).start() 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): def stop_recognition_with_label(status_label):
@ -100,10 +105,14 @@ def exit_program():
global window global window
stop_recognition() stop_recognition()
release_camera() release_camera()
if window is not None:
window.destroy() window.destroy()
window = None
def update_canvas(canvas, fingers, img): def update_canvas(canvas, fingers, img):
"""更新画布显示图像或处理手指数""" """更新画布显示图像或处理手指数"""
if root is None or not root.winfo_exists():
return
if isinstance(img, np.ndarray): if isinstance(img, np.ndarray):
img = Image.fromarray(img) img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img) imgtk = ImageTk.PhotoImage(image=img)
@ -114,12 +123,12 @@ def update_canvas(canvas, fingers, img):
def handle_finger_detection(finger_count): def handle_finger_detection(finger_count):
"""处理检测到的手指数""" """处理检测到的手指数"""
global paused, popup_open, main_launched global paused, popup_open, main_launched, ten_fingers_detected
if finger_count == 10 and not main_launched: if finger_count == 10 and not main_launched:
main_launched = True main_launched = True
ten_fingers_detected = True
launch_main_script() launch_main_script()
elif not popup_open: elif finger_count == 5 and not ten_fingers_detected and not popup_open:
if finger_count == 5:
paused = True paused = True
popup_open = True popup_open = True
show_stop_recognition_window() show_stop_recognition_window()
@ -146,7 +155,7 @@ def show_stop_recognition_window():
popup_open = False popup_open = False
stop_window.destroy() stop_window.destroy()
if root and root.winfo_exists(): if root is not None and root.winfo_exists():
stop_window = Toplevel(root) stop_window = Toplevel(root)
stop_window.title("停止识别") stop_window.title("停止识别")

Loading…
Cancel
Save