forked from longtao.wu/droboticflow
462 lines
20 KiB
Python
462 lines
20 KiB
Python
import hashlib
|
|
import json
|
|
import pathlib
|
|
import shutil
|
|
|
|
import pytest
|
|
|
|
from backend.operators.builtin.delivery_options import operator as delivery_options
|
|
from backend.operators.builtin.head_pose import operator as head_pose
|
|
from backend.operators.builtin.package import operator as package_operator
|
|
from backend.operators.builtin.wilor_hand_pose import operator as wilor
|
|
|
|
|
|
def helpers(params=None, payloads=None):
|
|
params = params or {}
|
|
payloads = payloads or []
|
|
|
|
def write_json(run_id, node_id, output_dir, name, data, output=None):
|
|
pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True)
|
|
return {"name": name, "data": data, "path": str(pathlib.Path(output_dir) / name)}
|
|
|
|
return {
|
|
"hyperparams": lambda node_data: params,
|
|
"payloads_from_inputs": lambda inputs: payloads,
|
|
"output_binding": lambda node_data: None,
|
|
"write_json_artifact": write_json,
|
|
"first_payload_with_keys": lambda inputs, *keys: None,
|
|
"collect_output_artifacts": lambda run_id, node_id, output_dir, **kwargs: [
|
|
{"id": f"{run_id}-{node_id}-{path.name}", "name": path.name, "path": str(path)}
|
|
for path in pathlib.Path(output_dir).rglob("*")
|
|
if path.is_file()
|
|
],
|
|
}
|
|
|
|
|
|
def test_delivery_options_defaults_handpose_off(tmp_path):
|
|
artifacts = delivery_options.run("run", "node", tmp_path, {}, [], helpers(payloads=[{}]))
|
|
assert artifacts[0]["data"] == {
|
|
"schemaVersion": "droboticflow.delivery-options/v1",
|
|
"includeHeadpose": True,
|
|
"includeHandpose": False,
|
|
}
|
|
|
|
|
|
def test_wilor_disabled_does_not_resolve_media_or_call_service(tmp_path):
|
|
artifacts = wilor.run(
|
|
"run",
|
|
"node",
|
|
tmp_path,
|
|
{},
|
|
[],
|
|
helpers(payloads=[{"schemaVersion": "droboticflow.delivery-options/v1", "includeHandpose": False}]),
|
|
)
|
|
assert artifacts[0]["data"]["enabled"] is False
|
|
assert artifacts[0]["data"]["status"] == "skipped_by_delivery_options"
|
|
assert artifacts[0]["data"]["presentation"] == "hand_annotation_prelabel"
|
|
assert artifacts[0]["data"]["requiresHumanReview"] is True
|
|
|
|
|
|
def test_headpose_requires_real_service_metadata_and_validates_frames(tmp_path, monkeypatch):
|
|
annotated = tmp_path / "annotated.mp4"
|
|
annotated.write_bytes(b"real-annotated-video")
|
|
monkeypatch.setattr(
|
|
head_pose,
|
|
"_post_json",
|
|
lambda url, payload, timeout: {
|
|
"frames": [
|
|
{
|
|
"frameIndex": 3,
|
|
"timestampSeconds": 0.1,
|
|
"yawDeg": 1.0,
|
|
"pitchDeg": 2.0,
|
|
"rollDeg": 3.0,
|
|
"confidence": 0.9,
|
|
}
|
|
],
|
|
"annotatedMediaUrl": str(annotated),
|
|
"runtimeEvidence": {"gpu": "A800", "modelSha256": "sha256:" + "b" * 64},
|
|
},
|
|
)
|
|
artifacts = head_pose.run(
|
|
"run",
|
|
"node",
|
|
tmp_path,
|
|
{},
|
|
[{"previewURL": "https://example.test/input.mp4"}],
|
|
helpers(
|
|
params={
|
|
"HEADPOSE_API_BASE_URL": "https://headpose.example.test",
|
|
"HEADPOSE_SERVICE_IMAGE_ID": "registry/headpose@sha256:" + "a" * 64,
|
|
"HEADPOSE_MODEL_SHA256": "sha256:" + "b" * 64,
|
|
"HEADPOSE_LICENSE_ID": "MIT",
|
|
}
|
|
),
|
|
)
|
|
manifest = artifacts[0]["data"]
|
|
assert manifest["schemaVersion"] == "droboticflow.headpose/v1"
|
|
assert manifest["frames"][0]["yawDeg"] == 1.0
|
|
assert manifest["frames"][0]["timestamp"] == 0.1
|
|
assert manifest["summary"]["yawDeg"]["mean"] == 1.0
|
|
assert manifest["rendering"]["browserPlayable"] is True
|
|
assert artifacts[1]["name"] == "headpose-annotated.mp4"
|
|
|
|
|
|
def test_headpose_prefers_nested_presigned_url_over_bos_path():
|
|
payload = {
|
|
"path": "bos://bucket/capture_camera_right_rgb.mp4",
|
|
"source": {
|
|
"path": "bos://bucket/capture_camera_right_rgb.mp4",
|
|
"previewURL": "https://bucket.example/capture_camera_right_rgb.mp4?signature=real",
|
|
},
|
|
}
|
|
assert head_pose._media_reference(payload).startswith("https://")
|
|
|
|
|
|
def test_headpose_prefers_inline_manifest_local_media_before_bos_artifact(tmp_path):
|
|
local_video = tmp_path / "blurred-left-camera.mp4"
|
|
local_video.write_bytes(b"real-shared-workspace-video")
|
|
inputs = [
|
|
{
|
|
"name": "blurred-manifest.json",
|
|
"path": str(tmp_path / "blurred-manifest.json"),
|
|
"payload": {
|
|
"mode": "inline",
|
|
"type": "json",
|
|
"inline": {"items": [{"path": str(local_video), "source": {"path": str(local_video)}}]},
|
|
},
|
|
},
|
|
{"name": "blurred-left-camera.mp4", "path": "bos://bucket/blurred-left-camera.mp4"},
|
|
]
|
|
assert head_pose._input_reference(inputs) == str(local_video)
|
|
|
|
|
|
def test_headpose_materializes_bos_video_into_shared_workspace(tmp_path):
|
|
local_video = tmp_path / "materialized-left.mp4"
|
|
local_video.write_bytes(b"shared-video")
|
|
artifact = {"name": "left.mp4", "path": "bos://bucket/left.mp4", "type": "video"}
|
|
observed = {}
|
|
|
|
def materialize(item, name, output, archives=None):
|
|
observed.update(item=item, name=name, output=pathlib.Path(output))
|
|
return local_video
|
|
|
|
result = head_pose._materialized_input_reference(
|
|
[artifact],
|
|
tmp_path / "node-output",
|
|
{"source_path_from_item": materialize, "source_ref_from_artifact": lambda item: {"path": item["path"]}},
|
|
)
|
|
assert result == str(local_video)
|
|
assert observed["item"]["source"]["path"] == "bos://bucket/left.mp4"
|
|
|
|
|
|
def test_wilor_materializes_bos_video_before_service_upload(tmp_path):
|
|
local_video = tmp_path / "materialized-hand.mp4"
|
|
local_video.write_bytes(b"shared-video")
|
|
artifact = {"name": "left.mp4", "path": "bos://bucket/left.mp4", "type": "video"}
|
|
assert wilor._input_ref({}, [artifact]) == artifact["path"]
|
|
result = wilor._materialized_input_ref(
|
|
artifact["path"],
|
|
[artifact],
|
|
tmp_path / "node-output",
|
|
{
|
|
"source_path_from_item": lambda item, name, output, archives=None: local_video,
|
|
"source_ref_from_artifact": lambda item: {"path": item["path"]},
|
|
},
|
|
)
|
|
assert result == str(local_video)
|
|
|
|
|
|
def test_headpose_service_skips_empty_sampled_frames_but_keeps_video_fail_closed():
|
|
source = pathlib.Path("services/headpose/app.py").read_text(encoding="utf-8")
|
|
assert 'if str(error) != "no face detected in sampled frame"' in source
|
|
assert 'raise RuntimeError("no sampled video frame produced a headpose result")' in source
|
|
assert "encode_browser_video" in source
|
|
assert '"libx264"' in source
|
|
assert '"+faststart"' in source
|
|
assert '"browserPlayable": True' in source
|
|
|
|
|
|
def test_package_v3_rejects_missing_required_headpose(tmp_path):
|
|
with pytest.raises(RuntimeError, match="headpose"):
|
|
package_operator.run(
|
|
"run",
|
|
"node",
|
|
tmp_path,
|
|
{},
|
|
[],
|
|
helpers(params={"REQUIRE_HEADPOSE": "true"}, payloads=[]),
|
|
)
|
|
|
|
|
|
def test_v5_pose_free_package_writes_only_video_semantic_and_manifests(tmp_path):
|
|
clip = tmp_path / "accepted.mp4"
|
|
clip.write_bytes(b"real-final-video")
|
|
canonical = '{"grade":"A"}'
|
|
acceptance = {
|
|
"canonical": canonical,
|
|
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
|
|
"confirmedByUserId": "acceptance-user",
|
|
}
|
|
accepted = {
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
|
|
"annotationCount": 1,
|
|
"items": [{
|
|
"clipId": "slice-001",
|
|
"clipName": "slice-001.mp4",
|
|
"clipPath": str(clip),
|
|
"startSecond": 0,
|
|
"endSecond": 1,
|
|
"sourceStartSecond": 0,
|
|
"sourceEndSecond": 1,
|
|
"sourceVideo": "blurred-source.mp4",
|
|
"finalAcceptanceConfirmed": True,
|
|
"finalAcceptance": acceptance,
|
|
"transformChain": [
|
|
{"kind": "opencv-undistort", "applied": True},
|
|
{"kind": "face-anonymization", "applied": True, "detector": {"modelId": "opencv-zoo/yunet-2023mar"}},
|
|
],
|
|
"annotation": {"actions": [{"labelId": "06", "label": "热食加工与出餐", "start": 0, "end": 1}]},
|
|
}],
|
|
}
|
|
package_helpers = helpers(
|
|
params={"REQUIRE_HEADPOSE": "false", "POSE_FREE": "true", "REQUIRE_FINAL_ACCEPTANCE": "true", "GENERATE_SEGMENT_MP4": "true"},
|
|
payloads=[accepted],
|
|
)
|
|
|
|
def write_json(run_id, node_id, output_dir, name, data, output=None):
|
|
path = pathlib.Path(output_dir) / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
|
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
|
|
|
|
package_helpers.update({
|
|
"write_json_artifact": write_json,
|
|
"write_video_segment_with_ffmpeg": lambda source, destination, start, end: shutil.copy2(source, destination),
|
|
"write_video_segment": lambda source, destination, start, end: shutil.copy2(source, destination),
|
|
"inspect_video_file": lambda path: {"duration": 1.0},
|
|
})
|
|
with pytest.MonkeyPatch.context() as monkeypatch:
|
|
monkeypatch.setattr(package_operator, "_write_precise_final_segment", lambda source, destination, start, end: shutil.copy2(source, destination))
|
|
artifacts = package_operator.run("run", "package", tmp_path / "output", {}, [], package_helpers)
|
|
package_dir = tmp_path / "output" / "packages" / "slice-001_package"
|
|
assert (package_dir / "slice-001.mp4").is_file()
|
|
assert (package_dir / "semantic.json").is_file()
|
|
assert (package_dir / "segment.json").is_file()
|
|
assert not (package_dir / "headpose.json").exists()
|
|
assert not (package_dir / "handpose.json").exists()
|
|
segment = json.loads((package_dir / "segment.json").read_text(encoding="utf-8"))
|
|
assert segment["semantic"]["file"] == "semantic.json"
|
|
assert [item["kind"] for item in segment["transformChain"]] == ["opencv-undistort", "face-anonymization"]
|
|
assert "headpose" not in segment and "handpose" not in segment
|
|
package_manifest = json.loads((tmp_path / "output" / "packages-manifest.json").read_text(encoding="utf-8"))
|
|
assert package_manifest["trainingEnabled"] is False
|
|
assert "trainingProvenance" not in package_manifest
|
|
assert package_manifest["items"][0]["clipDuration"] == 1
|
|
assert package_manifest["items"][0]["sourceVideo"] == "blurred-source.mp4"
|
|
assert package_manifest["items"][0]["quality"]["source_video"] == "blurred-source.mp4"
|
|
artifact_ids = [artifact["id"] for artifact in artifacts]
|
|
assert len(artifact_ids) == len(set(artifact_ids)) == 4
|
|
|
|
|
|
def test_v56_stereo_package_crops_both_cameras_and_pose_frames_with_same_boundary(tmp_path):
|
|
left_source = tmp_path / "capture_camera_left_part0001.mp4"
|
|
right_source = tmp_path / "capture_camera_right_part0001.mp4"
|
|
left_source.write_bytes(b"left-rectified-and-blurred")
|
|
right_source.write_bytes(b"right-rectified-and-blurred")
|
|
canonical = '{"accepted":true}'
|
|
accepted = {
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
|
|
"annotationCount": 1,
|
|
"items": [{
|
|
"clipId": "stereo-001",
|
|
"clipName": "stereo-001-left.mp4",
|
|
"clipPath": str(left_source),
|
|
"collectionId": "capture",
|
|
"sourceVideo": left_source.name,
|
|
"startSecond": 0.0,
|
|
"endSecond": 1.0,
|
|
"sourceStartSecond": 0.0,
|
|
"sourceEndSecond": 1.0,
|
|
"finalAcceptanceConfirmed": True,
|
|
"finalAcceptance": {
|
|
"canonical": canonical,
|
|
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
|
|
"confirmedByUserId": "acceptance-user",
|
|
},
|
|
"annotation": {"actions": []},
|
|
}],
|
|
}
|
|
headpose = {
|
|
"schemaVersion": "droboticflow.headpose/v1",
|
|
"enabled": True,
|
|
"model": "6DRepNet",
|
|
"frames": [{"frameIndex": 3, "timestampSeconds": 0.2, "yawDeg": 1, "pitchDeg": 2, "rollDeg": 3, "confidence": 0.9}],
|
|
}
|
|
handpose = {
|
|
"schemaVersion": "droboticflow.wilor.hand_pose_result/v1",
|
|
"status": "success",
|
|
"result": {"frames": [{"sourceFrame": 3, "timestampMs": 200, "detectedHands": 1}]},
|
|
}
|
|
package_helpers = helpers(
|
|
params={
|
|
"REQUIRE_HEADPOSE": "true",
|
|
"REQUIRE_HANDPOSE": "true",
|
|
"REQUIRE_STEREO_DELIVERY": "true",
|
|
"POSE_FREE": "false",
|
|
"REQUIRE_FINAL_ACCEPTANCE": "true",
|
|
"GENERATE_SEGMENT_MP4": "true",
|
|
},
|
|
payloads=[accepted, headpose, handpose],
|
|
)
|
|
|
|
def write_json(run_id, node_id, output_dir, name, data, output=None):
|
|
path = pathlib.Path(output_dir) / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
|
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
|
|
|
|
package_helpers.update({
|
|
"write_json_artifact": write_json,
|
|
"source_path_from_item": lambda item, name, output, archives=None: pathlib.Path(str((item.get("source") or {}).get("path") or item.get("path"))),
|
|
"source_ref_from_artifact": lambda artifact: {"path": artifact.get("path", "")},
|
|
"write_video_segment_with_ffmpeg": lambda source, destination, start, end: shutil.copy2(source, destination),
|
|
"write_video_segment": lambda source, destination, start, end: shutil.copy2(source, destination),
|
|
"inspect_video_file": lambda path: {"duration": 1.0},
|
|
})
|
|
right_artifact = {
|
|
"name": right_source.name,
|
|
"path": str(right_source),
|
|
"type": "video",
|
|
"portId": "rightVideos",
|
|
"data": {"collectionId": "capture", "cameraSide": "right"},
|
|
}
|
|
with pytest.MonkeyPatch.context() as monkeypatch:
|
|
monkeypatch.setattr(package_operator, "_write_precise_final_segment", lambda source, destination, start, end: (pathlib.Path(destination).parent.mkdir(parents=True, exist_ok=True), shutil.copy2(source, destination)))
|
|
package_operator._run_fanyu_v5(
|
|
"run-v56",
|
|
"package",
|
|
tmp_path / "stereo-output",
|
|
{},
|
|
[{"name": "accepted.json", "data": accepted}, right_artifact, {"name": "headpose.json", "data": headpose}, {"name": "handpose.json", "data": handpose}],
|
|
package_helpers,
|
|
)
|
|
|
|
package_dir = tmp_path / "stereo-output" / "packages" / "stereo-001_package"
|
|
assert (package_dir / "stereo-001-left.mp4").is_file()
|
|
assert (package_dir / "stereo-001_right.mp4").is_file()
|
|
assert (package_dir / "headpose.json").is_file()
|
|
assert (package_dir / "handpose.json").is_file()
|
|
segment = json.loads((package_dir / "segment.json").read_text(encoding="utf-8"))
|
|
assert set(segment["videos"]) == {"left", "right"}
|
|
assert segment["videos"]["right"]["durationSeconds"] == 1.0
|
|
assert segment["headpose"]["frameCount"] == 1
|
|
assert segment["handpose"]["frameCount"] == 1
|
|
assert segment["handPrelabel"] == {"file": "handpose.json", "frameCount": 1, "reviewRequired": True}
|
|
manifest = json.loads((tmp_path / "stereo-output" / "packages-manifest.json").read_text(encoding="utf-8"))
|
|
assert manifest["schemaVersion"] == "droboticflow.delivery.manifest/v3"
|
|
assert manifest["stereoDelivery"] is True
|
|
assert manifest["headposeIncluded"] is True
|
|
assert manifest["handposeIncluded"] is True
|
|
assert manifest["handPrelabelIncluded"] is True
|
|
|
|
|
|
@pytest.mark.parametrize("schema", ["droboticflow.headpose/v1", "droboticflow.wilor.hand_pose_result/v1"])
|
|
def test_v5_pose_free_package_rejects_unexpected_pose_payloads(tmp_path, schema):
|
|
accepted = {
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
|
|
"annotationCount": 1,
|
|
"items": [],
|
|
}
|
|
with pytest.raises(RuntimeError, match="pose-free"):
|
|
package_operator.run(
|
|
"run",
|
|
"package",
|
|
tmp_path,
|
|
{},
|
|
[],
|
|
helpers(
|
|
params={"REQUIRE_HEADPOSE": "false", "POSE_FREE": "true", "REQUIRE_FINAL_ACCEPTANCE": "true"},
|
|
payloads=[accepted, {"schemaVersion": schema, "enabled": True, "frames": []}],
|
|
),
|
|
)
|
|
|
|
|
|
def test_v5_virtual_rough_cut_materializes_full_source_then_cuts_final_range(tmp_path):
|
|
full_source = tmp_path / "full-sanitized.mp4"
|
|
full_source.write_bytes(b"real-full-sanitized-video")
|
|
canonical = '{"confirmed":true}'
|
|
accepted = {
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
|
|
"annotationCount": 1,
|
|
"items": [{
|
|
"clipId": "rough-final-001",
|
|
"clipName": "rough-final-001.mp4",
|
|
"clipPath": "bos://testxyz/sanitized/full.mp4",
|
|
"source": {"path": "bos://testxyz/sanitized/full.mp4"},
|
|
"sourceIsFullVideo": True,
|
|
"startSecond": 8.5,
|
|
"endSecond": 11.25,
|
|
"sourceStartSecond": 8.5,
|
|
"sourceEndSecond": 11.25,
|
|
"finalAcceptanceConfirmed": True,
|
|
"finalAcceptance": {
|
|
"canonical": canonical,
|
|
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
|
|
"confirmedByUserId": "acceptance-user",
|
|
},
|
|
"annotation": {"actions": []},
|
|
}],
|
|
}
|
|
observed = {}
|
|
package_helpers = helpers(
|
|
params={"REQUIRE_HEADPOSE": "false", "REQUIRE_FINAL_ACCEPTANCE": "true", "GENERATE_SEGMENT_MP4": "true"},
|
|
payloads=[accepted],
|
|
)
|
|
|
|
def write_json(run_id, node_id, output_dir, name, data, output=None):
|
|
path = pathlib.Path(output_dir) / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
|
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
|
|
|
|
def cut(source, destination, start, end):
|
|
observed.update(source=pathlib.Path(source), start=start, end=end)
|
|
pathlib.Path(destination).parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy2(source, destination)
|
|
return {"duration": end - start}
|
|
|
|
package_helpers.update({
|
|
"write_json_artifact": write_json,
|
|
"source_path_from_item": lambda item, name, output, archives=None: full_source,
|
|
"write_video_segment_with_ffmpeg": cut,
|
|
"write_video_segment": cut,
|
|
"inspect_video_file": lambda path: {"duration": 2.75},
|
|
})
|
|
with pytest.MonkeyPatch.context() as monkeypatch:
|
|
monkeypatch.setattr(package_operator, "_write_precise_final_segment", cut)
|
|
package_operator.run("run", "package", tmp_path / "virtual-output", {}, [], package_helpers)
|
|
assert observed == {"source": full_source, "start": 8.5, "end": 11.25}
|
|
|
|
|
|
def test_precise_final_segment_reencodes_and_resets_timestamps(tmp_path, monkeypatch):
|
|
source = tmp_path / "source.mp4"
|
|
destination = tmp_path / "final.mp4"
|
|
source.write_bytes(b"source")
|
|
observed = {}
|
|
|
|
def fake_run(args, **kwargs):
|
|
observed["args"] = args
|
|
destination.write_bytes(b"final")
|
|
return type("Completed", (), {"returncode": 0, "stdout": "", "stderr": ""})()
|
|
|
|
monkeypatch.setattr(package_operator.shutil, "which", lambda name: "/usr/bin/ffmpeg")
|
|
monkeypatch.setattr(package_operator.subprocess, "run", fake_run)
|
|
package_operator._write_precise_final_segment(source, destination, 9.6, 20.5)
|
|
assert destination.is_file()
|
|
assert observed["args"][observed["args"].index("-ss") + 1] == "9.600000"
|
|
assert observed["args"][observed["args"].index("-t") + 1] == "10.900000"
|
|
assert "libx264" in observed["args"]
|
|
assert observed["args"][observed["args"].index("-reset_timestamps") + 1] == "1"
|
|
assert "-movflags" not in observed["args"]
|