Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

Help with optimization OpenCV Python

$
0
0
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](/upfiles/15868750789878317.png)

Viewing all articles
Browse latest Browse all 2088

Trending Articles