Я работаю над проектом искусственного интеллекта для обнаружения и стрельбы по объектам в игре Aimlabs с использованием модели YOLOv5. Код работает нормально, когда игра поставлена на паузу, но не перемещает мышь точно во время игры. Есть идеи, как это исправить?
import cv2
import torch
import numpy as np
import mss
from PIL import Image
import win32api
import win32con
import time
# Load the YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
# Set device (CPU or GPU)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device).eval()
# Screen capture settings
sct = mss.mss()
monitor = sct.monitors[1]
# Function to simulate mouse movement and click
def point_and_click(x, y):
print(f"Moving to: ({x}, {y})") # Debug
# Move the mouse to the position
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE, x * 65535 // win32api.GetSystemMetrics(0), y * 65535 // win32api.GetSystemMetrics(1))
time.sleep(0.05) # Brief pause before clicking to stabilize position
# Perform the click
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
print(f"Clicked at: ({x}, {y})") # Debug
while True:
# Capture the screen
sct_img = sct.grab(monitor)
img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')
frame = np.array(img)
# Perform inference
results = model(frame)
df = results.pandas().xyxy[0]
if not df.empty:
for index, row in df.iterrows():
x1, y1, x2, y2 = int(row['xmin']), int(row['ymin']), int(row['xmax']), int(row['ymax'])
conf, cls = row['confidence'], row['name']
center_x = (x1 + x2) // 2
center_y = (y1 + y2) // 2
# Draw bounding box and label
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, f"{cls} {conf:.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
point_and_click(center_x, center_y)
else:
print("No objects detected.")
cv2.imshow('YOLOv5 Object Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
Подробнее здесь: https://stackoverflow.com/questions/788 ... nd-pywin32
Движение мыши и щелчки не работают правильно в игре с использованием YOLOv5 и PyWin32 ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
C# CefSharp chromeBrowser Попытка имитировать нажатия клавиш и щелчки мыши
Anonymous » » в форуме C# - 0 Ответы
- 26 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Движение мыши не работает в игре (fortnite) с использованием pydirectinput
Anonymous » » в форуме Python - 0 Ответы
- 26 Просмотры
-
Последнее сообщение Anonymous
-