parent f418af6739
commit dc7b69e610

@ -43,7 +43,7 @@ def start_recognition(callback=None):
def run_recognition(callback=None): def run_recognition(callback=None):
"""运行手势识别""" """运行手势识别"""
global keep_running, paused, total_raised_fingers global keep_running, paused
while keep_running and cap.isOpened(): while keep_running and cap.isOpened():
ret, img = cap.read() ret, img = cap.read()
@ -74,7 +74,7 @@ def stop_recognition():
global keep_running, cap, paused global keep_running, cap, paused
keep_running = False keep_running = False
paused = False paused = False
if cap is not None and cap.isOpened(): if cap is not None:
cap.release() cap.release()
cap = None cap = None
cv2.destroyAllWindows() cv2.destroyAllWindows()
@ -82,10 +82,15 @@ def stop_recognition():
def release_camera(): def release_camera():
"""释放摄像头资源""" """释放摄像头资源"""
global cap global cap
if cap is not None and cap.isOpened(): if cap is not None:
cap.release() cap.release()
cap = None cap = None
def reset_hand_detection():
"""重置手部检测状态"""
global hands
hands = None
def detect_gesture_and_fingers(hand_landmarks): def detect_gesture_and_fingers(hand_landmarks):
"""检测手势和手指状态""" """检测手势和手指状态"""
gesture_image = get_hand_image(hand_landmarks) gesture_image = get_hand_image(hand_landmarks)

@ -2,21 +2,19 @@ import threading
from tkinter import * from tkinter import *
from tkinter import ttk from tkinter import ttk
from PIL import Image, ImageTk from PIL import Image, ImageTk
import numpy as np # 导入 numpy import numpy as np
import subprocess # 导入 subprocess 模块 import subprocess
from gesture_recognition import start_recognition, stop_recognition, release_camera, keep_running from gesture_recognition import start_recognition, stop_recognition, release_camera, keep_running, reset_hand_detection
WINDOW_WIDTH = 800 WINDOW_WIDTH = 800
WINDOW_HEIGHT = 705 WINDOW_HEIGHT = 705
main_launched = False
main_launched = False # 标志位,记录 main.py 是否已经启动 status_label = None
def main(): def main():
"""显示欢迎屏幕""" """显示欢迎屏幕"""
show_welcome_screen() show_welcome_screen()
def set_window_position(window, width, height): def set_window_position(window, width, height):
"""设置窗口位置居中""" """设置窗口位置居中"""
screen_width = window.winfo_screenwidth() screen_width = window.winfo_screenwidth()
@ -25,7 +23,6 @@ def set_window_position(window, width, height):
y = (screen_height - height) // 2 y = (screen_height - height) // 2
window.geometry(f'{width}x{height}+{x}+{y}') window.geometry(f'{width}x{height}+{x}+{y}')
def show_welcome_screen(): def show_welcome_screen():
"""显示欢迎界面""" """显示欢迎界面"""
welcome = Tk() welcome = Tk()
@ -37,19 +34,17 @@ def show_welcome_screen():
bg_image = ImageTk.PhotoImage(bg_image) bg_image = ImageTk.PhotoImage(bg_image)
background_label = Label(welcome, image=bg_image) background_label = Label(welcome, image=bg_image)
background_label.image = bg_image # 保持引用,防止垃圾回收 background_label.image = bg_image
background_label.place(relwidth=1, relheight=1) background_label.place(relwidth=1, relheight=1)
label = Label(welcome, text="欢迎使用手势识别系统", font=('Helvetica', 24, 'bold'), bg="lightblue", fg="white") label = Label(welcome, text="欢迎使用手势识别系统", font=('Helvetica', 24, 'bold'), bg="lightblue", fg="white")
label.place(relx=0.5, rely=0.4, anchor=CENTER) label.place(relx=0.5, rely=0.4, anchor=CENTER)
btn_continue = Button(welcome, text="继续", command=lambda: [welcome.destroy(), show_main_screen()], btn_continue = Button(welcome, text="继续", command=lambda: [welcome.destroy(), show_main_screen()], font=('Helvetica', 14), bg="yellow", fg="black", relief=SOLID)
font=('Helvetica', 14), bg="yellow", fg="black", relief=SOLID)
btn_continue.place(relx=0.5, rely=0.6, anchor=CENTER) btn_continue.place(relx=0.5, rely=0.6, anchor=CENTER)
welcome.mainloop() welcome.mainloop()
def show_main_screen(): def show_main_screen():
"""显示主界面""" """显示主界面"""
global window, canvas, root, paused, popup_open, status_label global window, canvas, root, paused, popup_open, status_label
@ -86,7 +81,6 @@ def show_main_screen():
window.mainloop() window.mainloop()
def start_thread(root, canvas, status_label): def start_thread(root, canvas, status_label):
"""启动识别线程""" """启动识别线程"""
global keep_running, paused, popup_open global keep_running, paused, popup_open
@ -94,16 +88,13 @@ def start_thread(root, canvas, status_label):
status_label.config(text="正在识别...") status_label.config(text="正在识别...")
paused = False paused = False
popup_open = False popup_open = False
threading.Thread(target=lambda: start_recognition( threading.Thread(target=lambda: start_recognition(callback=lambda fingers, img: root.after(0, update_canvas, canvas, fingers, img))).start()
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):
"""停止识别并更新状态标签""" """停止识别并更新状态标签"""
stop_recognition() stop_recognition()
status_label.config(text="已停止") status_label.config(text="已停止")
def exit_program(): def exit_program():
"""退出程序""" """退出程序"""
global window global window
@ -111,10 +102,9 @@ def exit_program():
release_camera() release_camera()
window.destroy() window.destroy()
def update_canvas(canvas, fingers, img): def update_canvas(canvas, fingers, img):
"""更新画布显示图像或处理手指数""" """更新画布显示图像或处理手指数"""
if isinstance(img, np.ndarray): # 确保传入的img是图像数据 if isinstance(img, np.ndarray):
img = Image.fromarray(img) img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img) imgtk = ImageTk.PhotoImage(image=img)
canvas.create_image(0, 0, anchor=NW, image=imgtk) canvas.create_image(0, 0, anchor=NW, image=imgtk)
@ -122,7 +112,6 @@ def update_canvas(canvas, fingers, img):
canvas.update() canvas.update()
handle_finger_detection(fingers) handle_finger_detection(fingers)
def handle_finger_detection(finger_count): def handle_finger_detection(finger_count):
"""处理检测到的手指数""" """处理检测到的手指数"""
global paused, popup_open, main_launched global paused, popup_open, main_launched
@ -135,31 +124,30 @@ def handle_finger_detection(finger_count):
popup_open = True popup_open = True
show_stop_recognition_window() show_stop_recognition_window()
def launch_main_script(): def launch_main_script():
"""启动 main.py 脚本""" """启动 main.py 脚本"""
subprocess.Popen(['python', 'main.py']) subprocess.Popen(['python', 'main.py'])
def show_stop_recognition_window(): def show_stop_recognition_window():
"""显示停止识别确认窗口""" """显示停止识别确认窗口"""
def on_continue(): def on_continue():
"""继续识别""" """继续识别"""
global paused, popup_open global paused, popup_open, status_label
paused = False paused = False
popup_open = False # 关闭弹窗后将标志设置为False popup_open = False
stop_window.destroy() stop_window.destroy()
reset_hand_detection()
start_thread(root, canvas, status_label) start_thread(root, canvas, status_label)
def on_stop(): def on_stop():
"""停止识别""" """停止识别"""
global popup_open global popup_open
stop_recognition() stop_recognition()
popup_open = False # 关闭弹窗后将标志设置为False popup_open = False
stop_window.destroy() stop_window.destroy()
stop_window = Tk() if root and root.winfo_exists():
stop_window = Toplevel(root)
stop_window.title("停止识别") stop_window.title("停止识别")
label = Label(stop_window, text="您竖起了五根手指,是否停止识别?", font=('Helvetica', 24, 'bold')) label = Label(stop_window, text="您竖起了五根手指,是否停止识别?", font=('Helvetica', 24, 'bold'))
@ -174,6 +162,5 @@ def show_stop_recognition_window():
stop_window.protocol("WM_DELETE_WINDOW", on_continue) stop_window.protocol("WM_DELETE_WINDOW", on_continue)
stop_window.mainloop() stop_window.mainloop()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

Loading…
Cancel
Save