from pyautogui import press, typewrite, hotkey
import cv2 as cv
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
from pynput.keyboard import Key, Controller
keyboard = Controller()
model_path = r'C:\Users\Saransh\PycharmProjects\OpenCV\gesture_recognizer.task'
BaseOptions = mp.tasks.BaseOptions
GestureRecognizer = mp.tasks.vision.GestureRecognizer
GestureRecognizerOptions = mp.tasks.vision.GestureRecognizerOptions
GestureRecognizerResult = mp.tasks.vision.GestureRecognizerResult
VisionRunningMode = mp.tasks.vision.RunningMode
def print_result(result: GestureRecognizerResult,output_image: mp.Image,timestamp_ms: int):
if(type(result.gestures)==list and result.gestures):
if(result.gestures[0][0].category_name=='Pointing_Up'):
press('w')
if(result.gestures[0][0].category_name=='Victory'):
press('d')
if(result.gestures[0][0].category_name=='ILoveYou'):
press('a')
if(result.gestures[0][0].category_name=='Closed_Fist'):
press('s')
if(type(result.gestures)==list and result.gestures):
print(result.gestures[0][0].category_name)
options = GestureRecognizerOptions(
base_options= BaseOptions(model_asset_path="C:\\Users\\Saransh\\PycharmProjects\\OpenCV\\gesture_recognizer.task"),
running_mode =VisionRunningMode.LIVE_STREAM,
result_callback=print_result)
cap= cv.VideoCapture(0)
frame_timestamp_ms = 0
with GestureRecognizer.create_from_options(options) as recognizer:
while cap.read():
res,frame=cap.read()
if(res==False):
break
frame=cv.flip(frame,1)
frame=cv.cvtColor(frame, cv.COLOR_BGR2RGB)
k=cv.waitKey(1)
mp_image=mp.Image(image_format=mp.ImageFormat.SRGB,data=frame)
recognizer.recognize_async(mp_image,frame_timestamp_ms)
frame_timestamp_ms+=1
cap.release()
cap.destroyAllWindows()
我希望按键也能在其他应用程序中复制,但它只在 pycharm 环境中复制。任何人都可以帮我吗?它只在 Pycharm 和其他一些应用程序(如 Brave 浏览器、spotif、记事本)中复制,但在其他应用程序中不复制
在这种情况下,最好使用其中一个
pywin32
模块,来帮助你一点,因为似乎仅仅模拟按键是不够的,你需要将其直接发送到应用程序:
import win32com.client, time
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run('name of the app you are trying to run')
time.sleep(3)
shell.AppActivate('same name as earlier')
shell.SendKeys('s') #sends key s to the application
感谢 this stackoverflow 帖子提供了大部分代码:)
在任何情况下,
pyautogui
、keyboard
和pynput
有时无法访问应用程序,特别是如果应用程序处于非活动状态(我假设在您的情况下它是活动的)。