python使用opencv肢体检测增加鼠标中间控制

按中间后不断截取屏幕图片识别人体中的头部并自动移动鼠标到头部连点鼠标左键。有兴趣的可以在第一人称射击游戏中试下。本案例只是草稿性能底下。如果大神进来可以帮忙优化下性能么。

from pynput import mouse
import cv2
from PIL import ImageGrab
import numpy as np
import mediapipe as mp
import time
import pyautogui as ui
import datetime

# 定义窗口名称
WINDOW_NAME = 'Screen'

# 获取屏幕大小
SCREEN_SIZE = (1024, 768)

mpDraw = mp.solutions.drawing_utils
mpPose = mp.solutions.pose
pose = mpPose.Pose()
pTime=0

# 定义全局鼠标事件回调函数
def on_click(x, y, button, pressed):
    print(f'鼠标点击:{x}, {y} ({button.name}) {"按下" if pressed else "释放"}')
    if button.name == "middle" and pressed:
        print("中间按下",pressed)
        # 创建窗口
        # cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
        # cv2.resizeWindow(WINDOW_NAME, SCREEN_SIZE)

        # while pressed:
        for x in range(100):
            # 获取屏幕图像
            image = ImageGrab.grab()
            # 将图像转换为OpenCV格式
            image = np.array(image)
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            # ret, frame = image.read()
            # imgRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            results = pose.process(image)
            # print(results.pose_landmarks)
            if results.pose_landmarks:
                mpDraw.draw_landmarks(image, results.pose_landmarks, mpPose.POSE_CONNECTIONS)
                for id, lm in enumerate(results.pose_landmarks.landmark):
                    h, w, c = image.shape
                    # print("id=", id)
                    # print("lm=", lm)
                    cx, cy = int(lm.x * w), int(lm.y * h)
                    # print("cx and cy = ", cx, cy)
                    if id == 0:
                        cv2.circle(image, (cx, cy), 5, (0, 0, 255), cv2.FILLED)
                        ui.moveTo(cx, cy, duration=0.01)
                        ui.mouseDown()
                        current_time = datetime.datetime.now()
                        print("start:    " + str(current_time))
                    else:
                        cv2.circle(image, (cx, cy), 2, (255, 0, 0), cv2.FILLED)
            cTime = time.time()
            fps = 1 / (cTime - 0)
            pTime = cTime
            cv2.putText(image, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
            # cv2.imshow(WINDOW_NAME, image)
            # c = cv2.waitKey(10)
            # if c == 27:
            #     break
            time.sleep(0.01)
        ui.mouseUp()
        current_time = datetime.datetime.now()
        print("end:    " + str(current_time))
        # 释放窗口资源
        # cv2.destroyAllWindows()
    if button.name == "middle" and False==pressed:
        print("中弹起",pressed)

# 创建鼠标监听器
listener = mouse.Listener(on_click=on_click)

# 启动监听器
listener.start()

# 等待监听器结束
listener.join()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值