Opt: Template.match_result returns similarity and Button

- Add Button.move() Button.crop()
This commit is contained in:
LmeSzinc 2021-06-30 13:59:24 +08:00
parent 672d459469
commit cf5bfbc9a7
6 changed files with 71 additions and 15 deletions

View File

@ -20,11 +20,9 @@ class CampaignBase(CampaignBase_):
if '-5' not in name:
return super().campaign_get_entrance(name)
sim, point = TEMPLATE_STAGE_SOS.match_result(self.device.image)
sim, button = TEMPLATE_STAGE_SOS.match_result(self.device.image)
if sim < 0.85:
raise CampaignNameError
button = area_offset(area=(-12, -12, 44, 32), offset=point)
color = get_color(self.device.image, button)
entrance = Button(area=button, color=color, button=button, name=name)
entrance = button.crop((-12, -12, 44, 32), image=self.device.image, name=name)
return entrance

View File

@ -27,13 +27,11 @@ class CampaignBase(CampaignBase_):
"""
template = dic_archives_template[name]
sim, point = template.match_result(self.device.image)
sim, button = template.match_result(self.device.image)
if sim < 0.85:
return None
button = area_offset(area=(-12, -12, 44, 32), offset=point)
color = get_color(self.device.image, button)
entrance = Button(area=button, color=color, button=button, name=name)
entrance = button.crop((-12, -12, 44, 32), image=self.device.image, name=name)
return entrance
def _search_archives_entrance(self, name, skip_first_screenshot=True):

View File

@ -148,6 +148,48 @@ class Button:
area = area_offset(self.area, offset=diff)
return color_similar(color1=get_color(image, area), color2=self.color, threshold=threshold)
def crop(self, area, image=None, name=None):
"""
Get a new button by relative coordinates.
Args:
area (tuple):
image: Pillow image. If provided, load color and image from it.
name (str):
Returns:
Button:
"""
if name is None:
name = self.name
new_area = area_offset(area, offset=self.area[:2])
new_button = area_offset(area, offset=self.button[:2])
button = Button(area=new_area, color=self.color, button=new_button, file=self.file, name=name)
if image is not None:
button.load_color(image)
return button
def move(self, vector, image=None, name=None):
"""
Move button.
Args:
vector (tuple):
image: Pillow image. If provided, load color and image from it.
name (str):
Returns:
Button:
"""
if name is None:
name = self.name
new_area = area_offset(self.area, offset=vector)
new_button = area_offset(self.button, offset=vector)
button = Button(area=new_area, color=self.color, button=new_button, file=self.file, name=name)
if image is not None:
button.load_color(image)
return button
class ButtonGrid:
def __init__(self, origin, delta, button_shape, grid_shape, name=None):
@ -185,7 +227,7 @@ class ButtonGrid:
ButtonGrid:
"""
if name is None:
name = self._name + '_CROP'
name = self._name
origin = self.origin + area[:2]
button_shape = np.subtract(area[2:], area[:2])
return ButtonGrid(
@ -201,7 +243,7 @@ class ButtonGrid:
ButtonGrid:
"""
if name is None:
name = self._name + '_MOVE'
name = self._name
origin = self.origin + vector
return ButtonGrid(
origin=origin, delta=self.delta, button_shape=self.button_shape, grid_shape=self.grid_shape, name=name)

View File

@ -1,12 +1,13 @@
import os
import cv2
import imageio
import numpy as np
from PIL import Image
import module.config.server as server
from module.base.button import Button
from module.base.decorator import cached_property
from module.base.utils import *
class Template:
@ -76,12 +77,16 @@ class Template:
image:
Returns:
bool: If matches.
float: Similarity
Button:
"""
res = cv2.matchTemplate(np.array(image), self.image, cv2.TM_CCOEFF_NORMED)
_, sim, _, point = cv2.minMaxLoc(res)
# print(self.file, sim)
return sim, point
area = area_offset(area=(0, 0, *self.size), offset=point)
button = Button(area=area, color=get_color(image, area), button=area, name=f'MATCH_RESULT')
return sim, button
def match_multi(self, image, similarity=0.85):
"""

View File

@ -140,6 +140,19 @@ def area_limit(area1, area2):
return (max(area1[0], area2[0]), max(area1[1], area2[1]), min(area1[2], area2[2]), min(area1[3], area2[3]))
def area_size(area):
"""
Area size or shape.
Args:
area (tuple): (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
Returns:
tuple: (x, y).
"""
return (area[2] - area[0], area[3] - area[1])
def point_limit(point, area):
"""
Limit point in an area.

View File

@ -63,9 +63,9 @@ class Combat(Level, HPBalancer, Retirement, SubmarineCall, CombatAuto, CombatMan
Returns:
bool:
"""
similarity, location = TEMPLATE_COMBAT_LOADING.match_result(self.device.image.crop((0, 620, 1280, 720)))
similarity, button = TEMPLATE_COMBAT_LOADING.match_result(self.device.image.crop((0, 620, 1280, 720)))
if similarity > 0.85:
loading = (location[0] + 38 - LOADING_BAR.area[0]) / (LOADING_BAR.area[2] - LOADING_BAR.area[0])
loading = (button.area[0] + 38 - LOADING_BAR.area[0]) / (LOADING_BAR.area[2] - LOADING_BAR.area[0])
logger.attr('Loading', f'{int(loading * 100)}%')
return True
else: