Add: 增加了未通关的章节名的识别

- ocr预处理时会将图片左对齐
This commit is contained in:
LmeSzinc 2020-04-18 21:19:15 +08:00
parent 0663ee3d63
commit a8a243052f
7 changed files with 34 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -90,6 +90,14 @@ class Ocr:
# Resize to input size.
size = (int(image.shape[1] / image.shape[0] * image_shape[1]), image_shape[1])
image = cv2.resize(image, size, interpolation=cv2.INTER_LINEAR)
# Left align
x = np.where(np.mean(image, axis=0) < 220)[0]
if len(x):
x = x[0] - 2 if x[0] - 2 >= 2 else 0
image = image[:, x:]
# Pad to image_shape=(280, 32)
diff_x = image_shape[0] - image.shape[1]
if diff_x > 0:
image = np.pad(image, ((0, 0), (0, diff_x)), mode='constant', constant_values=255)

View File

@ -39,6 +39,7 @@ class CampaignBase(Map):
def run(self):
logger.hr(self.ENTRANCE, level=2)
self.handle_spare_fleet()
self.ENTRANCE.area = self.ENTRANCE.button
self.enter_map(self.ENTRANCE, mode=self.config.CAMPAIGN_MODE)
self.handle_map_fleet_lock()
self.handle_fleet_reverse()

View File

@ -5,9 +5,10 @@ import numpy as np
from module.base.ocr import Ocr
from module.base.utils import extract_letters, area_offset
from module.logger import logger
from module.template.assets import TEMPLATE_STAGE_CLEAR, Button
from module.template.assets import TEMPLATE_STAGE_CLEAR, TEMPLATE_STAGE_PERCENT, Button
stage_clear_color = np.mean(np.mean(TEMPLATE_STAGE_CLEAR.image, axis=0), axis=0)
stage_clear_color = tuple(np.mean(np.mean(TEMPLATE_STAGE_CLEAR.image, axis=0), axis=0))
stage_percentage_color = tuple(np.mean(np.mean(TEMPLATE_STAGE_PERCENT.image, axis=0), axis=0))
def ensure_chapter_index(name):
@ -56,33 +57,40 @@ class CampaignOcr:
# 0.8705039 , 0.99954903, 0.99983317, 0.99996626, 1. ],
# dtype=float32)
if result is None or len(result) == 0:
logger.warning('No stage clear image found.')
name_offset = (77, 9)
name_offset = (75, 9)
name_size = (60, 16)
name_letter = (255, 255, 255)
name_back = (102, 102, 102)
digits = []
for point in result:
point = point[::-1]
button = tuple(np.append(point, point + TEMPLATE_STAGE_CLEAR.image.shape[:2]))
button = tuple(np.append(point, point + TEMPLATE_STAGE_CLEAR.image.shape[:2][::-1]))
point = point + name_offset
name = image.crop(np.append(point, point + name_size))
name = extract_letters(name, letter=name_letter, back=name_back)
stage = self.extract_stage_name(name)
digits.append(Button(area=area_offset(stage, point), color=stage_clear_color, button=button, name='stage'))
# chapter, stage = self.name_separate(name)
# color = TEMPLATE_STAGE_CLEAR.color
# digits.append(Button(area=area_offset(chapter, point), color=color, button=button, name='chapter'))
# digits.append(Button(area=area_offset(stage, point), color=color, button=button, name='stage'))
result = TEMPLATE_STAGE_PERCENT.match_multi(image, similarity=0.95)
name_offset = (50, 0)
for point in result:
point = point[::-1]
button = tuple(np.append(point, point + TEMPLATE_STAGE_PERCENT.image.shape[:2][::-1]))
point = point + name_offset
name = image.crop(np.append(point, point + name_size))
name = extract_letters(name, letter=name_letter, back=name_back)
stage = self.extract_stage_name(name)
digits.append(
Button(area=area_offset(stage, point), color=stage_percentage_color, button=button, name='stage'))
if len(digits) == 0:
logger.warning('No stage found.')
return digits
@staticmethod
def extract_stage_name(image):
x_skip = 2
x_skip = 7
interval = 5
x_color = np.convolve(np.mean(image, axis=0), np.ones(interval), 'valid') / interval
x_list = np.where(x_color[x_skip:] > 235)[0]
@ -128,9 +136,11 @@ class CampaignOcr:
def get_stage_name(self, image):
self.stage = {}
buttons = self.extract_campaign_name_image(image)
# ocr = Digit(buttons)
ocr = Ocr(buttons, lang='stage')
result = ocr.ocr(image)
if not isinstance(result, list):
result = [result]
result = [res.lower().replace('--', '-') for res in result]
chapter = [separate_name(res)[0] for res in result]
@ -142,12 +152,6 @@ class CampaignOcr:
button.name = name
self.stage[name] = button
# self.chapter = np.argmax(np.bincount(result[::2]))
# for stage, button in zip(result[1::2], buttons[1::2]):
# button.area = button.button
# button.name = f'STAGE_{self.chapter}_{stage}'
# self.stage[f'{self.chapter}-{stage}'] = button
logger.attr('Chapter', self.chapter)
logger.attr('Stage', ', '.join(self.stage.keys()))

View File

@ -29,6 +29,7 @@ MAP_AIR_RAID = Button(area=(350, 447, 1280, 472), color=(154, 43, 46), button=(3
MAP_AMBUSH = Button(area=(261, 433, 1280, 449), color=(161, 41, 43), button=(261, 433, 1280, 449), file='./assets/handler/MAP_AMBUSH.png')
MAP_AMBUSH_EVADE = Button(area=(325, 393, 1280, 395), color=(255, 255, 255), button=(979, 444, 1152, 502), file='./assets/handler/MAP_AMBUSH_EVADE.png')
MAP_BUFF = Button(area=(145, 115, 437, 159), color=(103, 118, 118), button=(145, 115, 437, 159), file='./assets/handler/MAP_BUFF.png')
MAP_CLEAR_PERCENTAGE = Button(area=(626, 185, 970, 190), color=(245, 213, 88), button=(626, 185, 970, 190), file='./assets/handler/MAP_CLEAR_PERCENTAGE.png')
MAP_ENEMY_SEARCHING = Button(area=(531, 320, 864, 382), color=(200, 99, 91), button=(531, 320, 864, 382), file='./assets/handler/MAP_ENEMY_SEARCHING.png')
MAP_GREEN = Button(area=(195, 260, 349, 292), color=(125, 190, 84), button=(195, 260, 349, 292), file='./assets/handler/MAP_GREEN.png')
MAP_STAR_1 = Button(area=(245, 377, 254, 384), color=(251, 233, 143), button=(245, 377, 254, 384), file='./assets/handler/MAP_STAR_1.png')

View File

@ -15,3 +15,4 @@ TEMPLATE_FORMATION_1 = Template(file='./assets/template/TEMPLATE_FORMATION_1.png
TEMPLATE_FORMATION_2 = Template(file='./assets/template/TEMPLATE_FORMATION_2.png')
TEMPLATE_FORMATION_3 = Template(file='./assets/template/TEMPLATE_FORMATION_3.png')
TEMPLATE_STAGE_CLEAR = Template(file='./assets/template/TEMPLATE_STAGE_CLEAR.png')
TEMPLATE_STAGE_PERCENT = Template(file='./assets/template/TEMPLATE_STAGE_PERCENT.png')