gameboy_worlds.emulation.harry_potter.base_metrics

 1from typing import Optional
 2from gameboy_worlds.emulation.harry_potter.parsers import _BaseHarryPotterParser
 3from gameboy_worlds.emulation.tracker import OCRegionMetric
 4import numpy as np
 5
 6
 7class HarryPotterOCRMetric(OCRegionMetric):
 8    """
 9    Captures dialogue screen regions for OCR when dialogue is active in Harry Potter games.
10    Does not perform OCR itself — exposes captured regions as numpy arrays
11    in the info dict for downstream agents/tools to process.
12
13    Requires the parser to have:
14    - A "dialogue_box_full" named screen region (the full dialogue area to capture)
15    - dialogue_box_open() and dialogue_box_empty() methods
16
17    Reports (via OCRegionMetric):
18    - ocr_regions: Dict mapping "dialogue" -> np.ndarray of shape (n, h, w, c)
19    - step: The step number when OCR text was detected
20    """
21
22    REQUIRED_PARSER = _BaseHarryPotterParser
23
24    def start(self):
25        self.kinds = {
26            "dialogue": "dialogue_box_full",
27        }
28        super().start()
29
30    def can_read_kind(self, current_frame: np.ndarray, kind: str) -> bool:
31        self.state_parser: _BaseHarryPotterParser
32        if kind == "dialogue":
33            in_dialogue = self.state_parser.dialogue_box_open(current_frame)
34            dialogue_empty = self.state_parser.dialogue_box_empty(current_frame)
35            return in_dialogue and not dialogue_empty
36        return False
class HarryPotterOCRMetric(gameboy_worlds.emulation.tracker.OCRegionMetric):
 8class HarryPotterOCRMetric(OCRegionMetric):
 9    """
10    Captures dialogue screen regions for OCR when dialogue is active in Harry Potter games.
11    Does not perform OCR itself — exposes captured regions as numpy arrays
12    in the info dict for downstream agents/tools to process.
13
14    Requires the parser to have:
15    - A "dialogue_box_full" named screen region (the full dialogue area to capture)
16    - dialogue_box_open() and dialogue_box_empty() methods
17
18    Reports (via OCRegionMetric):
19    - ocr_regions: Dict mapping "dialogue" -> np.ndarray of shape (n, h, w, c)
20    - step: The step number when OCR text was detected
21    """
22
23    REQUIRED_PARSER = _BaseHarryPotterParser
24
25    def start(self):
26        self.kinds = {
27            "dialogue": "dialogue_box_full",
28        }
29        super().start()
30
31    def can_read_kind(self, current_frame: np.ndarray, kind: str) -> bool:
32        self.state_parser: _BaseHarryPotterParser
33        if kind == "dialogue":
34            in_dialogue = self.state_parser.dialogue_box_open(current_frame)
35            dialogue_empty = self.state_parser.dialogue_box_empty(current_frame)
36            return in_dialogue and not dialogue_empty
37        return False

Captures dialogue screen regions for OCR when dialogue is active in Harry Potter games. Does not perform OCR itself — exposes captured regions as numpy arrays in the info dict for downstream agents/tools to process.

Requires the parser to have:

  • A "dialogue_box_full" named screen region (the full dialogue area to capture)
  • dialogue_box_open() and dialogue_box_empty() methods

Reports (via OCRegionMetric):

  • ocr_regions: Dict mapping "dialogue" -> np.ndarray of shape (n, h, w, c)
  • step: The step number when OCR text was detected
REQUIRED_PARSER = <class 'gameboy_worlds.emulation.harry_potter.parsers._BaseHarryPotterParser'>

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

def start(self):
25    def start(self):
26        self.kinds = {
27            "dialogue": "dialogue_box_full",
28        }
29        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:
31    def can_read_kind(self, current_frame: np.ndarray, kind: str) -> bool:
32        self.state_parser: _BaseHarryPotterParser
33        if kind == "dialogue":
34            in_dialogue = self.state_parser.dialogue_box_open(current_frame)
35            dialogue_empty = self.state_parser.dialogue_box_empty(current_frame)
36            return in_dialogue and not dialogue_empty
37        return False

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.