def detect(self, image): """ Args: image: Screenshot. Returns: bool: If success. """ start_time = time.time() image = np.array(image) self.image = image # Image initialization image = rgb2gray(crop(image, self.config.DETECTING_AREA)) # Perspective transform image_trans = cv2.warpPerspective(image, self.homo_data, self.homo_size) # Edge detection image_edge = cv2.Canny(image_trans, 100, 150) image_edge = cv2.bitwise_and(image_edge, self.ui_mask_homo_stroke) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) image_edge = cv2.morphologyEx(image_edge, cv2.MORPH_CLOSE, kernel) # Image.fromarray(image_edge, mode='L').show() # Find free tile if self.search_tile_center( image_edge, threshold_good=self.config.HOMO_CENTER_GOOD_THRESHOLD, threshold=self.config.HOMO_CENTER_THRESHOLD): pass elif self.search_tile_corner( image_edge, threshold=self.config.HOMO_CORNER_THRESHOLD): pass elif self.search_tile_rectangle( image_edge, threshold=self.config.HOMO_RECTANGLE_THRESHOLD): pass else: raise MapDetectionError('Failed to find a free tile') self.homo_loca %= self.config.HOMO_TILE # Detect map edges image_edge = cv2.bitwise_and( cv2.dilate(image_edge, kernel), cv2.inRange(image_trans, *self.config.HOMO_EDGE_COLOR_RANGE)) self.detect_edges(image_edge, hough_th=self.config.HOMO_EDGE_HOUGHLINES_THRESHOLD) # Log time_cost = round(time.time() - start_time, 3) logger.info('%ss %s edge_lines: %s hori, %s vert' % (float2str(time_cost), '_' if self.lower_edge else ' ', self._map_edge_count[1], self._map_edge_count[0])) logger.info( 'Edges: %s%s%s homo_loca: %s' % ('/' if self.left_edge else ' ', '_' if self.upper_edge else ' ', '\\' if self.right_edge else ' ', point2str(*self.homo_loca, length=3)))
def load_image(self, image): """Method that turns image to monochrome and hide UI. Args: image: Screenshot. Returns: np.ndarray """ image = rgb2gray(crop(image, self.config.DETECTING_AREA)) image = cv2.subtract(255, cv2.bitwise_and(image, ASSETS.ui_mask)) return image
def unpack(image): """ Split images vertically. Args: image: Returns: list: List of np.ndarray. """ size = image_size(image) if size == (1280, 720): return [image] else: if size[0] != 1280 or size[1] % 720 != 0: raise ImageError(f'Unexpected image size: {size}') return [crop(image, (0, n * 720, 1280, (n + 1) * 720)) for n in range(size[1] // 720)]
def get_research_status(self, image): """ Args: image: Screenshot Returns: list[str]: List of project status """ out = [] for index, status, scaling in zip(range(5), RESEARCH_STATUS, TEMPLATE_SCALING): info = status.crop((0, -40, 200, 0)) piece = rgb2gray(crop(image, info.area)) if TEMPLATE_WAITING.match(piece, scaling=scaling): out.append('waiting') elif TEMPLATE_RUNNING.match(piece, scaling=scaling): out.append('running') elif TEMPLATE_DETAIL.match(piece, scaling=scaling): out.append('detail') else: out.append('unknown') logger.info(f'Research status: {out}') return out
def ui_mask_in_map(self): DETECTING_AREA = (123, 55, 1280, 720) area = np.append(np.subtract(0, DETECTING_AREA[:2]), self.ui_mask.shape[::-1]) # area = (-123, -55, 1157, 665) return crop(self.ui_mask, area)
def ui_mask_os_in_map(self): area = np.append(np.subtract(0, DETECTING_AREA[:2]), self.ui_mask.shape[::-1]) # area = (-123, -55, 1157, 665) return crop(UI_MASK_OS.image, area)
def handle_user_agreement(self, xp, hierarchy): """ For CN only. CN client is bugged. User Agreement and Privacy Policy may popup again even you have agreed with it. This method scrolls to the bottom and click AGREE. Returns: bool: If handled. """ if server.server == 'cn': area_wait_results = self.get_for_any_ele([ XPS('//*[@text="sdk协议"]', xp, hierarchy), XPS('//*[@content-desc="sdk协议"]', xp, hierarchy) ]) if area_wait_results is False: return False agree_wait_results = self.get_for_any_ele([ XPS('//*[@text="同意"]', xp, hierarchy), XPS('//*[@content-desc="同意"]', xp, hierarchy) ]) start_padding_results = self.get_for_any_ele([ XPS('//*[@text="隐私政策"]', xp, hierarchy), XPS('//*[@content-desc="隐私政策"]', xp, hierarchy), XPS('//*[@text="用户协议"]', xp, hierarchy), XPS('//*[@content-desc="用户协议"]', xp, hierarchy) ]) start_margin_results = self.get_for_any_ele([ XPS('//*[@text="请滑动阅读协议内容"]', xp, hierarchy), XPS('//*[@content-desc="请滑动阅读协议内容"]', xp, hierarchy) ]) test_image_original = self.device.image image_handle_crop = crop( test_image_original, (start_padding_results[2], 0, start_margin_results[2], 720)) # Image.fromarray(image_handle_crop).show() sims = color_similarity_2d(image_handle_crop, color=(182, 189, 202)) points = np.sum(sims >= 255) if points == 0: return False sims_height = np.mean(sims, axis=1) # pyplot.plot(sims_height, color='r') # pyplot.show() peaks, __ = find_peaks(sims_height, height=225) if len(peaks) == 2: peaks = (peaks[0] + peaks[1]) / 2 start_pos = [ (start_padding_results[2] + start_margin_results[2]) / 2, float(peaks) ] end_pos = [ (start_padding_results[2] + start_margin_results[2]) / 2, area_wait_results[3] ] logger.info("user agreement position find result: " + ', '.join('%.2f' % _ for _ in start_pos)) logger.info("user agreement area expect: " + 'x:963-973, y:259-279') self.device.drag(start_pos, end_pos, segments=2, shake=(0, 25), point_random=(0, 0, 0, 0), shake_random=(0, -5, 0, 5)) AGREE = Button(area=agree_wait_results, color=(), button=agree_wait_results, name='AGREE') self.device.click(AGREE) return True