import time from ultralytics import YOLO from PIL import ImageGrab, Image, ImageDraw import numpy as np import cv2 import multiprocessing import onnxruntime import pydirectinput import keyboard import win32api import win32ui import win32con import win32gui def checkImg(i): boxes = i[0].boxes for box in boxes: x, y, x1, y1 = box.xyxy.tolist()[0] mvx = int(((x1 - x) / 2) + x) mvy = int(y + 20) return [mvx, mvy] def capture_window(window_title): # 获取窗口句柄 hwnd = win32gui.FindWindow(None, window_title) if hwnd == 0: print(f"窗口 '{window_title}' 未找到") return None # 获取窗口位置和大小 left, top, right, bottom = win32gui.GetWindowRect(hwnd) # 获取窗口设备上下文 window_dc = win32gui.GetWindowDC(hwnd) mfc_dc = win32ui.CreateDCFromHandle(window_dc) save_dc = mfc_dc.CreateCompatibleDC() # 创建一个位图对象 save_bitmap = win32ui.CreateBitmap() save_bitmap.CreateCompatibleBitmap(mfc_dc, right - left, bottom - top) save_dc.SelectObject(save_bitmap) # 将窗口内容复制到位图中 save_dc.BitBlt((0, 0), (right - left, bottom - top), mfc_dc, (0, 0), win32con.SRCCOPY) # 将位图保存为 PIL Image 对象 bmp_info = save_bitmap.GetInfo() bmp_str = save_bitmap.GetBitmapBits(True) image = Image.frombuffer('RGB', (bmp_info['bmWidth'], bmp_info['bmHeight']), bmp_str, 'raw', 'BGRX', 0, 1) # 释放资源 save_dc.DeleteDC() mfc_dc.DeleteDC() win32gui.ReleaseDC(hwnd, window_dc) return image # 截图并保存为文件 if __name__ == '__main__': window_title = "生死狙击微端战斗" # 替换成你要截图的窗口的标题 screen_width = win32api.GetSystemMetrics(0) screen_height = win32api.GetSystemMetrics(1) print("屏幕宽度:", screen_width) print("屏幕高度:", screen_height) # multiprocessing.freeze_support() model = YOLO( r"C:\Users\25166\PycharmProjects\yolov8Project\ultralytics-main\ultralytics\runs\detect\exp01\weights\best.pt") w = 1707 h = 1067 while True: captured_image = capture_window(window_title) result = model.predict(source=captured_image, conf=0.6) k = cv2.waitKey(1) & 0xFF # cv2.imshow("xxx", result[0].plot()) res = checkImg(result) # print(res)qqqqq # win32api.SetCursorPos(res) # pydirectinput.moveRel(res[0],res[1],relative=True) # pydirectinput.moveTo(999, 100) if keyboard.is_pressed('q'): break elif keyboard.is_pressed('f'): print('f按下了') win32api.SetCursorPos((200, 100)) res = checkImg(result) # print(res) # # win32api.SetCursorPos(res) # # pydirectinput.moveRel(res[0],res[1],relative=True) pydirectinput.moveRel(100, 100) print("关闭脚本") # cv2.destroyAllWindows() # screen_width = win32api.GetSystemMetrics(0) # screen_height = win32api.GetSystemMetrics(1) # # print("屏幕宽度:", screen_width) # print("屏幕高度:", screen_height)yolo 自瞄 鼠标有检测移动不了 管理员权限也不行
标签:res,dc,window,搞完,print,import,save From: https://www.cnblogs.com/inkser/p/17854553.html