Ask Your Question
0

Help with optimization OpenCV Python [closed]

asked 2020-04-14 09:46:41 -0600

includeBRAIN gravatar image

updated 2020-04-14 15:38:40 -0600

Im new to python and im trying to write a simple bot thats do some actions when detect green color mob. Bot is working, but slowly. So i need help with optimization. Bot where simple: detect mobs and get its coordinates, one shot mob with skill, teleport, check for Mp or Die. Here part of code:

import win32api, win32security, win32con, ctypes, win32gui, win32ui, pyautogui, time, cv2, numpy as np

hwnd = None
left = None
top = None
right = None
bot = None
width = None
height = None
centerX = None
centerY = None
templates = []
edenKey = 'f9'
attackKey = 'q'
teleportKey = 'w'

def LoadImages():
    global templates
    templates.append(cv2.imread("Images\\InGame.png", 0))      #0
    templates.append(cv2.imread("Images\\ChatOpen.png", 0))    #1
    templates.append(cv2.imread("Images\\InCombat.png", 0))    #2
    templates.append(cv2.imread("Images\\LowSP.png", 0))       #3
    templates.append(cv2.imread("Images\\Dead.png", 0))        #4
    templates.append(cv2.imread("Images\\Healer.png", 0))      #5
    templates.append(cv2.imread("Images\\Kafra.png", 0))       #6
    templates.append(cv2.imread("Images\\Kafra_0.png", 0))     #7
    templates.append(cv2.imread("Images\\Kafra_1.png", 0))     #8
    templates.append(cv2.imread("Images\\Kafra_2.png", 0))     #9
    templates.append(cv2.imread("Images\\Kafra_3.png", 0))     #10
    templates.append(cv2.imread("Images\\Kafra_4.png", 0))     #11
    templates.append(cv2.imread("Images\\Shop.png", 0))        #12
    templates.append(cv2.imread("Images\\Sell.png", 0))        #13
    templates.append(cv2.imread("Images\\Sell_0.png", 0))      #14
    templates.append(cv2.imread("Images\\Sell_1.png", 0))      #15
    templates.append(cv2.imread("Images\\Teleport.png", 0))    #16
    templates.append(cv2.imread("Images\\Teleport_0.png", 0))  #17

def GrabCoordinates():
    global hwnd, left, top, right, bot, width, height, centerX, centerY
    left, top, right, bot = win32gui.GetWindowRect(hwnd)
    width, height = right - left, bot - top
    centerX, centerY = left + (width / 2), top + (height / 2)

def MouseClick(x, y, count=1, speed = 0.15, delay=0.1):
    pyautogui.PAUSE = delay
    pyautogui.moveTo(x, y, speed)
    for i in range(count):
        pyautogui.mouseDown()
        pyautogui.mouseUp()
    pyautogui.PAUSE = 0.1

def GrabScreen():
    global hwnd, left, top, right, bot, width, height
    hwindc = win32gui.GetWindowDC(hwnd)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((left, top), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height, width, 4)
    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())
    return img

def DetectImage(templateIndex, threshold = .8, center = False):
    img_rgb = GrabScreen()
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    w, h = templates[templateIndex].shape[::-1]
    res = cv2.matchTemplate(img_gray, templates[templateIndex], cv2.TM_CCOEFF_NORMED)
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        if center:
            return [pt[0] + (w / 2), pt[1] + (h / 2)]
        else:
            return [pt[0], pt[1]]
    return None

def WaitForImageFound(templateIndex, delay = .15, center = False):
    coords = None
    while not(coords):
        coords = DetectImage(templateIndex, center=center)
        time.sleep(delay)
    return coords

def ChatCheck():
    if DetectImage(1):
        pyautogui.press('enter')

def SPDeadCheck(delay = 0.15):
    if DetectImage(3, .97):
        pyautogui.press(edenKey ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by includeBRAIN
close date 2020-04-20 05:27:09.435847

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-14 12:32:15 -0600

mvuori gravatar image

I notice three things: * For every grabbed screen you read the template from disk - slow, just read it once into memory * Doing the operations for a full size window is very slow - resize the grabbed screen to smallest size that still works * You seem to convert the screen from BGR to RGB, but a Windows screen would be RGB

edit flag offensive delete link more

Comments

Thanks, fixed

includeBRAIN gravatar imageincludeBRAIN ( 2020-04-14 13:22:43 -0600 )edit

Does it works?

supra56 gravatar imagesupra56 ( 2020-04-17 21:39:47 -0600 )edit

Yes, why not?

includeBRAIN gravatar imageincludeBRAIN ( 2020-04-18 11:07:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-14 09:44:20 -0600

Seen: 537 times

Last updated: Apr 14 '20