Spaces:
Sleeping
Sleeping
Download tests/test_encoding.py from DmitryPlant/plants-analysis-api: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/DmitryPlant/plants-analysis-api/resolve/main/tests/test_encoding.py
- Command line
-
hf download hf://spaces/DmitryPlant/plants-analysis-api/tests/test_encoding.py
-
curl -L -o test_encoding.py https://huggingface.co/spaces/DmitryPlant/plants-analysis-api/resolve/main/tests/test_encoding.py
1.52 kB
| import base64 | |
| from pathlib import Path | |
| import cv2 | |
| import numpy as np | |
| from analysis.encoding import bgr_to_png_b64, img_b64_to_bgr | |
| def test_round_trip_preserves_bgr_bytes(): | |
| rng = np.random.default_rng(0) | |
| img = rng.integers(0, 255, size=(64, 64, 3), dtype=np.uint8) | |
| encoded = bgr_to_png_b64(img) | |
| assert encoded.startswith("data:image/png;base64,") | |
| decoded = img_b64_to_bgr(encoded) | |
| assert decoded.shape == img.shape | |
| assert np.array_equal(decoded, img) | |
| def test_decodes_raw_base64_without_data_uri_prefix(): | |
| img = np.zeros((4, 4, 3), dtype=np.uint8) | |
| encoded = bgr_to_png_b64(img) | |
| raw_b64 = encoded.removeprefix("data:image/png;base64,") | |
| assert np.array_equal(img_b64_to_bgr(raw_b64), img) | |
| def test_round_trip_grayscale(): | |
| img = np.arange(64, dtype=np.uint8).reshape(8, 8) | |
| encoded = bgr_to_png_b64(img) | |
| decoded = img_b64_to_bgr(encoded) | |
| assert decoded.shape == img.shape | |
| assert np.array_equal(decoded, img) | |
| def test_decodes_jpeg_with_jpeg_data_uri_prefix(): | |
| sample = Path(__file__).resolve().parent.parent.parent / "dataset" / "photo_2026-01-22_19-20-24.jpg" | |
| raw = sample.read_bytes() | |
| encoded = "data:image/jpeg;base64," + base64.b64encode(raw).decode() | |
| decoded = img_b64_to_bgr(encoded) | |
| assert decoded.ndim == 3 | |
| assert decoded.dtype == np.uint8 | |
| reference = cv2.imread(str(sample)) | |
| assert decoded.shape == reference.shape | |
| assert np.array_equal(decoded, reference) | |