import tkinter import cv2 from identify import * from tkinter.filedialog import * import threading # import pymouse from tkinter import * from tkinter import ttk import tkinter.filedialog as dir import queue from tkinter.messagebox import showinfo import win32com from win32com.client import Dispatch from pptx import Presentation from selenium import webdriver from selenium.webdriver.common.keys import Keys import win32con import win32api import time import os import string window = tkinter.Tk() window.title('窗口标题') window.geometry('200x50') control_flag = 0 x = 0 y = 0 def identify_thread(): capture = cv2.VideoCapture(0) j = 1 count = 0 last_control_flag = 0 global control_flag, x, y while 1: ret, image = capture.read() mp_drawing = mp.solutions.drawing_utils mp_holistic = mp.solutions.holistic holistic = mp_holistic.Holistic(static_image_mode=True) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = holistic.process(image) hand_points = [] if results.left_hand_landmarks: for hand_landmarks in results.left_hand_landmarks.landmark: h, w, c = image.shape cx, cy = int(hand_landmarks.x * w), int(hand_landmarks.y * h) hand_points.append((cx, cy)) flag = judge_control(hand_points) # x = position[0] # y = position[1] # cv2.namedWindow("Video") # cv2.imshow("Video", image) # if cv2.waitKey(1) & 0xFF == ord('q'): # break if not flag: control_flag = 0 continue if flag != last_control_flag: last_control_flag = flag count = 0 else: if count < 4: count = count + 1 if count < 3: continue control_flag = flag # print("final_control_flag = " + str(control_flag)) capture.release() cv2.destroyAllWindows() def open_ppt(file_path): ppt = Dispatch('PowerPoint.Application') ppt.Visible = 1 # 后台运行 ppt.DisplayAlerts = 0 # 不显示,不警告 pptSel = ppt.Presentations.Open(file_path) def show(): win32api.keybd_event(116, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(116, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 time.sleep(2) win32api.keybd_event(34, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(34, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_page_up(): win32api.keybd_event(38, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(38, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_page_down(): win32api.keybd_event(40, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(40, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_ppt_begin(): win32api.keybd_event(116, 0, 0, 0) # 代表按下f键 win32api.keybd_event(16, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(116, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 win32api.keybd_event(16, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_ppt_end(): win32api.keybd_event(27, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(27, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_open_pencil(): win32api.keybd_event(17, 0, 0, 0) # 代表按下f键 win32api.keybd_event(80, 0, 0, 0) # 代表按下f键 time.sleep(0.02) win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 win32api.keybd_event(80, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_draw(): global x, y # m = pymouse.PyMouse() # m.move(x, y) # win32api.keybd_event(40, 0, 0, 0) # 代表按下f键 # time.sleep(0.02) # win32api.keybd_event(40, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键 def control_thread(): global control_flag while 1: time.sleep(2) # if control_flag in range(1, 2, 3, 4, 5): # time.sleep(2) # elif control_flag == 6: # time.sleep(0.05) print("control_flag = " + str(control_flag)) if control_flag == 1: control_ppt_begin() if control_flag == 2: control_page_up() if control_flag == 3: control_page_down() if control_flag == 4: control_ppt_end() # if control_flag == 5: # control_open_pencil() # if control_flag == 6: # control_draw() def open_file(): global control_flag file_path = askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser('H:/'))) open_ppt(file_path) identify_t = threading.Thread(target=identify_thread) # print("control_flag1 = " + str(control_flag)) control_t = threading.Thread(target=control_thread) # print("control_flag2 = " + str(control_flag)) identify_t.setDaemon(True) control_t.setDaemon(True) identify_t.start() control_t.start() if __name__ == '__main__': bt1 = tkinter.Button(window, text='打开文件', width=15, height=15, command=open_file) bt1.pack() window.mainloop()