1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-09 18:39:04 +08:00

Upd: resize image in campaign bonus

This commit is contained in:
sui-feng-cb 2025-12-11 23:25:44 +08:00
parent 2dc14708f1
commit 45c1b50605
3 changed files with 57 additions and 22 deletions

View File

@ -25,8 +25,8 @@ class CampaignBonusStatistics(GetItemsStatistics):
if CAMPAIGN_BONUS_STRATEGY_CHECK.match(image, offset=(200, 500), similarity=similarity):
return False
if AUTO_SEARCH_MENU_EXIT.match(image, offset=(200, 20), similarity=similarity) \
and (CAMPAIGN_BONUS.match(image, offset=(200, 500), similarity=similarity) \
and CAMPAIGN_BONUS_SINGLE.match(image, offset=(200, 500), similarity=similarity)):
and CAMPAIGN_BONUS_SINGLE.match(image, offset=(200, 100), similarity=similarity) \
and CAMPAIGN_BONUS.match(image, offset=(200, 500), similarity=similarity):
return True
return False

View File

@ -24,6 +24,7 @@ class DropStatistics:
TEMPLATE_FOLDER = 'item_templates'
TEMPLATE_BASIC = './assets/stats_basic'
SKIP_IMAGE_FOLDER = 'skip_images'
IMAGE_EXTENSION = '.png'
CNOCR_CONTEXT = 'cpu'
CSV_FILE = 'drop_result.csv'
CSV_OVERWRITE = True
@ -94,6 +95,8 @@ class DropStatistics:
"""
Move a image file to {SKIP_IMAGE_FOLDER}/{CAMPAIGN}.
"""
if not self.SKIP_IMAGE_FOLDER:
return False
campaign = os.path.basename(os.path.abspath(os.path.join(file, '../')))
folder = self.skip_file_folder(campaign)
os.makedirs(folder, exist_ok=True)
@ -104,10 +107,10 @@ class DropStatistics:
Extract template from a single file.
New templates will be given an auto-increased ID.
"""
image = load_image(file)
similarity = get_similarity(image)
images = unpack(image)[-1::]
for image in images:
images = unpack(load_image(file))[-1::]
similarities = [get_similarity(image) for image in images]
images = [resize_image(image) for image in images]
for image, similarity in zip(images, similarities):
# if self.get_items.appear_on(image):
# self.get_items.extract_template(image, folder=self.template_folder)
if self.campaign_bonus.appear_on(image, similarity=similarity):
@ -129,11 +132,11 @@ class DropStatistics:
"""
ts = os.path.splitext(os.path.basename(file))[0]
campaign = os.path.basename(os.path.abspath(os.path.join(file, '../')))
image = load_image(file)
similarity = get_similarity(image)
images = unpack(image)[-1::]
images = unpack(load_image(file))[-1::]
similarities = [get_similarity(image) for image in images]
images = [resize_image(image) for image in images]
enemy_name = 'unknown'
for image in images:
for image, similarity in zip(images, similarities):
# if self.battle_status.appear_on(image):
# enemy_name = self.battle_status.stats_battle_status(image)
# if self.get_items.appear_on(image):
@ -157,7 +160,8 @@ 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), ext=['.png', '.jpg']).items()):
drop_folder = load_folder(self.drop_folder(campaign), ext=DropStatistics.IMAGE_EXTENSION)
for ts, file in tqdm(drop_folder.items()):
try:
self.parse_template(file)
except ImageError as e:
@ -184,7 +188,8 @@ 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), ext=['.png', '.jpg']).items()):
drop_folder = load_folder(self.drop_folder(campaign), ext=DropStatistics.IMAGE_EXTENSION)
for ts, file in tqdm(drop_folder.items()):
try:
rows = list(self.parse_drop(file))
writer.writerows(rows)
@ -210,6 +215,8 @@ if __name__ == '__main__':
# This will save images {DROP_FOLDER}/{SKIP_IMAGE_FOLDER}/{CAMPAIGN}.
# If folder doesn't exist, auto create
DropStatistics.SKIP_IMAGE_FOLDER = 'skip_images'
# image file extension suck as '.png', '.jpg'
DropStatistics.IMAGE_EXTENSION = ['.png', '.jpg', '.PNG']
# 'cpu' or 'gpu', default to 'cpu'.
# Use 'gpu' for faster prediction, but you must have the gpu version of mxnet installed.
DropStatistics.CNOCR_CONTEXT = 'cpu'

View File

@ -66,18 +66,48 @@ def unpack(image):
list: List of np.ndarray.
"""
size = image_size(image)
if size == (1280, 720):
if size == (1280, 720) or size[0] == round(size[1] * 16 / 9) \
or size[0] != 1280 or size[1] % 720 != 0:
return [image]
elif size[0] / 1280 == size[1] / 720:
return [cv2.resize(image, (1280, 720), interpolation=cv2.INTER_LANCZOS4)]
else:
if size[0] != 1280 or size[1] % 720 != 0:
raise ImageInvalidResolution(f'Unexpected image size: {size}')
return [crop(image, (0, n * 720, 1280, (n + 1) * 720)) for n in range(size[1] // 720)]
def resize_image(image):
"""
Crop and resize to 1280x720.
Args:
image:
Returns:
np.ndarray:
"""
size = image_size(image)
width, height = size
if size == (1280, 720):
return image
elif width == round(height * 16 / 9):
return cv2.resize(image, (1280, 720), interpolation=cv2.INTER_LANCZOS4)
elif width != 1280 or height % 720 != 0:
if width / height < 16 / 9:
crop_height = width * 9 / 16
y1 = round(height / 2 - crop_height / 2)
y2 = round(height / 2 + crop_height / 2)
crop_img = crop(image, (0, y1, width, y2))
else:
crop_width = height * 16 / 9
x1 = round(width / 2 - crop_width / 2)
x2 = round(width / 2 + crop_width / 2)
crop_img = crop(image, (x1, 0, x2, height))
return cv2.resize(crop_img, (1280, 720), interpolation=cv2.INTER_LANCZOS4)
else:
raise ImageInvalidResolution(f'Unexpected image size: {size}')
def get_similarity(image):
"""
Get similarity to.
Get similarity from a image.
Args:
image:
@ -88,9 +118,7 @@ def get_similarity(image):
size = image_size(image)
if size == (1280, 720):
return 0.85
elif size[0] / 1280 == size[1] / 720:
return 0.7
elif size[0] == round(size[1] * 16 / 9) or size[0] != 1280 or size[1] % 720 != 0:
return 0.69
else:
if size[0] != 1280 or size[1] % 720 != 0:
raise ImageInvalidResolution(f'Unexpected image size: {size}')
return 0.85