Download tests/test_xtracker_fast.py from Marnu25/x-tracker: direct link, hf CLI and curl.
- Browser
- Download file 7.27 kB
-
https://huggingface.co/spaces/Marnu25/x-tracker/resolve/main/tests/test_xtracker_fast.py
- Command line
-
hf download hf://spaces/Marnu25/x-tracker/tests/test_xtracker_fast.py
-
curl -L -o test_xtracker_fast.py https://huggingface.co/spaces/Marnu25/x-tracker/resolve/main/tests/test_xtracker_fast.py
7.27 kB
| import re | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import cv2 | |
| import numpy as np | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| ROOT_DIR = Path(__file__).resolve().parent.parent | |
| if str(ROOT_DIR) not in sys.path: | |
| sys.path.insert(0, str(ROOT_DIR)) | |
| FORBIDDEN_TERMS = [ | |
| r"\bnsfw\b", | |
| r"nsfw_", | |
| r"_nsfw", | |
| r"siglip", | |
| r"pornography", | |
| r"hentai", | |
| r"strangerguardhf", | |
| r"auto_blur", | |
| ] | |
| SANITIZED_FILES = [ | |
| "detector.py", | |
| "camera_manager.py", | |
| "main.py", | |
| "Dockerfile", | |
| "download_models.sh", | |
| "README.md", | |
| "templates/index.html", | |
| "static/app.js", | |
| "static/style.css", | |
| ] | |
| def test_static_sanitization_audit(rel_path: str): | |
| file_path = ROOT_DIR / rel_path | |
| assert file_path.exists(), f"Expected file {rel_path} to exist" | |
| content = file_path.read_text(encoding="utf-8") | |
| for pattern in FORBIDDEN_TERMS: | |
| matches = re.findall(pattern, content, flags=re.IGNORECASE) | |
| assert not matches, f"Found forbidden term '{pattern}' in {rel_path}: {matches}" | |
| def test_detector_initialization_and_detect_contract(): | |
| from detector import ObjectDetector, TrackedBox, compute_box_iou | |
| det = ObjectDetector() | |
| assert det.loaded_yolo is True | |
| assert det.loaded_pose is True | |
| frame = np.zeros((720, 1280, 3), dtype=np.uint8) | |
| cv2.rectangle(frame, (400, 150), (650, 680), (180, 200, 220), -1) | |
| detections, telemetry = det.detect(frame) | |
| assert isinstance(detections, list) | |
| assert isinstance(telemetry, dict) | |
| assert "gpu_active" in telemetry | |
| assert "provider" in telemetry | |
| assert "keypoints_count" in telemetry | |
| assert "objects_count" in telemetry | |
| for key in telemetry: | |
| assert "nsfw" not in key.lower() | |
| # Test IoU and TrackedBox temporal smoothing (alpha = 0.60) | |
| iou = compute_box_iou([100, 100, 200, 200], [150, 150, 250, 250]) | |
| assert 0.14 < iou < 0.15 | |
| tb = TrackedBox("obj_1", "person", "person", 90.0, [100, 100, 200, 200]) | |
| tb.update("person", 80.0, [120, 120, 220, 220]) | |
| assert tb.box == [112, 112, 212, 212] | |
| assert tb.hit_count == 2 | |
| def test_render_overlay_wireframe_reticle_and_speed(): | |
| from detector import ObjectDetector | |
| det = ObjectDetector() | |
| frame = np.full((720, 1280, 3), 30, dtype=np.uint8) | |
| det.latest_keypoints = [ | |
| [[640 + (i * 5), 200 + (i * 20), 0.92] for i in range(17)] | |
| ] | |
| sample_dets = [ | |
| { | |
| "id": "obj_1", | |
| "base_label": "head / face", | |
| "label": "head / face", | |
| "confidence": 94.5, | |
| "box": [580, 160, 700, 280], | |
| "center": [0.5, 0.3], | |
| } | |
| ] | |
| t0 = time.perf_counter() | |
| rendered = det.render_overlay( | |
| frame, | |
| detections=sample_dets, | |
| telemetry=det.latest_telemetry, | |
| crop_meta=(0, 0, 1280, 720), | |
| active_target_id="obj_1", | |
| ) | |
| elapsed_ms = (time.perf_counter() - t0) * 1000.0 | |
| assert rendered.shape == (720, 1280, 3) | |
| assert elapsed_ms < 25.0, f"Overlay rendering took {elapsed_ms:.2f}ms (> 25ms)" | |
| assert not np.array_equal(frame, rendered) | |
| def test_visual_filter_dimensions_and_latency(filter_name: str): | |
| from filters import AVAILABLE_FILTERS, process_frame | |
| assert filter_name in AVAILABLE_FILTERS | |
| rng = np.random.default_rng(42) | |
| frame = rng.integers(0, 256, size=(720, 1280, 3), dtype=np.uint8) | |
| # Warmup | |
| _ = process_frame(frame, filter_name) | |
| t0 = time.perf_counter() | |
| out = process_frame(frame, filter_name) | |
| elapsed_ms = (time.perf_counter() - t0) * 1000.0 | |
| assert out is not None | |
| assert out.shape == (720, 1280, 3) | |
| assert out.dtype == np.uint8 | |
| assert elapsed_ms < 30.0, f"Filter '{filter_name}' took {elapsed_ms:.2f}ms" | |
| def test_camera_manager_ptz_and_scene_intelligence(tmp_path: Path): | |
| from camera_manager import CameraManager, apply_targeted_zoom | |
| frame = np.zeros((720, 1280, 3), dtype=np.uint8) | |
| zoomed, meta = apply_targeted_zoom(frame, 2.0, 0.5, 0.5) | |
| assert zoomed.shape == (720, 1280, 3) | |
| assert meta == (320, 180, 640, 360) | |
| cm = CameraManager(config_path=tmp_path / "cameras.json") | |
| assert cm.show_hud_overlay is True | |
| ok, _, z = cm.set_zoom(2.5, 0.4, 0.6) | |
| assert ok and z == 2.5 | |
| assert cm.zoom_center_x == 0.4 and cm.zoom_center_y == 0.6 | |
| for mode in ["face", "person", "hand", "torso", "pelvic", "feet", "off"]: | |
| ok, _ = cm.set_auto_tracking_mode(mode) | |
| assert ok | |
| assert cm.auto_tracking_mode == mode | |
| ok_enc, buf = cv2.imencode(".jpg", frame) | |
| assert ok_enc | |
| t0 = time.perf_counter() | |
| pushed = cm.push_external_frame(buf.tobytes()) | |
| push_ms = (time.perf_counter() - t0) * 1000.0 | |
| assert pushed is True | |
| assert push_ms < 25.0 | |
| scene = cm._build_scene_intelligence() | |
| assert "title" in scene | |
| assert "description" in scene | |
| assert "tracking_details" in scene | |
| assert "nsfw_details" not in scene | |
| cm.stop() | |
| def test_fastapi_endpoints(): | |
| from main import app, camera | |
| try: | |
| client = TestClient(app) | |
| r_root = client.get("/") | |
| assert r_root.status_code == 200 | |
| assert "X-Tracker Studio" in r_root.text | |
| r_status = client.get("/api/status") | |
| assert r_status.status_code == 200 | |
| st_json = r_status.json() | |
| assert st_json["status"] == "online" | |
| assert "nsfw" not in st_json["camera"] | |
| assert "telemetry" in st_json["camera"] | |
| r_filters = client.get("/api/filters") | |
| assert r_filters.status_code == 200 | |
| assert len(r_filters.json()["available_filters"]) == 12 | |
| assert client.post("/api/filter/thermal").status_code == 200 | |
| assert client.post("/api/filter/normal").status_code == 200 | |
| assert client.post("/api/zoom/2.0").status_code == 200 | |
| assert client.post("/api/zoom/focus", json={"x": 0.35, "y": 0.45, "zoom": 2.2}).status_code == 200 | |
| assert client.post("/api/zoom/reset").status_code == 200 | |
| r_dets = client.get("/api/detections") | |
| assert r_dets.status_code == 200 | |
| det_json = r_dets.json() | |
| assert "detections" in det_json | |
| assert "telemetry" in det_json | |
| assert "nsfw" not in det_json | |
| dummy_img = np.zeros((360, 640, 3), dtype=np.uint8) | |
| _, jpg = cv2.imencode(".jpg", dummy_img) | |
| jpg_bytes = jpg.tobytes() | |
| r_push = client.post("/api/frame/push", content=jpg_bytes) | |
| assert r_push.status_code == 200 | |
| assert r_push.json()["status"] == "success" | |
| r_infer = client.post("/api/infer", content=jpg_bytes) | |
| assert r_infer.status_code == 200 | |
| inf_json = r_infer.json() | |
| assert inf_json["status"] == "success" | |
| assert "nsfw" not in inf_json | |
| assert "telemetry" in inf_json | |
| r_media = client.get("/api/media") | |
| assert r_media.status_code == 200 | |
| assert "snapshots" in r_media.json() | |
| finally: | |
| camera.stop() | |