1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-12 12:08:21 +08:00

Upd: threshold for predict_valid in campaign_bonus

This commit is contained in:
sui-feng-cb
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):
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):

View File

@@ -120,7 +120,7 @@ class DropStatistics:
if self.campaign_bonus.appear_on(image, similarity=similarity):
for button in [CAMPAIGN_BONUS_SINGLE, CAMPAIGN_BONUS]:
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]
else:
raise ImageError('No campaign bonus on image.')
@@ -135,7 +135,7 @@ class DropStatistics:
print('')
logger.hr(f'Extract templates from {campaign}', level=1)
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:
self.parse_template(file)
except ImageError as e:
@@ -160,7 +160,7 @@ class DropStatistics:
with open(self.csv_file, 'a', newline='', encoding=DropStatistics.CSV_ENCODING) as 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:
rows = list(self.parse_drop(file))
writer.writerows(rows)

View File

@@ -217,13 +217,15 @@ class ItemGrid:
self.next_cost_template_index += 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.
Args:
image (np.ndarray):
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:
str: Template name.
@@ -234,7 +236,10 @@ class ItemGrid:
# Match frequently hit templates first
names = np.array(list(self.templates.keys()))[np.argsort(list(self.templates_hit.values()))][::-1]
# 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:
if color_similar(color1=color, color2=self.colors[name], threshold=30):
res = cv2.matchTemplate(image, self.templates[name], cv2.TM_CCOEFF_NORMED)
@@ -243,6 +248,8 @@ class ItemGrid:
self.templates_hit[name] += 1
return name
if mode == 'known':
return 'unknown'
self.next_template_index += 1
name = str(self.next_template_index)
logger.info(f'New template: {name}')
@@ -334,7 +341,7 @@ class ItemGrid:
else:
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:
image (np.ndarray):
@@ -343,6 +350,7 @@ class ItemGrid:
cost (bool): If predict the cost to buy item.
price (bool): If predict item price.
tag (bool): If predict item tag. Tags are like `catchup`, `bonus`.
mode (str): 'all' or 'known'
Returns:
list[Item]:
@@ -354,7 +362,7 @@ class ItemGrid:
for item, a in zip(self.items, amount_list):
item.amount = a
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):
item.name = n
if cost: