Ask Your Question

Revision history [back]

Help with optimization OpenCV Python

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:

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

    def GrabCoordinates():
        global hwnd, left, top, right, bot, width, height
        left, top, right, bot = win32gui.GetWindowRect(hwnd)
        width, height = right - left, bot - top

    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 cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)

    def DetectImage(templatePath, threshold = .8, center = False):
        img_rgb = GrabScreen()
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(templatePath, 0)
        w, h = template.shape[::-1]
        res = cv2.matchTemplate(img_gray, template, 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(templatePach, delay = .15, center = False):
        coords = None
        while not(coords):
            coords = DetectImage(templatePach, center=center)
            time.sleep(delay)
        return coords

    def ChatCheck():
        if DetectImage("Images\\ChatOpen.png"):
            pyautogui.press('enter')

    def SPDeadCheck(delay=0.15):
        if DetectImage("Images\\LowSP.png", threshold=.97):
            pyautogui.press(edenKey)
        if DetectImage("Images\\Dead.png"):
            pyautogui.press('enter')
            WaitForImageFound("Images\\ChatOpen.png")
            pyautogui.press(['@', 'g', 'o', ' ', 'e', 'd', 'e', 'n', 'enter'], interval=delay)

    def CombatCheck():
        while DetectImage("Images\\InCombat.png"):
            image_hsv = cv2.cvtColor(GrabScreen(), cv2.COLOR_RGB2HSV)
            mask = cv2.inRange(image_hsv, (36, 25, 25), (70, 255, 255))
            ret, thresh = cv2.threshold(mask, 127, 255, 0)
            contours, hierarchy = cv2.findContours(thresh, 1, 2)
            for item in range(len(contours)):
                cnt = contours[item]
                if len(cnt) >= 40:
                    M = cv2.moments(cnt)
                    if M["m00"] != 0:
                        cx = int(M['m10'] / M['m00'])
                        cy = int(M['m01'] / M['m00'])
                        x, y, w, h = cv2.boundingRect(cnt)
                        pyautogui.press(attackKey)
                        MouseClick(x + (w / 2), y + (h / 2))
                        time.sleep(0.2)
            pyautogui.press(teleportKey)
            SPDeadCheck()

Can you help me optimize speed in CombatCheck() before i try to add multithreading? Mb some numpy things or ignore hsv and do it only with gray+threshhold? Also interesting a way to add multithreading. Here is mob image:

image description

Help with optimization OpenCV Python

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.moveTo(x, y, speed)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
    pyautogui.PAUSE = 0.1

 def GrabCoordinates():
    GrabScreen():
    global hwnd, left, top, right, bot, width, height
        left, top, right, bot = win32gui.GetWindowRect(hwnd)
        width, height = right - left, bot - top

    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 cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)

    img

def DetectImage(templatePath, DetectImage(templateIndex, threshold = .8, center = False):
     img_rgb = GrabScreen()
     img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(templatePath, 0)
        w, h = template.shape[::-1]
    templates[templateIndex].shape[::-1]
    res = cv2.matchTemplate(img_gray, template, 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(templatePach, WaitForImageFound(templateIndex, delay = .15, center = False):
     coords = None
     while not(coords):
         coords = DetectImage(templatePach, DetectImage(templateIndex, center=center)
         time.sleep(delay)
     return coords

 def ChatCheck():
     if DetectImage("Images\\ChatOpen.png"):
    DetectImage(1):
        pyautogui.press('enter')

 def SPDeadCheck(delay=0.15):
    SPDeadCheck(delay = 0.15):
    if DetectImage("Images\\LowSP.png", threshold=.97):
    DetectImage(3, .97):
        pyautogui.press(edenKey)
     if DetectImage("Images\\Dead.png"):
    DetectImage(4):
        pyautogui.press('enter')
            WaitForImageFound("Images\\ChatOpen.png")
    WaitForImageFound(1)
        pyautogui.press(['@', 'g', 'o', ' ', 'e', 'd', 'e', 'n', 'enter'], interval=delay)

 def CombatCheck():
    global centerX, centerY
    while DetectImage("Images\\InCombat.png"):
DetectImage(2):
        for i in range(3):  #targets is moving, more checks = more targets
            image_hsv = cv2.cvtColor(GrabScreen(), cv2.COLOR_RGB2HSV)
            mask = cv2.inRange(image_hsv, (36, 25, 25), (70, 255, 255))
            ret, thresh = cv2.threshold(mask, 127, 255, 0)
            contours, hierarchy = cv2.findContours(thresh, 1, 2)
            if (contours):
                for item cnt in range(len(contours)):
                cnt = contours[item]
contours:    #get first target with condition, do things, then break
                    if len(cnt) >= 40:
40:  #mb replace with some func like C# Linq
                        M = cv2.moments(cnt)
                     if M["m00"] != 0:
                         cx = int(M['m10'] / M['m00'])
                         cy = int(M['m01'] / M['m00'])
                         x, y, w, h = cv2.boundingRect(cnt)
                            x += w / 2
                            y += h / 2
                            distance = cv2.norm((x, y), (centerX, centerY))
                            pyautogui.press(attackKey)
                        MouseClick(x     MouseClick(x, y)
                            time.sleep((distance/1000) + (w / 2), y + (h / 2))
                        time.sleep(0.2)
    0.2)   #time for moving to target
                            break       #mobs is moving, old stored positions now uselles
        pyautogui.press(teleportKey)
         SPDeadCheck()

Can you help me optimize speed in CombatCheck() before i try to add multithreading? Mb some numpy things or ignore hsv and do it only with gray+threshhold? Also interesting a way to add multithreading. Here is mob image:

image description