gameboy_worlds.emulation.sword_of_hope.base_metrics

 1from typing import Optional
 2
 3import numpy as np
 4
 5from gameboy_worlds.emulation.sword_of_hope.parsers import _BaseSwordOfHopeParser
 6from gameboy_worlds.emulation.tracker import OCRegionMetric
 7
 8
 9class SwordOfHopeOCRMetric(OCRegionMetric):
10    REQUIRED_PARSER = _BaseSwordOfHopeParser
11
12    def reset(self, first=False):
13        super().reset(first)
14
15    def start(self):
16        self.kinds = {"full_screen": "full_screen"}
17        super().start()
18
19    def can_read_kind(self, current_frame: np.ndarray, kind: str) -> bool:
20        return True
class SwordOfHopeOCRMetric(gameboy_worlds.emulation.tracker.OCRegionMetric):
10class SwordOfHopeOCRMetric(OCRegionMetric):
11    REQUIRED_PARSER = _BaseSwordOfHopeParser
12
13    def reset(self, first=False):
14        super().reset(first)
15
16    def start(self):
17        self.kinds = {"full_screen": "full_screen"}
18        super().start()
19
20    def can_read_kind(self, current_frame: np.ndarray, kind: str) -> bool:
21        return True

Watch particular screen regions and capture subscreens for OCR when possible. Does not actually perform OCR itself, but makes it easy to capture the relevant regions. Children implementing this must define self.kinds in start() and then call on super().start().

Reports:

  • ocr_regions: A dictionary mapping kinds to captured regions that had OCR-eligible text detected in them. The keys are kinds of OCR regions, and the values are the stacks of captured screen regions as numpy arrays of shape (num_captures, height, width, channels).
  • step: The current step number. Useful for differentiating when multiple OCR texts were found in the same episode. You can typically safely ignore this.

Final Reports:

  • ocr_regions: A list of tuples for all steps where OCR was detected. Is in form: List[Tuple[int, Dict[str, np.ndarray]]] where the int is the step number and the Dict maps kinds to a stack of the captured screen region.
REQUIRED_PARSER = <class 'gameboy_worlds.emulation.sword_of_hope.parsers._BaseSwordOfHopeParser'>

The StateParser which implements the minimum required functionality for this MetricGroup to work.

def reset(self, first=False):
13    def reset(self, first=False):
14        super().reset(first)

ocr_regions will track a list of the form List[Tuple[int, Dict[str, np.ndarray]]] which is a list of (step_number, {kind: ocr_region}) dictionaries.

def start(self):
16    def start(self):
17        self.kinds = {"full_screen": "full_screen"}
18        super().start()

Assumes the child has initialized a dict called self.kinds which tracks the various kinds of OCR that could be done. self.kinds should be in the form: {kind: region_name} where region_name is the name of the region to OCR for that kind. Will track ocr captured region results in form of list of dictionaries where these kinds are keys.

def can_read_kind(self, current_frame: numpy.ndarray, kind: str) -> bool:
20    def can_read_kind(self, current_frame: np.ndarray, kind: str) -> bool:
21        return True

Checks if the frame has text for the given kind.

Arguments:
  • frame (np.ndarray): The frame to check.
  • kind (str): The kind of text to check for.