CoolFace
Modelpublic

tobil/omatrack-telemetry-reader

sourceHugging Faceupdated 17d agoView on Hugging Face
0likes
test_read_frame.py70 linesDownload Raw Back to root
1# origin: PUBLIC — generated arrays only; no footage, screenshots or reading fixtures.2import os3from pathlib import Path4import unittest5 6import numpy as np7from PIL import Image8 9from read_frame import GaugeReader, crop_bytes, decode_with_count, inspect_layout10 11 12class ExampleTests(unittest.TestCase):13    def test_negatives(self):14        for shade in (0, 32, 127, 255):15            self.assertEqual(inspect_layout(np.full((1080, 1920, 3), shade, np.uint8)), "rejected")16        rng = np.random.default_rng(7)17        for _ in range(4):18            pixels = rng.integers(0, 256, (1080, 1920, 3), dtype=np.uint8)19            self.assertEqual(inspect_layout(pixels), "rejected")20        self.assertEqual(inspect_layout(np.zeros((720, 1280, 3), np.uint8)), "unsupported_geometry")21        with self.assertRaises(ValueError):22            inspect_layout(np.zeros((1080, 1920, 3), np.float32))23 24    def test_crop_shape_and_bar_orientation(self):25        image = Image.new("RGB", (1920, 1080))26        image.paste((0, 0, 255), (956, 628, 999, 761))27        image.paste((255, 0, 0), (956, 761, 999, 894))28        crops = crop_bytes(image)29        self.assertEqual(crops.shape, (4, 64, 192, 3))30        self.assertEqual(crops.dtype, np.uint8)31        # Bottom becomes left after clockwise rotation; no image reflection.32        np.testing.assert_array_equal(crops[2, 32, 0], [255, 0, 0])33        np.testing.assert_array_equal(crops[2, 32, -1], [0, 0, 255])34        self.assertTrue((crops[0, :, :61] == 0).all())35        with self.assertRaises(ValueError):36            crop_bytes(image.resize((1280, 720)))37 38    def test_count_constrained_repeated_digits(self):39        logits = np.full((11, 24), -12, dtype=np.float32)40        logits[2] = 6  # token 2 means digit 1, not digit 241        logits[0, 5::6] = 10  # blanks permit repeated copies of the same digit42        for length in (1, 2, 3):43            counts = np.full(3, -4, dtype=np.float32)44            counts[length - 1] = 445            self.assertEqual(decode_with_count(logits, counts), "1" * length)46        with self.assertRaises(ValueError):47            decode_with_count(logits * np.nan, np.zeros(3, np.float32))48 49    def test_model_checksum_failure(self):50        import tempfile51        with tempfile.TemporaryDirectory() as directory:52            model = Path(directory) / "not-a-model.onnx"53            model.write_bytes(b"public synthetic invalid model")54            with self.assertRaises(ValueError):55                GaugeReader(model)56 57    @unittest.skipUnless(os.environ.get("OMATRACK_EXAMPLE_MODEL"), "set OMATRACK_EXAMPLE_MODEL for downloaded-model smoke test")58    def test_downloaded_model_rejects_blank(self):59        reader = GaugeReader(os.environ["OMATRACK_EXAMPLE_MODEL"])60        result = reader.read(Image.new("RGB", (1920, 1080)))61        self.assertEqual(result["status"], "rejected")62        self.assertTrue(result["visited"])63        self.assertFalse(result["layout_supported"])64        self.assertTrue(all(value is None for value in result["observations"].values()))65        self.assertFalse(any(result["known"].values()))66 67 68if __name__ == "__main__":69    unittest.main()70