Alttaki kodu farklı bir .py'a açın.
Alttaki kodu wind_.py olarak açın ki uğraşmayın.
YOLOV8'i Nasıl Trainlemeniz gerektiğini anlatmayacağım. İki Google aramasıyla bulabilirsiniz.
Python:
import cv2
from time import time
from ultralytics import YOLO
import pydirectinput
from wind_ import WindowCapture
model = YOLO("best.pt") #Trainlediğiniz Yolov8
window_capture = WindowCapture()
def get_screen():
loop_time = time()
while True:
screenshot = window_capture.get_screenshot()
frame = cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)
results = model(frame)
if results and len(results[0]) > 0:
boxs = results[0].boxes
for box in boxs.xyxy:
print(results)
x_center = (box[0] + box[2]) / 2 # X koordinatının ortalamasını al
y_center = (box[1] + box[3]) / 2 # Y koordinatının ortalamasını al
pydirectinput.moveTo(int(x_center),int(y_center))
time.sleep(0.05)
pydirectinput.click()
print('FPS {}'.format(1 / (time() - loop_time)))
loop_time = time()
if cv2.waitKey(1) == ord('q'):
cv2.destroyAllWindows()
break
get_screen()
Alttaki kodu wind_.py olarak açın ki uğraşmayın.
Python:
import numpy as np
import win32gui, win32ui, win32con, win32api
class WindowCapture:
w = 0
h = 0
hwnd = None
cropped_x = 0
cropped_y = 0
offset_x = 0
offset_y = 0
def __init__(self):
self.w = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
self.h = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
border_pixels = 0
titlebar_pixels = 0
self.w = self.w - (border_pixels * 2)
self.h = self.h - titlebar_pixels - border_pixels
self.cropped_x = border_pixels
self.cropped_y = titlebar_pixels
def get_screenshot(self):
wDC = win32gui.GetWindowDC(0)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY)
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.frombuffer(signedIntsArray, dtype='uint8')
img.shape = (self.h, self.w, 4)
# free resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(0, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
img = img[...,:3]
img = np.ascontiguousarray(img)
return img
YOLOV8'i Nasıl Trainlemeniz gerektiğini anlatmayacağım. İki Google aramasıyla bulabilirsiniz.
Moderatörün son düzenlenenleri: