Help with optimization OpenCV Python [closed]
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 ...