Download example_usage.py from fang718/pp-ocrv6-hanzi-ids-ocr: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/fang718/pp-ocrv6-hanzi-ids-ocr/resolve/main/example_usage.py
- Command line
-
hf download hf://fang718/pp-ocrv6-hanzi-ids-ocr/example_usage.py
-
curl -L -o example_usage.py https://huggingface.co/fang718/pp-ocrv6-hanzi-ids-ocr/resolve/main/example_usage.py
4.39 kB
| """Minimal end-to-end use of the PP-OCRv6 Hanzi IDS model. | |
| python example_usage.py path/to/glyph.png | |
| With no argument the script renders a few characters from a local font and | |
| predicts them, so it can be pasted into a fresh environment as a smoke test. | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from PIL import Image, ImageDraw, ImageFont | |
| from hanzi_ids import CanonicalOutput, IDSModel, beam_batch | |
| from hanzi_ids.render_utils import normalize_image | |
| MODEL_DIR = Path(__file__).resolve().parent | |
| # Default fonts used only by the self-test path. Any TTF/OTF works. | |
| FONT_MAIN = "C:/Windows/Fonts/simsun.ttc" | |
| FONT_EXT_B = "C:/Windows/Fonts/simsunb.ttf" | |
| class IDSRunner: | |
| """Load the checkpoint ensemble and predict IDS candidates for glyph images.""" | |
| def __init__(self, model_dir=MODEL_DIR, device="cpu", threads=4): | |
| model_dir = Path(model_dir) | |
| torch.set_num_threads(threads) | |
| self.device = torch.device(device) | |
| self.config = json.loads( | |
| (model_dir / "inference_config.json").read_text(encoding="utf-8") | |
| ) | |
| self.models = [] | |
| for name in self.config["checkpoints"]: | |
| ck = torch.load(model_dir / name, map_location="cpu", weights_only=True) | |
| model = IDSModel( | |
| ck["vocab"], | |
| ck["args"]["dim"], | |
| ck["args"]["layers"], | |
| tree_position=ck["args"].get("tree_position", False), | |
| ) | |
| model.load_state_dict(ck["model"]) | |
| model.eval() | |
| model.to(self.device) | |
| self.models.append(model) | |
| self.policy = CanonicalOutput.load(model_dir / self.config["output_policy"]) | |
| def _batch(self, images): | |
| rows = [ | |
| np.repeat( | |
| (np.asarray(normalize_image(im), dtype=np.float32) / 127.5 - 1)[None], | |
| 3, | |
| axis=0, | |
| ) | |
| for im in images | |
| ] | |
| return torch.stack([torch.from_numpy(r) for r in rows]).to(self.device) | |
| def predict(self, images, beam_size=5): | |
| """Return up to five distinct IDS candidates per image, best first.""" | |
| with torch.inference_mode(): | |
| batch = self._batch(images) | |
| results = [] | |
| for tensor, raw in zip(batch, beam_batch(self.models, batch, beam_size)): | |
| candidates = self.policy.apply(raw, merge=True) | |
| if len(candidates) < 5: | |
| wider = beam_batch(self.models, tensor[None], 12)[0] | |
| candidates = self.policy.apply(wider, merge=True) | |
| results.append( | |
| [ | |
| { | |
| "ids": c["ids"], | |
| "canonical_log_mass": c["canonical_log_mass"], | |
| "mean_token_probability": c["mean_token_probability"], | |
| "equivalent_raw_ids": c.get("equivalent_raw_ids", []), | |
| "valid": c["valid"], | |
| } | |
| for c in candidates[:5] | |
| ] | |
| ) | |
| return results | |
| def render_glyph(char, font_path, px=200): | |
| font = ImageFont.truetype(font_path, px) | |
| left, top, right, bottom = font.getbbox(char) | |
| image = Image.new("L", (right - left + 20, bottom - top + 20), 255) | |
| ImageDraw.Draw(image).text((10 - left, 10 - top), char, font=font, fill=0) | |
| return image | |
| def pick_font(char): | |
| return FONT_EXT_B if ord(char) >= 0x20000 else FONT_MAIN | |
| def self_test(runner): | |
| chars = ["\u4e00", "\u54c1", "\u5b57", "\u660e", "\u6728", "\u6797", "\u68ee"] | |
| for char in chars: | |
| print(f"=== {char} U+{ord(char):04X}") | |
| for c in runner.predict([render_glyph(char, pick_font(char))])[0]: | |
| print( | |
| f" {c['ids']:<28} mass={c['canonical_log_mass']:>8.3f} " | |
| f"p={c['mean_token_probability']:.4f}" | |
| ) | |
| return True | |
| def main(): | |
| runner = IDSRunner() | |
| if len(sys.argv) > 1: | |
| path = Path(sys.argv[1]) | |
| candidates = runner.predict([Image.open(path)])[0] | |
| print(json.dumps({"file": str(path), "candidates": candidates}, | |
| ensure_ascii=False, indent=2)) | |
| return 0 | |
| return 0 if self_test(runner) else 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |