def get_position_count(self): area = cv.extract_region_from_image( self.current_frame.grayscale_frame, self.game.screen_regions["POSITIONS"] ) self.visual_debugger.store_image_data( area, area.shape, 4 ) text = tesserocr.image_to_text(Image.fromarray(area)) # print(text) if "Flat" in text: return 0 matches = re.findall("\d", text) # print(matches) position = 0 if len(matches) > 0: for match in matches: stripped = match.strip() if len(stripped) > 0: position = int(stripped) break # print('position: %d' % position) return position
def get_pl(self): area = cv.extract_region_from_image( self.current_frame.grayscale_frame, self.game.screen_regions["PL"] ) self.visual_debugger.store_image_data( area, area.shape, 3 ) text = tesserocr.image_to_text(Image.fromarray(area), psm=tesserocr.PSM.SINGLE_LINE) # print(text) matches = re.findall("-?\d*", text) # print(matches) if len(matches) > 0: c = '' for match in matches: stripped = match.strip() c = c + stripped if len(c) > 0: c = re.sub("(?<=\d)\D",'',c) return int(c) print('DID NOT FIND PL') return 0
def get_text(self, region, game_frame): area = cv.extract_region_from_image( game_frame.grayscale_frame, self.game.screen_regions[region] ) return tesserocr.image_to_text(Image.fromarray(area))
def get_position_and_fill_count(self): area = cv.extract_region_from_image( self.current_frame.grayscale_frame, self.game.screen_regions["POSITIONS"]) self.visual_debugger.store_image_data(area, area.shape, 4) text = tesserocr.image_to_text(Image.fromarray(area)) matches = re.findall("\d*\s", text) # print(matches) position = 0 if len(matches) > 0: for match in matches: stripped = match.strip() if len(stripped) > 0: position = int(stripped) fills = 0 matches = re.findall("\(.*-", text) if len(matches) > 0: for match in matches: if len(match) > 0: numbers = re.findall("\d*", match) for number in numbers: if len(number) > 0: fills = int(number) return (position, fills)
def extract_game_area(self, frame_buffer): game_area_buffer = [] for game_frame in frame_buffer.frames: game_area = cv.extract_region_from_image( game_frame.grayscale_frame, self.game.screen_regions["GAME_REGION"]) frame = FrameTransformer.rescale(game_area, 0.5) game_area_buffer.append(frame) return game_area_buffer
def get_market_change(self): area = cv.extract_region_from_image( self.current_frame.grayscale_frame, self.game.screen_regions["MARKET_CHANGE"]) text = tesserocr.image_to_text(Image.fromarray(area)) matches = re.findall("\d*", text) for match in matches: if len(match) > 0: return int(match.strip()) return -1
def get_working_sells(self): area = cv.extract_region_from_image( self.current_frame.grayscale_frame, self.game.screen_regions["WORKING_SELLS"]) self.visual_debugger.store_image_data(area, area.shape, 2) text = tesserocr.image_to_text(Image.fromarray(area)) matches = re.findall("\(\d\)", text) for match in matches: if len(match) > 0: return int(match.strip(' ()')) return -1
def _build_game_matrix(self, frame): matrix = [["", "", "", ""],["", "", "", ""],["", "", "", ""],["", "", "", ""]] num_mat = np.zeros(shape=(4,4), dtype=np.uint16) for rows in range(4): for cols in range(4): current_tile = cv.extract_region_from_image(frame.frame, self.game.screen_regions["TILES"]["TILE_"+str(rows*4+cols)]) match_color = '#%02x%02x%02x' % tuple(current_tile[5, 5, :]) if match_color in self.game.styles: matrix[rows][cols] = list(self.game.point_values.keys())[self.game.styles.index(match_color)] if matrix[rows][cols] != 'blank': num_mat[rows, cols] = int(matrix[rows][cols]) else: return False, False return matrix, num_mat
def _get_ocr_score(self, frame): ''' DEPRECATED: Use get_score(); there is no need to OCR as the score can be constructed from the matrix directly ''' score_region = cv.extract_region_from_image(frame.grayscale_frame, self.game.screen_regions["CURRENT_SCORE"]) images, text_regions = extract_ocr_candidates(score_region) if len(images) == 0: return None ocr = perform_ocr(images[0], scale=15) ocr_cand = ocr.replace(' ', '') try: ocr_int = int(ocr_cand) except ValueError: ocr_int = None return ocr_int
def _is_game_over(self, frame): score_region = cv.extract_region_from_image(frame.grayscale_frame, self.game.screen_regions["CURRENT_SCORE"]) if np.all(score_region == score_region[0][0]): return True else: return False