Esempio n. 1
0
 def _view_init(self):
     if not hasattr(self, 'view'):
         storage = ((10, 7), [(110.307, 103.657), (1012.311, 103.657),
                              (-32.959, 600.567), (1113.057, 600.567)])
         view = View(self.config, mode='os', grid_class=OSGrid)
         view.detector_set_backend('homography')
         view.backend.load_homography(storage=storage)
         self.view = view
Esempio n. 2
0
    def update(self, camera=True):
        """Update map image

        Args:
            camera: True to update camera position and perspective data.
        """
        self.device.screenshot()
        if not camera:
            self.view.update(image=self.device.image)
            return True

        if not hasattr(self, 'view'):
            self.view = View(self.config)
        try:
            self.view.load(self.device.image)
        except MapDetectionError as e:
            if self.info_bar_count():
                logger.info('Perspective error cause by info bar. Waiting.')
                self.handle_info_bar()
                return self.update(camera=camera)
            elif self.is_in_stage():
                logger.warning('Image is in stage')
                raise CampaignEnd('Image is in stage')
            elif not self.appear(IN_MAP):
                logger.warning('Image to detect is not in_map')
                raise e
            elif 'Camera outside map' in str(e):
                string = str(e)
                logger.warning(string)
                x, y = string.split('=')[1].strip('() ').split(',')
                self._map_swipe((-int(x.strip()), -int(y.strip())))
            else:
                raise e

        if not self._correct_camera:
            self.show_camera()
            return False
        # Set camera position
        if self.view.left_edge:
            x = 0 + self.view.center_loca[0]
        elif self.view.right_edge:
            x = self.map.shape[0] - self.view.shape[0] + self.view.center_loca[0]
        else:
            x = self.camera[0]
        if self.view.lower_edge:
            y = 0 + self.view.center_loca[1]
        elif self.view.upper_edge:
            y = self.map.shape[1] - self.view.shape[1] + self.view.center_loca[1]
        else:
            y = self.camera[1]

        if self.camera != (x, y):
            logger.attr_align('camera_corrected', f'{location2node(self.camera)} -> {location2node((x, y))}')
        self.camera = (x, y)
        self.show_camera()
Esempio n. 3
0
    def os_default_view(self):
        """
        Returns:
            View:
        """
        def empty(*args, **kwargs):
            pass

        storage = ((10, 7), [(110.307, 103.657), (1012.311, 103.657),
                             (-32.959, 600.567), (1113.057, 600.567)])
        view = View(self.config, mode='os')
        view.detector_set_backend('homography')
        view.backend.load_homography(storage=storage)
        view.backend.load = empty
        view.backend.left_edge = None
        view.backend.right_edge = None
        view.backend.upper_edge = None
        view.backend.lower_edge = None
        view.backend.homo_loca = (53, 60)
        view.load(self.device.image)

        return view
    NAME:       Siren name, images will save in <FOLDER>/<NAME>
    NODE:       Node in local map view, that you are going to crop.
"""
CONFIG = 'alas'
FOLDER = ''
NAME = 'Deutschland'
NODE = 'D5'

if __name__ == '__main__':
    for folder in [FOLDER, os.path.join(FOLDER, NAME)]:
        if not os.path.exists(folder):
            os.mkdir(folder)

    cfg = AzurLaneConfig(CONFIG).merge(Config())
    al = ModuleBase(cfg)
    view = View(cfg)
    al.device.screenshot()
    view.load(al.device.image)
    grid = view[node2location(NODE.upper())]

    print('Please check if it is cropping the right area')
    image = rgb2gray(grid.relative_crop((-0.5, -1, 0.5, 0), shape=(60, 60)))
    image = Image.fromarray(image, mode='L').show()

    images = []
    for n in range(300):
        print(n)
        images.append(al.device.screenshot())
    for n, image in enumerate(images):
        grid.image = np.array(image)
        image = rgb2gray(grid.relative_crop((-0.5, -1, 0.5, 0), shape=(60, 60)))
Esempio n. 5
0
 def _view_init(self):
     if not hasattr(self, 'view'):
         self.view = View(self.config, grid_class=self.grid_class)
Esempio n. 6
0
 def _view_init(self):
     if not hasattr(self, 'view'):
         self.view = View(self.config)
Esempio n. 7
0
# cfg = Config()
# cfg.DETECTION_BACKEND = 'perspective'
# view = View(AzurLaneConfig('template').merge(cfg))
# view.load(image)
# view.predict()
# view.show()
# view.backend.draw()

# ==============================
# Method 2:
# Homography with real-time perspective calculation.
# This is the default method in Alas currently.
# ==============================
cfg = Config()
cfg.DETECTION_BACKEND = 'homography'
view = View(AzurLaneConfig('template').merge(cfg))
view.load(image)
view.predict()
view.show()
view.backend.draw()

# ==============================
# Method 3:
# Homography with hard-coded perspective data (HOMO_STORAGE).
# Get HOMO_STORAGE from log or from method 2.
# ==============================
# cfg = Config()
# cfg.DETECTION_BACKEND = 'homography'
# view = View(AzurLaneConfig('template').merge(cfg))
# homo_storage = ()  # Paste your HOMO_STORAGE here.
# view.backend.load_homography(storage=homo_storage)
class Config:
    """
    Paste the config of map file here
    """
    pass

from module.os.config import OSConfig
cfg = AzurLaneConfig('alas').merge(OSConfig())

# Folder to save temp images
folder = './screenshots/relative_crop'
# Put Screenshot here
file = ''

i = load_image(file)
grids = View(cfg)
grids.load(np.array(i))
grids.predict()
grids.show()


os.makedirs(folder, exist_ok=True)
for grid in grids:
    # Find more relative_crop area in module/map/grid_predictor.py
    # This one is for `predict_enemy_genre`
    piece = rgb2gray(grid.relative_crop((-0.5, -1, 0.5, 0), shape=(60, 60)))

    file = '%s_%s_%s.png' % (int(time.time()), grid.location[0], grid.location[1])
    file = os.path.join(folder, file)
    Image.fromarray(piece).save(file)