1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-04-13 02:25:51 +08:00

Upd: threshold for predict_valid in campaign_bonus

This commit is contained in:
2025-12-04 19:23:50 +08:00
parent fa44b728bc
commit 7bd0413610
3 changed files with 22 additions and 8 deletions

View File

@@ -9,7 +9,13 @@ from module.statistics.utils import *
class BonusItem(Item): class BonusItem(Item):
def predict_valid(self): def predict_valid(self):
return np.mean(rgb2gray(self.image) > 160) > 0.3 image = rgb2gray(self.image)
width, height = image_size(image)
wm, hm = width // 2, height // 2
row_center = image[hm: hm + 5, :]
info_bar = image[64: 69, :]
column_center = image[:, wm: wm + 5]
return all(np.mean(img > 160) > 0.1 for img in [image, row_center, info_bar, column_center])
class CampaignBonusStatistics(GetItemsStatistics): class CampaignBonusStatistics(GetItemsStatistics):

View File

@@ -120,7 +120,7 @@ class DropStatistics:
if self.campaign_bonus.appear_on(image, similarity=similarity): if self.campaign_bonus.appear_on(image, similarity=similarity):
for button in [CAMPAIGN_BONUS_SINGLE, CAMPAIGN_BONUS]: for button in [CAMPAIGN_BONUS_SINGLE, CAMPAIGN_BONUS]:
self.campaign_bonus.bonus_button = button self.campaign_bonus.bonus_button = button
for item in self.campaign_bonus.stats_get_items(image): for item in self.campaign_bonus.stats_get_items(image, mode='known'):
yield [ts, campaign, enemy_name, button.name, item.name, item.amount] yield [ts, campaign, enemy_name, button.name, item.name, item.amount]
else: else:
raise ImageError('No campaign bonus on image.') raise ImageError('No campaign bonus on image.')
@@ -135,7 +135,7 @@ class DropStatistics:
print('') print('')
logger.hr(f'Extract templates from {campaign}', level=1) logger.hr(f'Extract templates from {campaign}', level=1)
self.check_server(campaign) self.check_server(campaign)
for ts, file in tqdm(load_folder(self.drop_folder(campaign)).items()): for ts, file in tqdm(load_folder(self.drop_folder(campaign), ext=['.png', '.jpg']).items()):
try: try:
self.parse_template(file) self.parse_template(file)
except ImageError as e: except ImageError as e:
@@ -160,7 +160,7 @@ class DropStatistics:
with open(self.csv_file, 'a', newline='', encoding=DropStatistics.CSV_ENCODING) as csv_file: with open(self.csv_file, 'a', newline='', encoding=DropStatistics.CSV_ENCODING) as csv_file:
writer = csv.writer(csv_file) writer = csv.writer(csv_file)
for ts, file in tqdm(load_folder(self.drop_folder(campaign)).items()): for ts, file in tqdm(load_folder(self.drop_folder(campaign), ext=['.png', '.jpg']).items()):
try: try:
rows = list(self.parse_drop(file)) rows = list(self.parse_drop(file))
writer.writerows(rows) writer.writerows(rows)

View File

@@ -217,13 +217,15 @@ class ItemGrid:
self.next_cost_template_index += 1 self.next_cost_template_index += 1
self.next_cost_template_index = max(self.next_cost_template_index, max_digit + 1) self.next_cost_template_index = max(self.next_cost_template_index, max_digit + 1)
def match_template(self, image, similarity=None): def match_template(self, image, similarity=None, mode='all'):
""" """
Match templates, try most frequent hit templates first. Match templates, try most frequent hit templates first.
Args: Args:
image (np.ndarray): image (np.ndarray):
similarity (float): similarity (float):
mode (str): 'all' for all templates, may create new templates
'known' for known templates only, which will skip images named with digit
Returns: Returns:
str: Template name. str: Template name.
@@ -234,7 +236,10 @@ class ItemGrid:
# Match frequently hit templates first # Match frequently hit templates first
names = np.array(list(self.templates.keys()))[np.argsort(list(self.templates_hit.values()))][::-1] names = np.array(list(self.templates.keys()))[np.argsort(list(self.templates_hit.values()))][::-1]
# Match known templates first # Match known templates first
names = [name for name in names if not name.isdigit()] + [name for name in names if name.isdigit()] tmp = [name for name in names if not name.isdigit()]
if mode == 'all':
tmp.extend([name for name in names if name.isdigit()])
names = tmp
for name in names: for name in names:
if color_similar(color1=color, color2=self.colors[name], threshold=30): if color_similar(color1=color, color2=self.colors[name], threshold=30):
res = cv2.matchTemplate(image, self.templates[name], cv2.TM_CCOEFF_NORMED) res = cv2.matchTemplate(image, self.templates[name], cv2.TM_CCOEFF_NORMED)
@@ -243,6 +248,8 @@ class ItemGrid:
self.templates_hit[name] += 1 self.templates_hit[name] += 1
return name return name
if mode == 'known':
return 'unknown'
self.next_template_index += 1 self.next_template_index += 1
name = str(self.next_template_index) name = str(self.next_template_index)
logger.info(f'New template: {name}') logger.info(f'New template: {name}')
@@ -334,7 +341,7 @@ class ItemGrid:
else: else:
return None return None
def predict(self, image, name=True, amount=True, cost=False, price=False, tag=False): def predict(self, image, name=True, amount=True, cost=False, price=False, tag=False, mode='all'):
""" """
Args: Args:
image (np.ndarray): image (np.ndarray):
@@ -343,6 +350,7 @@ class ItemGrid:
cost (bool): If predict the cost to buy item. cost (bool): If predict the cost to buy item.
price (bool): If predict item price. price (bool): If predict item price.
tag (bool): If predict item tag. Tags are like `catchup`, `bonus`. tag (bool): If predict item tag. Tags are like `catchup`, `bonus`.
mode (str): 'all' or 'known'
Returns: Returns:
list[Item]: list[Item]:
@@ -354,7 +362,7 @@ class ItemGrid:
for item, a in zip(self.items, amount_list): for item, a in zip(self.items, amount_list):
item.amount = a item.amount = a
if name: if name:
name_list = [self.match_template(item.image) for item in self.items] name_list = [self.match_template(item.image, mode=mode) for item in self.items]
for item, n in zip(self.items, name_list): for item, n in zip(self.items, name_list):
item.name = n item.name = n
if cost: if cost: