from __future__ import annotations import json import os import statistics import time import urllib.request import cv2 import numpy as np DEMO_URL = "https://raw.githubusercontent.com/open-mmlab/mmpose/v1.3.2/tests/data/coco/000000000785.jpg" def percentile(values: list[float], p: float) -> float: values = sorted(values) return values[min(len(values) - 1, max(0, int(np.ceil(len(values) * p)) - 1))] def run_offline_benchmark(models, threshold: float, max_people: int) -> None: path = "/logs/offline-benchmark.json" if os.path.exists(path): return try: encoded = np.frombuffer(urllib.request.urlopen(DEMO_URL, timeout=30).read(), dtype=np.uint8) one = cv2.imdecode(encoded, cv2.IMREAD_COLOR) if one is None: raise RuntimeError("official demo image decode failed") images = {1: one, 2: np.concatenate([one, one], axis=1), 4: np.concatenate([np.concatenate([one, one], axis=1)] * 2, axis=0)} for _ in range(50): models.infer(one, threshold, max_people) report = {"source_url": DEMO_URL, "warmup_iterations": 50, "measured_iterations": 60, "image_retained": False, "profiles": {}} for requested, image in images.items(): samples = []; counts = [] for _ in range(60): started = time.perf_counter_ns() people, det_ms, pose_ms = models.infer(image, threshold, max_people) samples.append((time.perf_counter_ns() - started) / 1e6); counts.append(len(people)) report["profiles"][str(requested)] = { "requested_people": requested, "detected_people_min": min(counts), "detected_people_max": max(counts), "p50_ms": round(percentile(samples, .50), 3), "p95_ms": round(percentile(samples, .95), 3), "p99_ms": round(percentile(samples, .99), 3), "fps_from_p50": round(1000 / percentile(samples, .50), 3)} with open(path, "x", encoding="utf-8") as fh: json.dump(report, fh, indent=2) except Exception as exc: with open(path, "w", encoding="utf-8") as fh: json.dump({"error": f"{type(exc).__name__}: {str(exc)[:240]}", "source_url": DEMO_URL, "image_retained": False}, fh, indent=2)