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