Spaces:
Sleeping
Sleeping
Download tests/test_plate_format.py from Abhyuday-06/VisionGuard-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/spaces/Abhyuday-06/VisionGuard-AI/resolve/main/tests/test_plate_format.py
- Command line
-
hf download hf://spaces/Abhyuday-06/VisionGuard-AI/tests/test_plate_format.py
-
curl -L -o test_plate_format.py https://huggingface.co/spaces/Abhyuday-06/VisionGuard-AI/resolve/main/tests/test_plate_format.py
2.59 kB
| import numpy as np | |
| import pytest | |
| from core.plate_format import decode_constrained, format_plate, is_valid_indian_plate, repair_text | |
| ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_" | |
| def probs_for(text: str, slots: int = 10, confusions: dict[int, dict[str, float]] | None = None) -> np.ndarray: | |
| """Build a slot-probability matrix that 'reads' ``text`` (pad-filled), with optional per-slot confusions.""" | |
| padded = text.ljust(slots, "_") | |
| m = np.full((slots, len(ALPHABET)), 1e-4) | |
| for i, ch in enumerate(padded): | |
| m[i, ALPHABET.index(ch)] = 0.9 | |
| for alt, p in (confusions or {}).get(i, {}).items(): | |
| m[i, ALPHABET.index(alt)] = p | |
| return m / m.sum(axis=1, keepdims=True) | |
| def test_valid_plates(plate): | |
| assert is_valid_indian_plate(plate) | |
| def test_invalid_plates(plate): | |
| assert not is_valid_indian_plate(plate) | |
| def test_letter_o_in_digit_slot_is_fixed(): | |
| # model prefers 'O' at index 2 (should be digit 0) -> constrained decode must return 0 | |
| p = probs_for("KAO5MG1909", confusions={2: {"0": 0.6}}) | |
| p[2, ALPHABET.index("O")] = 0.9 | |
| res = decode_constrained(p, ALPHABET) | |
| assert res.text == "KA05MG1909" | |
| assert res.valid | |
| def test_digit_eight_in_letter_slot_is_fixed(): | |
| p = probs_for("KA0181234", confusions={4: {"B": 0.5}}) # series letter 'B' misread as '8' | |
| res = decode_constrained(p, ALPHABET) | |
| assert res.text == "KA01B1234" | |
| assert res.valid | |
| def test_invalid_state_code_is_corrected_to_nearest_legal_state(): | |
| p = probs_for("KX01AB1234", confusions={1: {"A": 0.3}}) | |
| res = decode_constrained(p, ALPHABET) | |
| assert res.text.startswith("KA") | |
| def test_bh_series(): | |
| res = decode_constrained(probs_for("22BH1234AB"), ALPHABET) | |
| assert res.text == "22BH1234AB" and res.valid | |
| def test_garbage_falls_back_to_raw_when_no_layout_fits(): | |
| p = np.full((10, len(ALPHABET)), 1e-6) | |
| for i in range(10): | |
| p[i, ALPHABET.index("_")] = 1.0 | |
| res = decode_constrained(p / p.sum(axis=1, keepdims=True), ALPHABET) | |
| assert res.text == "" | |
| assert not res.valid | |
| def test_repair_text_for_string_only_backends(): | |
| assert repair_text("KAO1AB1Z34") == "KA01AB1234" | |
| assert repair_text("MH 12 DE 1433") == "MH12DE1433" | |
| def test_format_plate(): | |
| assert format_plate("KA01AB1234") == "KA 01 AB 1234" | |
| assert format_plate("22BH1234AB") == "22 BH 1234 AB" | |