1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| from ultralytics import YOLOv10 import time import pyautogui as pt import pygetwindow import numpy as np import cv2 as cv import torch from PIL import ImageGrab
model = YOLOv10("best.pt")
window_title = "" window = pygetwindow.getWindowsWithTitle(window_title)[0]
device = torch.device("cuda:0") model.to(device)
while True: if window: x, y, w, h = window.left, window.top, window.width, window.height screenshot = ImageGrab.grab(bbox=[x, y, x + w, y + h]) image_src = cv.cvtColor(np.array(screenshot), cv.COLOR_RGB2BGR) size_x, size_y = image_src.shape[1], image_src.shape[0] image_det = cv.resize(image_src, (640, 640)) result = model.predict(source=image_det, imgsz=640, conf=0.1, save=False) boxes = result[0].boxes.xywhn boxes = sorted(boxes, key=lambda x:x[0]) for box in boxes: cv.rectangle(image_src, (int((box[0] - box[2]/2) * size_x), int((box[1] - box[3]/2) * size_y)), (int((box[0] + box[2]/2) * size_x), int((box[1] + box[3]/2) * size_y)), color=(255, 255, 0), thickness=2) pt.click(x=x + box[0] * size_x, y=y + box[1] * size_y) time.sleep(0.8)
|