Repository navigation
Expand file tree
/
Copy pathscore_reader.py
More file actions
61 lines (47 loc) · 2.09 KB
/
Copy pathscore_reader.py
File metadata and controls
61 lines (47 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional
import numpy as np
from PIL import Image
@dataclass
class TemplateDigitReader:
"""Lightweight digit classifier based on template matching.
The scoreboard digits in Junimo Kart use a fixed font and position. Instead of
running a neural network, we binarize the digit image and compare it against
pre-labelled templates. This approach is fast, deterministic, and more
resilient to the small blurring/aliasing artifacts that occur when grabbing
the screen.
"""
labels_dir: Path = Path("DigitClassification/Labels")
diff_threshold: float = 0.25
def __post_init__(self) -> None:
self.templates = self._load_templates(self.labels_dir)
def predict(self, image: Image.Image) -> Optional[str]:
"""Return the digit character or ``None`` if confidence is low."""
processed = self._preprocess(image)
best_digit: Optional[str] = None
best_score = float("inf")
for digit, template in self.templates.items():
# Mean absolute error between binary masks gives a stable distance
score = float(np.mean(np.abs(processed - template)))
if score < best_score:
best_score = score
best_digit = digit
if best_score > self.diff_threshold:
return None
return best_digit
@staticmethod
def _load_templates(path: Path) -> Dict[str, np.ndarray]:
templates: Dict[str, np.ndarray] = {}
for file in path.glob("*.png"):
digit = file.stem
templates[digit] = TemplateDigitReader._preprocess(Image.open(file))
return templates
@staticmethod
def _preprocess(image: Image.Image) -> np.ndarray:
# Standardize size and convert to binary mask to make matching robust to
# minor color/anti-aliasing changes.
normalized = image.convert("L").resize((15, 30))
array = np.array(normalized, dtype=np.float32) / 255.0
return (array > 0.5).astype(np.float32)