forked from longtao.wu/droboticflow
279 lines
11 KiB
Python
279 lines
11 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import math
|
|
import pathlib
|
|
import re
|
|
import shutil
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
IMAGE_DIGEST_PATTERN = re.compile(r"^\S+@sha256:[a-f0-9]{64}$")
|
|
MODEL_DIGEST_PATTERN = re.compile(r"^sha256:[a-f0-9]{64}$")
|
|
MEDIA_SUFFIXES = {".jpg", ".jpeg", ".png", ".bmp", ".webp", ".mp4", ".mov", ".m4v", ".avi", ".mkv", ".webm"}
|
|
|
|
|
|
def _media_reference(value: Any) -> str:
|
|
if isinstance(value, list):
|
|
for item in value:
|
|
found = _media_reference(item)
|
|
if found:
|
|
return found
|
|
return ""
|
|
if not isinstance(value, dict):
|
|
return ""
|
|
for key in ("previewURL", "previewUrl", "videoURL", "videoUrl", "imageURL", "imageUrl", "mediaURL", "mediaUrl", "url"):
|
|
candidate = str(value.get(key, "")).strip()
|
|
suffix = pathlib.PurePosixPath(urllib.parse.urlparse(candidate).path).suffix.lower()
|
|
if candidate and suffix in MEDIA_SUFFIXES:
|
|
return candidate
|
|
for key in ("source", "preview", "media", "file", "items", "entries", "rows", "data", "payload", "inline"):
|
|
found = _media_reference(value.get(key))
|
|
if found:
|
|
return found
|
|
candidate = str(value.get("path", "")).strip()
|
|
if candidate and pathlib.PurePosixPath(urllib.parse.urlparse(candidate).path).suffix.lower() in MEDIA_SUFFIXES:
|
|
return candidate
|
|
return ""
|
|
|
|
|
|
def _input_reference(inputs: list[dict[str, Any]]) -> str:
|
|
for artifact in inputs:
|
|
found = _media_reference(artifact)
|
|
if found:
|
|
return found
|
|
for artifact in inputs:
|
|
path = pathlib.Path(str(artifact.get("path", "")).strip())
|
|
if path.is_file() and path.suffix.lower() == ".json":
|
|
try:
|
|
found = _media_reference(json.loads(path.read_text(encoding="utf-8")))
|
|
except (OSError, UnicodeDecodeError, json.JSONDecodeError):
|
|
found = ""
|
|
if found:
|
|
return found
|
|
for artifact in inputs:
|
|
for candidate in (
|
|
artifact.get("previewURL"),
|
|
artifact.get("url"),
|
|
artifact.get("path"),
|
|
(artifact.get("data") or {}).get("source", {}).get("path") if isinstance(artifact.get("data"), dict) else "",
|
|
):
|
|
value = str(candidate or "").strip()
|
|
if value:
|
|
return value
|
|
return ""
|
|
|
|
|
|
def _materialized_input_reference(
|
|
inputs: list[dict[str, Any]],
|
|
output_dir: pathlib.Path,
|
|
helpers: HelperMap,
|
|
) -> str:
|
|
source = _input_reference(inputs)
|
|
parsed = urllib.parse.urlparse(source)
|
|
if parsed.scheme in {"http", "https"} or (not parsed.scheme and pathlib.Path(source).is_file()):
|
|
return source
|
|
materialize = helpers.get("source_path_from_item")
|
|
if not callable(materialize):
|
|
return source
|
|
source_ref = helpers.get("source_ref_from_artifact")
|
|
for artifact in inputs:
|
|
if not _media_reference(artifact):
|
|
continue
|
|
reference = source_ref(artifact) if callable(source_ref) else artifact.get("source", {})
|
|
name = str(artifact.get("name") or pathlib.PurePosixPath(parsed.path).name or "headpose-input.mp4")
|
|
local = pathlib.Path(
|
|
materialize(
|
|
{
|
|
"source": reference if isinstance(reference, dict) else {},
|
|
"path": str(artifact.get("path", "")),
|
|
},
|
|
name,
|
|
output_dir / "_inputs",
|
|
{},
|
|
)
|
|
)
|
|
if local.is_file() and local.stat().st_size > 0:
|
|
return str(local)
|
|
return source
|
|
|
|
|
|
def _post_json(url: str, payload: dict[str, Any], timeout: int) -> dict[str, Any]:
|
|
request = urllib.request.Request(
|
|
url,
|
|
data=json.dumps(payload).encode("utf-8"),
|
|
headers={"Content-Type": "application/json", "Accept": "application/json"},
|
|
method="POST",
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=timeout) as response:
|
|
parsed = json.loads(response.read().decode("utf-8"))
|
|
except urllib.error.HTTPError as exc:
|
|
detail = exc.read().decode("utf-8", errors="replace")
|
|
raise RuntimeError(f"Headpose inference request failed with HTTP {exc.code}: {detail[:1000]}") from exc
|
|
except (urllib.error.URLError, TimeoutError) as exc:
|
|
raise RuntimeError(f"Headpose inference request failed: {exc}") from exc
|
|
if not isinstance(parsed, dict):
|
|
raise RuntimeError("Headpose inference returned a non-object payload")
|
|
return parsed
|
|
|
|
|
|
def _materialize_annotated_media(base_url: str, value: str, output_dir: pathlib.Path, timeout: int) -> pathlib.Path:
|
|
source = value.strip()
|
|
if not source:
|
|
raise RuntimeError("Headpose inference did not return annotatedMediaUrl")
|
|
parsed = urllib.parse.urlparse(source)
|
|
suffix = pathlib.PurePosixPath(parsed.path).suffix.lower() or ".mp4"
|
|
destination = output_dir / "headpose_outputs" / f"headpose-annotated{suffix}"
|
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
if parsed.scheme in {"http", "https"}:
|
|
resolved = source
|
|
elif parsed.scheme:
|
|
raise RuntimeError(f"Unsupported Headpose annotated media scheme: {parsed.scheme}")
|
|
else:
|
|
local = pathlib.Path(source)
|
|
if local.is_file():
|
|
shutil.copy2(local, destination)
|
|
return destination
|
|
resolved = urllib.parse.urljoin(base_url.rstrip("/") + "/", source.lstrip("/"))
|
|
try:
|
|
with urllib.request.urlopen(resolved, timeout=timeout) as response:
|
|
destination.write_bytes(response.read())
|
|
except (urllib.error.URLError, urllib.error.HTTPError, TimeoutError) as exc:
|
|
raise RuntimeError(f"Headpose annotated media download failed: {exc}") from exc
|
|
if not destination.is_file() or destination.stat().st_size <= 0:
|
|
raise RuntimeError("Headpose annotated media is empty")
|
|
return destination
|
|
|
|
|
|
def _summary(frames: list[dict[str, Any]]) -> dict[str, Any]:
|
|
def stats(key: str) -> dict[str, float]:
|
|
values = [float(frame[key]) for frame in frames]
|
|
return {"min": min(values), "max": max(values), "mean": sum(values) / len(values)}
|
|
|
|
return {
|
|
"yawDeg": stats("yawDeg"),
|
|
"pitchDeg": stats("pitchDeg"),
|
|
"rollDeg": stats("rollDeg"),
|
|
"confidenceMean": sum(float(frame["confidence"]) for frame in frames) / len(frames),
|
|
}
|
|
|
|
|
|
def run(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: pathlib.Path,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
base_url = str(params.get("HEADPOSE_API_BASE_URL", "")).strip().rstrip("/")
|
|
model_image = str(params.get("HEADPOSE_SERVICE_IMAGE_ID", "")).strip()
|
|
model_digest = str(params.get("HEADPOSE_MODEL_SHA256", "")).strip().lower()
|
|
license_id = str(params.get("HEADPOSE_LICENSE_ID", "")).strip()
|
|
model_name = str(params.get("HEADPOSE_MODEL", "6DRepNet")).strip() or "6DRepNet"
|
|
if not base_url or not IMAGE_DIGEST_PATTERN.fullmatch(model_image):
|
|
raise RuntimeError("Headpose requires HEADPOSE_API_BASE_URL and a digest-pinned HEADPOSE_SERVICE_IMAGE_ID")
|
|
if not MODEL_DIGEST_PATTERN.fullmatch(model_digest) or not license_id:
|
|
raise RuntimeError("Headpose requires HEADPOSE_MODEL_SHA256 and HEADPOSE_LICENSE_ID")
|
|
source = _materialized_input_reference(inputs, output_dir, helpers)
|
|
if not source:
|
|
raise RuntimeError("Headpose requires an upstream image or video artifact")
|
|
frame_stride = int(params.get("FRAME_STRIDE", 3) or 3)
|
|
max_frames = int(params.get("MAX_FRAMES", 120) or 120)
|
|
timeout = int(params.get("REQUEST_TIMEOUT_SECONDS", 300) or 300)
|
|
response = _post_json(
|
|
f"{base_url}/v1/headpose/infer",
|
|
{
|
|
"source": source,
|
|
"frameStride": frame_stride,
|
|
"maxFrames": max_frames,
|
|
"model": model_name,
|
|
"modelSha256": model_digest,
|
|
},
|
|
timeout,
|
|
)
|
|
frames = response.get("frames")
|
|
if not isinstance(frames, list) or not frames:
|
|
raise RuntimeError("Headpose inference returned no frame results")
|
|
normalized_frames = []
|
|
previous_timestamp = -1.0
|
|
for index, frame in enumerate(frames):
|
|
if not isinstance(frame, dict):
|
|
raise RuntimeError("Headpose frame result must be an object")
|
|
timestamp = float(frame.get("timestampSeconds", frame.get("timestamp", 0)))
|
|
normalized = {
|
|
"frameIndex": int(frame.get("frameIndex", index)),
|
|
"timestamp": timestamp,
|
|
"timestampSeconds": timestamp,
|
|
"yawDeg": float(frame.get("yawDeg")),
|
|
"pitchDeg": float(frame.get("pitchDeg")),
|
|
"rollDeg": float(frame.get("rollDeg")),
|
|
"confidence": float(frame.get("confidence", 0)),
|
|
}
|
|
if not all(math.isfinite(float(normalized[key])) for key in ("timestamp", "yawDeg", "pitchDeg", "rollDeg", "confidence")):
|
|
raise RuntimeError("Headpose frame values must be finite")
|
|
if timestamp < 0 or timestamp < previous_timestamp:
|
|
raise RuntimeError("Headpose timestamps must be non-negative and monotonic")
|
|
if any(abs(float(normalized[key])) > 180 for key in ("yawDeg", "pitchDeg", "rollDeg")):
|
|
raise RuntimeError("Headpose angles must be in [-180, 180] degrees")
|
|
if not 0 <= normalized["confidence"] <= 1:
|
|
raise RuntimeError("Headpose confidence must be in [0, 1]")
|
|
normalized_frames.append(normalized)
|
|
previous_timestamp = timestamp
|
|
runtime_evidence = response.get("runtimeEvidence")
|
|
if not isinstance(runtime_evidence, dict):
|
|
raise RuntimeError("Headpose inference returned no runtime evidence")
|
|
if str(runtime_evidence.get("modelSha256", "")).strip().lower() != model_digest:
|
|
raise RuntimeError("Headpose runtime model digest does not match the configured digest")
|
|
annotated_media = _materialize_annotated_media(
|
|
base_url,
|
|
str(response.get("annotatedMediaUrl", "")),
|
|
output_dir,
|
|
timeout,
|
|
)
|
|
manifest = {
|
|
"schemaVersion": "droboticflow.headpose/v1",
|
|
"enabled": True,
|
|
"source": source,
|
|
"model": model_name,
|
|
"serviceImageId": model_image,
|
|
"modelSha256": model_digest,
|
|
"licenseId": license_id,
|
|
"frameCount": len(normalized_frames),
|
|
"frames": normalized_frames,
|
|
"summary": _summary(normalized_frames),
|
|
"annotatedMedia": annotated_media.name,
|
|
"rendering": response.get("rendering") or {
|
|
"kind": "headpose_axis_overlay",
|
|
"browserPlayable": annotated_media.suffix.lower() in {".mp4", ".jpg", ".jpeg", ".png"},
|
|
},
|
|
"runtimeEvidence": runtime_evidence,
|
|
}
|
|
binding = helpers["output_binding"](node_data)
|
|
artifacts = [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"headpose-manifest.json",
|
|
manifest,
|
|
output=binding,
|
|
)
|
|
]
|
|
artifacts.extend(
|
|
helpers["collect_output_artifacts"](
|
|
run_id,
|
|
node_id,
|
|
annotated_media.parent,
|
|
node_data=node_data,
|
|
output=binding,
|
|
)
|
|
)
|
|
return artifacts
|