forked from longtao.wu/droboticflow
653 lines
37 KiB
Python
653 lines
37 KiB
Python
import json
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
from backend.operators.builtin.orbbec_data_normalize import operator as orbbec_operator
|
|
from backend.operators.builtin.directory_scan import operator as directory_operator
|
|
from backend.operators.builtin.video_face_blur import operator as blur_operator
|
|
from backend.operators.builtin.qwen_behavior_rough_cut import operator as qwen_operator
|
|
from backend.operators.builtin.annotation_gate import operator as annotation_gate_operator
|
|
from backend.operators.builtin.package import operator as package_operator
|
|
from backend.operators.builtin.semantic_prelabel import operator as semantic_operator
|
|
|
|
|
|
def test_pts_validation_identifies_file_and_rejects_all_invalid_inputs(tmp_path):
|
|
import pytest
|
|
video = tmp_path / "camera_right_part0004.mp4"
|
|
pts = tmp_path / "camera_right_part0004_pts.csv"
|
|
with pytest.raises(RuntimeError, match="camera_right_part0004"):
|
|
blur_operator._validate_video_pts(video, None, 2)
|
|
for contents in ["", "timestamp_us\n", "wrong_header\n1\n", "timestamp_us\nbad\n", "timestamp_us\n2\n1\n", "timestamp_us\n1\n"]:
|
|
pts.write_text(contents)
|
|
with pytest.raises(RuntimeError, match="camera_right_part0004_pts.csv"):
|
|
blur_operator._validate_video_pts(video, pts, 2)
|
|
pts.write_bytes(b"\xff\xfeinvalid")
|
|
with pytest.raises(RuntimeError, match="camera_right_part0004_pts.csv"):
|
|
blur_operator._validate_video_pts(video, pts, 2)
|
|
pts.write_text("timestamp_us\n1\n2\n")
|
|
assert blur_operator._validate_video_pts(video, pts, 2) == 2
|
|
|
|
|
|
def test_face_blur_pairs_each_segment_pts_instead_of_last_collection_pts():
|
|
videos = [{"name": f"capture_camera_left_part{part:04d}.mp4", "data": {"collectionId": "capture"}} for part in (1, 2)]
|
|
pts = [{"name": f"capture_camera_left_part{part:04d}_pts.csv", "path": f"/pts/part{part}.csv", "data": {"collectionId": "capture"}} for part in (1, 2)]
|
|
assert blur_operator._timestamp_for_video(videos[0], pts)["path"] == "/pts/part1.csv"
|
|
assert blur_operator._timestamp_for_video(videos[1], pts)["path"] == "/pts/part2.csv"
|
|
|
|
|
|
def test_face_blur_rejects_ambiguous_pts_instead_of_using_a_different_segment():
|
|
import pytest
|
|
with pytest.raises(RuntimeError, match="无法唯一匹配"):
|
|
blur_operator._timestamp_for_video({"name": "unknown.mp4", "data": {"collectionId": "capture"}}, [
|
|
{"name": "first_pts.csv", "data": {"collectionId": "capture"}},
|
|
{"name": "second_pts.csv", "data": {"collectionId": "capture"}},
|
|
])
|
|
|
|
|
|
def helpers(tmp_path: Path):
|
|
def hyperparams(node_data):
|
|
return {item["key"]: str(item.get("value", "")) for item in node_data.get("hyperparams", [])}
|
|
|
|
def first_payload_with_keys(inputs, *keys):
|
|
for artifact in inputs:
|
|
data = artifact.get("data")
|
|
if isinstance(data, dict) and all(key in data for key in keys):
|
|
return data
|
|
return None
|
|
|
|
def write_json_artifact(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
|
return {"runId": run_id, "nodeId": node_id, "name": name, "path": str(path), "type": "json", "data": payload}
|
|
|
|
def parse_json_param(params, key, default, expected_type=None):
|
|
value = params.get(key, "")
|
|
parsed = json.loads(value) if value else default
|
|
assert expected_type is None or isinstance(parsed, expected_type)
|
|
return parsed
|
|
|
|
def parse_unit_interval_param(params, key, default):
|
|
value = float(params.get(key, default))
|
|
assert 0 <= value <= 1
|
|
return value
|
|
|
|
return {
|
|
"hyperparams": hyperparams,
|
|
"first_payload_with_keys": first_payload_with_keys,
|
|
"write_json_artifact": write_json_artifact,
|
|
"output_binding": lambda _node_data: {"id": "out-1", "name": "output", "type": "json"},
|
|
"parse_json_param": parse_json_param,
|
|
"parse_unit_interval_param": parse_unit_interval_param,
|
|
}
|
|
|
|
|
|
def test_orbbec_normalizer_pairs_real_capture_files(tmp_path):
|
|
capture = "Orbbec_Ego_AZER76400GG_19700102_061040"
|
|
entries = []
|
|
for suffix, mime in [
|
|
("camera_left_part0001.mp4", "video/mp4"),
|
|
("camera_left_part0001_pts.csv", "text/csv"),
|
|
("camera_right_part0001.mp4", "video/mp4"),
|
|
("camera_right_part0001_pts.csv", "text/csv"),
|
|
("audio.wav", "audio/x-wav"),
|
|
("audio_pts.csv", "text/csv"),
|
|
("imu.csv", "text/csv"),
|
|
("calibration_camera.yaml", "application/x-yaml"),
|
|
("calibration_imu.yaml", "application/x-yaml"),
|
|
]:
|
|
path = f"{capture}/{capture}_{suffix}"
|
|
entries.append({"path": path, "sizeBytes": 128, "mimeType": mime, "source": {"path": f"bos://testxyz/{path}"}})
|
|
artifacts = orbbec_operator.run(
|
|
"run-1",
|
|
"normalize",
|
|
tmp_path,
|
|
{"hyperparams": [{"key": "CAMERA_SIDE", "value": "both"}, {"key": "REQUIRE_STEREO", "value": "true"}]},
|
|
[{"data": {"sourcePath": "bos://testxyz/fanyu_test", "entries": entries}}],
|
|
helpers(tmp_path),
|
|
)
|
|
payload = artifacts[0]["data"]
|
|
assert payload["schemaVersion"] == "droboticflow.orbbec.normalized/v1"
|
|
assert payload["collectionCount"] == 1
|
|
assert payload["videoCount"] == 2
|
|
assert payload["collections"][0]["valid"] is True
|
|
assert {item["cameraSide"] for item in payload["items"]} == {"left", "right"}
|
|
assert all(item["pairedFiles"]["imu"].endswith("_imu.csv") for item in payload["items"])
|
|
|
|
|
|
def test_normalizer_pairs_each_segment_and_rejects_unmapped_transcodes():
|
|
import pytest
|
|
capture = "FYLX_TEST02_AZER764009A_20260903_155824"
|
|
entries = [{"path": f"{capture}/part{part:04d}/{capture}_{suffix}"}
|
|
for part in (1, 2)
|
|
for suffix in (f"camera_left_part{part:04d}.mp4", f"camera_left_part{part:04d}_pts.csv",
|
|
f"audio_part{part:04d}.wav", f"audio_part{part:04d}_pts.csv", f"imu_part{part:04d}.csv")]
|
|
for part in (1, 2):
|
|
video = f"{capture}/part{part:04d}/{capture}_camera_left_part{part:04d}.mp4"
|
|
paired = orbbec_operator._paired_files(video, entries)
|
|
assert all(f"part{part:04d}" in paired[key] for key in ("pts", "audio", "audioPts", "imu"))
|
|
transformed = video.removesuffix(".mp4") + "_silent_480p18fps_wm.mp4"
|
|
assert orbbec_operator._paired_files(transformed, entries)["pts"] == paired["pts"]
|
|
exact = transformed.removesuffix(".mp4") + "_pts.csv"
|
|
assert orbbec_operator._paired_files(transformed, entries + [{"path": exact}])["pts"] == exact
|
|
|
|
|
|
def test_normalizer_rejects_ambiguous_sidecars():
|
|
import pytest
|
|
with pytest.raises(RuntimeError, match="配套文件不唯一"):
|
|
orbbec_operator._paired_files("capture_camera_left_part0001.mp4", [
|
|
{"path": "a/capture_camera_left_part0001_pts.csv"},
|
|
{"path": "b/capture_camera_left_part0001_pts.csv"},
|
|
])
|
|
|
|
|
|
def test_directory_reader_multiport_emits_real_collection_artifacts(tmp_path):
|
|
source = tmp_path / "capture"
|
|
source.mkdir()
|
|
for name in ("left.mp4", "frame.jpg", "calibration.yaml"):
|
|
(source / name).write_bytes(b"fixture")
|
|
|
|
def output_binding(node_data, index=0, name=""):
|
|
for output in node_data["outputs"]:
|
|
if output["id"] == name or output["name"] == name:
|
|
return output
|
|
return node_data["outputs"][index]
|
|
|
|
def build_virtual_artifact(run_id, node_id, name, path, artifact_type, size_bytes, data=None, metadata=None, output=None):
|
|
return {
|
|
"id": f"{run_id}-{node_id}-{name}",
|
|
"name": name,
|
|
"path": str(path),
|
|
"type": artifact_type,
|
|
"sizeBytes": size_bytes,
|
|
"data": data or {},
|
|
"portId": output.get("id", "") if output else "",
|
|
"portName": output.get("name", "") if output else "",
|
|
}
|
|
|
|
def write_json_artifact(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return build_virtual_artifact(run_id, node_id, name, path, "json", path.stat().st_size, payload, output=output)
|
|
|
|
def list_source_entries(root, _pattern):
|
|
return [
|
|
{"path": item.name, "sizeBytes": item.stat().st_size, "mimeType": "application/octet-stream"}
|
|
for item in sorted(Path(root).iterdir())
|
|
if item.is_file()
|
|
]
|
|
|
|
node_data = {
|
|
"hyperparams": [{"key": "SOURCE_PATH", "value": str(source)}],
|
|
"outputs": [
|
|
{"id": "directory", "name": "directory"},
|
|
{"id": "videos", "name": "videos"},
|
|
{"id": "images", "name": "images"},
|
|
{"id": "files", "name": "files"},
|
|
{"id": "summary", "name": "summary"},
|
|
],
|
|
}
|
|
artifacts = directory_operator.run(
|
|
"run-directory",
|
|
"directory",
|
|
tmp_path,
|
|
node_data,
|
|
[],
|
|
{
|
|
"hyperparams": lambda data: {item["key"]: item.get("value", "") for item in data.get("hyperparams", [])},
|
|
"resolve_source_path": lambda params, _inputs: Path(params["SOURCE_PATH"]),
|
|
"list_source_entries": list_source_entries,
|
|
"output_binding": output_binding,
|
|
"build_virtual_artifact": build_virtual_artifact,
|
|
"write_json_artifact": write_json_artifact,
|
|
"canonical_bos_path": lambda bucket, key: f"bos://{bucket}/{key}",
|
|
},
|
|
)
|
|
by_port = {}
|
|
for artifact in artifacts:
|
|
by_port.setdefault(artifact["portId"], []).append(artifact)
|
|
assert {"directory", "videos", "images", "files", "summary"}.issubset(by_port)
|
|
assert by_port["videos"][0]["name"] == "left.mp4"
|
|
assert by_port["images"][0]["name"] == "frame.jpg"
|
|
assert by_port["files"][0]["name"] == "calibration.yaml"
|
|
assert by_port["summary"][0]["data"]["videoCount"] == 1
|
|
assert all("manifest" not in artifact["name"] for artifact in artifacts)
|
|
|
|
|
|
def test_orbbec_normalizer_multiport_binds_camera_and_pts_outputs(tmp_path):
|
|
capture = "Orbbec_Ego_AZER76400GG_19700102_061040"
|
|
entries = []
|
|
for suffix, mime in [
|
|
("camera_left_part0001.mp4", "video/mp4"),
|
|
("camera_left_part0001_pts.csv", "text/csv"),
|
|
("camera_right_part0001.mp4", "video/mp4"),
|
|
("camera_right_part0001_pts.csv", "text/csv"),
|
|
("audio.wav", "audio/x-wav"),
|
|
("imu.csv", "text/csv"),
|
|
("calibration_camera.yaml", "application/x-yaml"),
|
|
]:
|
|
path = f"{capture}/{capture}_{suffix}"
|
|
entries.append({"path": path, "sizeBytes": 128, "mimeType": mime, "source": {"path": f"bos://testxyz/fanyu/{path}"}})
|
|
|
|
outputs = [
|
|
{"id": "leftVideos", "name": "leftVideos"}, {"id": "rightVideos", "name": "rightVideos"},
|
|
{"id": "videoPts", "name": "videoPts"}, {"id": "audio", "name": "audio"},
|
|
{"id": "imu", "name": "imu"}, {"id": "calibrations", "name": "calibrations"},
|
|
{"id": "summary", "name": "summary"},
|
|
]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return next((item for item in outputs if item["id"] == name or item["name"] == name), outputs[index])
|
|
|
|
def virtual(run_id, node_id, name, path, artifact_type, size_bytes, data=None, metadata=None, output=None):
|
|
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "path": path, "type": artifact_type, "sizeBytes": size_bytes, "data": data or {}, "portId": output["id"], "portName": output["name"]}
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return virtual(run_id, node_id, name, str(path), "json", path.stat().st_size, payload, output=output)
|
|
|
|
artifacts = orbbec_operator.run(
|
|
"run-multi",
|
|
"normalize",
|
|
tmp_path,
|
|
{"hyperparams": [{"key": "CAMERA_SIDE", "value": "both"}, {"key": "REQUIRE_STEREO", "value": "true"}], "outputs": outputs},
|
|
[{
|
|
"data": {"sourcePath": "bos://testxyz/fanyu", "entries": entries},
|
|
"payload": {
|
|
"mode": "ref",
|
|
"type": "file",
|
|
"ref": {"provider": "BOS", "connectionId": "storage-fanyu", "bucket": "testxyz", "prefix": "fanyu", "kind": "directory"},
|
|
},
|
|
}],
|
|
{"hyperparams": lambda data: {item["key"]: item.get("value", "") for item in data.get("hyperparams", [])}, "first_payload_with_keys": helpers(tmp_path)["first_payload_with_keys"], "output_binding": output_binding, "build_virtual_artifact": virtual, "write_json_artifact": write},
|
|
)
|
|
ports = {item["portId"] for item in artifacts}
|
|
assert {"leftVideos", "rightVideos", "videoPts", "audio", "imu", "calibrations", "summary"}.issubset(ports)
|
|
assert all("manifest" not in item["name"] for item in artifacts)
|
|
remote_artifacts = [item for item in artifacts if str(item.get("path", "")).startswith("bos://")]
|
|
assert remote_artifacts
|
|
assert all(item["payload"]["ref"]["connectionId"] == "storage-fanyu" for item in remote_artifacts)
|
|
assert all(item["payload"]["ref"]["kind"] == "object" for item in remote_artifacts)
|
|
|
|
|
|
def test_face_blur_multiport_rebinds_videos_and_summary(tmp_path):
|
|
outputs = [
|
|
{"id": "blurredVideos", "name": "blurredVideos"},
|
|
{"id": "detectionSummary", "name": "detectionSummary"},
|
|
{"id": "diagnosticFrames", "name": "diagnosticFrames"},
|
|
]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return next((item for item in outputs if item["id"] == name or item["name"] == name), outputs[index])
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": "json", "data": payload, "portId": output["id"], "portName": output["name"]}
|
|
|
|
original = blur_operator._run_legacy
|
|
|
|
def fake_legacy(run_id, node_id, output_dir, node_data, inputs, helpers):
|
|
video_path = Path(output_dir) / "blurred" / "blurred_left.mp4"
|
|
video_path.parent.mkdir(parents=True, exist_ok=True)
|
|
video_path.write_bytes(b"video")
|
|
diagnostic_path = Path(output_dir) / "face-blur-diagnostics" / "left" / "diagnostic-frame-000001.jpg"
|
|
diagnostic_path.parent.mkdir(parents=True, exist_ok=True)
|
|
diagnostic_path.write_bytes(b"diagnostic")
|
|
(Path(output_dir) / "blurred-manifest.json").write_text(json.dumps({
|
|
"itemCount": 1,
|
|
"totalFramesProcessed": 4,
|
|
"totalFacesDetected": 2,
|
|
"method": {"detectionMethod": "haar", "preservePts": True},
|
|
"items": [{
|
|
"collectionId": "left",
|
|
"sanitizedFilename": "blurred_left.mp4",
|
|
"faceBlurMetrics": {"diagnosticFrames": [{"path": str(diagnostic_path), "frameIndex": 1}]},
|
|
}],
|
|
}), encoding="utf-8")
|
|
return [
|
|
{"name": "blurred-manifest.json", "path": str(Path(output_dir) / "blurred-manifest.json"), "type": "json", "portId": "out-1"},
|
|
{"name": "blurred_left.mp4", "path": str(video_path), "type": "video", "portId": "out-1"},
|
|
{"name": diagnostic_path.name, "path": str(diagnostic_path), "type": "image", "portId": "out-1"},
|
|
]
|
|
|
|
blur_operator._run_legacy = fake_legacy
|
|
try:
|
|
artifacts = blur_operator.run(
|
|
"run-blur",
|
|
"blur",
|
|
tmp_path,
|
|
{"outputs": outputs},
|
|
[{"name": "left.mp4", "path": str(tmp_path / "left.mp4"), "type": "video", "portId": "videos"}],
|
|
{"output_binding": output_binding, "write_json_artifact": write},
|
|
)
|
|
finally:
|
|
blur_operator._run_legacy = original
|
|
assert [item["portId"] for item in artifacts] == ["blurredVideos", "diagnosticFrames", "detectionSummary"]
|
|
assert artifacts[1]["data"]["privacySafe"] is True
|
|
assert artifacts[-1]["data"]["totalFacesDetected"] == 2
|
|
assert artifacts[-1]["data"]["diagnosticFrames"][0]["frameIndex"] == 1
|
|
|
|
|
|
def test_qwen_multiport_rebinds_clips_prelabels_boundaries_and_summary(tmp_path):
|
|
outputs = [
|
|
{"id": "clips", "name": "clips"}, {"id": "prelabels", "name": "prelabels"},
|
|
{"id": "boundaries", "name": "boundaries"}, {"id": "inferenceSummary", "name": "inferenceSummary"},
|
|
]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return next((item for item in outputs if item["id"] == name or item["name"] == name), outputs[index])
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": "json", "data": payload, "portId": output["id"], "portName": output["name"]}
|
|
|
|
original = qwen_operator._run_legacy
|
|
|
|
def fake_legacy(run_id, node_id, output_dir, node_data, inputs, helpers):
|
|
manifest = {
|
|
"items": [{"clipId": "clip-1", "clipName": "clip-1.mp4", "startSecond": 1.0, "endSecond": 3.0, "rawPTS": {"startTimestampUs": 100}, "annotation": {"actions": []}}],
|
|
"rawBehaviorEvents": [{}], "technicalBlocks": [{}], "provenance": {"modelId": "Qwen/Qwen3-VL-8B-Instruct"},
|
|
}
|
|
(Path(output_dir) / "clips-manifest.json").write_text(json.dumps(manifest), encoding="utf-8")
|
|
return [{"name": "clips-manifest.json", "type": "json", "portId": "out-1"}, {"name": "clip-1.mp4", "type": "video", "path": "bos://testxyz/blurred.mp4", "portId": "out-1"}]
|
|
|
|
qwen_operator._run_legacy = fake_legacy
|
|
try:
|
|
artifacts = qwen_operator.run(
|
|
"run-qwen", "qwen", tmp_path, {"outputs": outputs},
|
|
[{"name": "blurred_left.mp4", "path": "bos://testxyz/blurred_left.mp4", "type": "video", "portId": "videos"}, {"name": "left_pts.csv", "path": "bos://testxyz/left_pts.csv", "type": "file", "portId": "timestamps"}],
|
|
{"output_binding": output_binding, "write_json_artifact": write},
|
|
)
|
|
finally:
|
|
qwen_operator._run_legacy = original
|
|
assert [item["portId"] for item in artifacts] == ["clips", "prelabels", "boundaries", "inferenceSummary"]
|
|
assert artifacts[1]["data"]["items"][0]["actions"] == []
|
|
assert artifacts[2]["data"]["boundaries"][0]["clipId"] == "clip-1"
|
|
|
|
|
|
def test_qwen_v58_emits_only_timestamp_windows(tmp_path):
|
|
outputs = [{"id": "roughCutWindows", "name": "roughCutWindows"}]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return outputs[0]
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": "json", "data": payload, "portId": output["id"], "portName": output["name"]}
|
|
|
|
original = qwen_operator._run_legacy
|
|
|
|
def fake_legacy(run_id, node_id, output_dir, node_data, inputs, helpers):
|
|
manifest = {
|
|
"items": [{
|
|
"clipId": "window-1",
|
|
"collectionId": "capture",
|
|
"sourceVideo": "blurred_left.mp4",
|
|
"sourceDurationSeconds": 25.0,
|
|
"startSecond": 1.0,
|
|
"endSecond": 3.0,
|
|
"behavior": "拿取商品",
|
|
"rawPTS": {"originTimestampUs": 1_000_000, "startTimestampUs": 2_000_000, "endTimestampUs": 4_000_000},
|
|
"annotation": {"actions": [{"confidence": 0.8}]},
|
|
}],
|
|
}
|
|
(Path(output_dir) / "clips-manifest.json").write_text(json.dumps(manifest), encoding="utf-8")
|
|
for name in ("_qwen_behavior_rough_cut_frames", "_qwen_behavior_rough_cut_videos"):
|
|
technical = Path(output_dir) / name
|
|
technical.mkdir()
|
|
(technical / "temporary.bin").write_bytes(b"technical-only")
|
|
return [{"name": "clips-manifest.json", "type": "json", "portId": "out-1"}]
|
|
|
|
qwen_operator._run_legacy = fake_legacy
|
|
try:
|
|
artifacts = qwen_operator.run(
|
|
"run-qwen-v58", "qwen", tmp_path, {"outputs": outputs},
|
|
[{"name": "blurred_left.mp4", "path": "bos://test/blurred_left.mp4", "type": "video", "portId": "videos"},
|
|
{"name": "left_pts.csv", "path": "bos://test/left_pts.csv", "portId": "timestamps"}],
|
|
{"output_binding": output_binding, "write_json_artifact": write},
|
|
)
|
|
finally:
|
|
qwen_operator._run_legacy = original
|
|
assert len(artifacts) == 1
|
|
assert artifacts[0]["portId"] == "roughCutWindows"
|
|
assert artifacts[0]["name"] == "rough-cut-windows.json"
|
|
assert artifacts[0]["data"]["schemaVersion"] == "droboticflow.rough-cut-windows/v1"
|
|
assert artifacts[0]["data"]["windowCount"] == 1
|
|
assert artifacts[0]["data"]["windows"][0]["sourceStartTimestampUs"] == 1_000_000
|
|
assert all(not item["name"].endswith(".mp4") for item in artifacts)
|
|
assert not (tmp_path / "clips-manifest.json").exists()
|
|
assert not (tmp_path / "_qwen_behavior_rough_cut_frames").exists()
|
|
assert not (tmp_path / "_qwen_behavior_rough_cut_videos").exists()
|
|
|
|
|
|
def test_annotation_gate_multiport_binds_approved_annotations_rejected_and_summary(tmp_path):
|
|
outputs = [
|
|
{"id": "approvedVideos", "name": "approvedVideos"}, {"id": "annotations", "name": "annotations"},
|
|
{"id": "rejected", "name": "rejected"}, {"id": "aggregateSummary", "name": "aggregateSummary"},
|
|
]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return next((item for item in outputs if item["id"] == name or item["name"] == name), outputs[index])
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": "json", "data": payload, "portId": output["id"], "portName": output["name"]}
|
|
|
|
artifacts = annotation_gate_operator.run(
|
|
"run-human", "human", tmp_path,
|
|
{"outputs": outputs, "hyperparams": [{"key": "ANNOTATION_TEMPLATE_ID", "value": "template"}, {"key": "ANNOTATION_WORKGROUP_ID", "value": "workgroup"}]},
|
|
[{"name": "clip.mp4", "type": "video", "portId": "clips"}, {"name": "headpose-annotated.mp4", "type": "video", "portId": "out-1"}, {"name": "prelabels.json", "type": "json", "portId": "prelabels", "data": {"items": [{"actions": []}]}}, {"name": "boundaries.json", "type": "json", "portId": "boundaries", "data": {"boundaries": [{"clipId": "clip"}]}}],
|
|
{"hyperparams": lambda data: {item["key"]: item.get("value", "") for item in data.get("hyperparams", [])}, "output_binding": output_binding, "payloads_from_inputs": lambda items: [item.get("data", {}) for item in items], "write_json_artifact": write},
|
|
)
|
|
assert [item["portId"] for item in artifacts] == ["approvedVideos", "annotations", "rejected", "aggregateSummary"]
|
|
assert artifacts[-1]["data"]["clipCount"] == 1
|
|
|
|
|
|
def test_package_multiport_binds_directory_media_semantic_segment_and_summary(tmp_path):
|
|
outputs = [
|
|
{"id": "packageDirectory", "name": "packageDirectory"},
|
|
{"id": "finalLeftVideos", "name": "finalLeftVideos"}, {"id": "finalRightVideos", "name": "finalRightVideos"},
|
|
{"id": "leftTimestampFiles", "name": "leftTimestampFiles"}, {"id": "rightTimestampFiles", "name": "rightTimestampFiles"},
|
|
{"id": "imuFiles", "name": "imuFiles"}, {"id": "timestampedAnnotationFiles", "name": "timestampedAnnotationFiles"},
|
|
{"id": "synchronizationFiles", "name": "synchronizationFiles"}, {"id": "calibrationFiles", "name": "calibrationFiles"},
|
|
{"id": "segmentFiles", "name": "segmentFiles"},
|
|
{"id": "deliverySummary", "name": "deliverySummary"},
|
|
]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return next((item for item in outputs if item["id"] == name or item["name"] == name), outputs[index])
|
|
|
|
def build_virtual(run_id, node_id, name, path, artifact_type, size_bytes, data=None, metadata=None, output=None):
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": path, "type": artifact_type, "data": data or {}, "portId": output["id"], "portName": output["name"]}
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": "json", "data": payload, "portId": output["id"], "portName": output["name"]}
|
|
|
|
original = package_operator._run_fanyu_v5
|
|
|
|
def fake_v5(run_id, node_id, output_dir, node_data, inputs, helpers):
|
|
root = Path(output_dir) / "packages" / "clip-1_package"
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
for name in ("clip-1.mp4", "clip-1_right.mp4", "clip-1_left_pts.csv", "clip-1_right_pts.csv", "clip-1_imu.csv", "semantic.json", "synchronized-slice.json", "segment.json"):
|
|
(root / name).write_bytes(b"fixture")
|
|
calibration = Path(output_dir) / "packages" / "calibration"
|
|
calibration.mkdir(parents=True, exist_ok=True)
|
|
(calibration / "camera.yaml").write_bytes(b"fixture")
|
|
manifest_path = Path(output_dir) / "packages-manifest.json"
|
|
manifest_path.write_text(json.dumps({"packageCount": 1}), encoding="utf-8")
|
|
return [
|
|
{"id": "manifest-id", "name": "packages-manifest.json", "path": str(manifest_path), "type": "json"},
|
|
{"id": "video-id-a1", "name": "clip-1.mp4", "path": str(root / "clip-1.mp4"), "type": "video"},
|
|
{"id": "video-id-a2", "name": "clip-1_right.mp4", "path": str(root / "clip-1_right.mp4"), "type": "video"},
|
|
{"id": "left-pts", "name": "clip-1_left_pts.csv", "path": str(root / "clip-1_left_pts.csv"), "type": "file"},
|
|
{"id": "right-pts", "name": "clip-1_right_pts.csv", "path": str(root / "clip-1_right_pts.csv"), "type": "file"},
|
|
{"id": "imu", "name": "clip-1_imu.csv", "path": str(root / "clip-1_imu.csv"), "type": "file"},
|
|
{"id": "semantic-id-a1", "name": "semantic.json", "path": str(root / "semantic.json"), "type": "json"},
|
|
{"id": "sync-id-a1", "name": "synchronized-slice.json", "path": str(root / "synchronized-slice.json"), "type": "json"},
|
|
{"id": "segment-id-a1", "name": "segment.json", "path": str(root / "segment.json"), "type": "json"},
|
|
{"id": "calibration-id", "name": "camera.yaml", "path": str(calibration / "camera.yaml"), "type": "file"},
|
|
]
|
|
|
|
def collect(_run, _node, root, node_data=None):
|
|
raise AssertionError("multiport package must preserve the generated artifact identities")
|
|
|
|
package_operator._run_fanyu_v5 = fake_v5
|
|
try:
|
|
artifacts = package_operator.run(
|
|
"run-package", "package", tmp_path, {"outputs": outputs, "hyperparams": [{"key": "REQUIRE_FINAL_ACCEPTANCE", "value": "true"}]},
|
|
[{"name": "clip-1.mp4", "type": "video", "portId": "approvedVideos"}, {"name": "annotations-manifest.json", "type": "json", "portId": "annotations", "data": {"items": [{"clipId": "clip-1"}]}}],
|
|
{"output_binding": output_binding, "build_virtual_artifact": build_virtual, "write_json_artifact": write, "collect_output_artifacts": collect, "hyperparams": lambda data: {item["key"]: item.get("value", "") for item in data.get("hyperparams", [])}},
|
|
)
|
|
finally:
|
|
package_operator._run_fanyu_v5 = original
|
|
assert {item["portId"] for item in artifacts} == {"packageDirectory", "finalLeftVideos", "finalRightVideos", "leftTimestampFiles", "rightTimestampFiles", "imuFiles", "timestampedAnnotationFiles", "synchronizationFiles", "calibrationFiles", "segmentFiles", "deliverySummary"}
|
|
directory = next(item for item in artifacts if item["name"] == "delivery-package-directory.index")
|
|
assert directory["data"]["schemaVersion"] == "droboticflow.directory-index/v1"
|
|
assert directory["data"]["packageCount"] == 1
|
|
assert next(item for item in artifacts if item["name"] == "packages-manifest.json")["id"] == "manifest-id"
|
|
assert len({item["id"] for item in artifacts}) == len(artifacts)
|
|
|
|
|
|
@pytest.mark.parametrize("raw_human", [False, True])
|
|
@pytest.mark.parametrize("sampled", [False, True])
|
|
def test_v58_package_organizes_accepted_revision_directory_without_first_cut(tmp_path, monkeypatch, raw_human, sampled):
|
|
accepted_root = tmp_path / "accepted" / "revision-000001"
|
|
source_slice = accepted_root / "slices" / "slice-000001"
|
|
source_slice.mkdir(parents=True)
|
|
for name in ("left.mp4", "right.mp4"):
|
|
(source_slice / name).write_bytes(b"accepted-materialized-file")
|
|
for name in ("left_pts.csv", "right_pts.csv"):
|
|
(source_slice / name).write_text("frame_index,timestamp_ms,clip_time_ms\n0,1000,0\n1,1500,500\n", encoding="utf-8")
|
|
(source_slice / "imu.csv").write_text(
|
|
"timestamp_ms,clip_time_ms,x,y,z,type\n1000,0,0,0,9.8,accel\n1000,0,0,0,0,gyro\n1500,500,0,0,9.8,accel\n1500,500,0,0,0,gyro\n",
|
|
encoding="utf-8",
|
|
)
|
|
(source_slice / "annotation.json").write_text(json.dumps({"actions": []}), encoding="utf-8")
|
|
(source_slice / "synchronized-slice.json").write_text(json.dumps({"schemaVersion": "droboticflow.synchronized-slice/v1"}), encoding="utf-8")
|
|
(accepted_root / "revision-manifest.json").write_text(json.dumps({
|
|
"schemaVersion": "droboticflow.slice-materialization/v2",
|
|
"revision": 1,
|
|
"humanItemId": "human-item",
|
|
"taskId": "task",
|
|
"runId": "source-run",
|
|
"sliceCount": 1,
|
|
"coverage": {"mode": "full_common_range", "contiguous": True, "gapMicroseconds": 0, "overlapMicroseconds": 0, "sliceCount": 1, "startSecond": 0.0, "endSecond": 1.0},
|
|
"items": [{"sliceId": "slice-000001", "directory": "slices/slice-000001", "startSecond": 0.0, "endSecond": 1.0, "startTimestampUs": 1_000_000, "endTimestampUs": 2_000_000}],
|
|
}), encoding="utf-8")
|
|
camera_calibration = tmp_path / "calibration_camera.yaml"
|
|
imu_calibration = tmp_path / "calibration_imu.yaml"
|
|
camera_calibration.write_text("camera: real\n", encoding="utf-8")
|
|
imu_calibration.write_text("imu: real\n", encoding="utf-8")
|
|
inputs = [
|
|
{"id": "accepted-directory", "name": "revision.directory.index", "path": str(accepted_root), "type": "file", "portId": "acceptedSliceDirectory", "data": {}},
|
|
{"id": "acceptance", "name": "acceptance-summary.json", "type": "json", "portId": "acceptanceSummary", "data": {"capture": {"captureId": "capture-a", "prefix": "test/capture-a", "fingerprint": "sha"}, "qualityReviews": [{"decision": "approved", "revision": 1}], "finalAcceptance": {"sampled": False, "samplingDecision": "not_selected", "samplingProbability": 0.5, "samplingPolicyRevision": 2, "samplingRandomValue": 0.8, "poseDelivery": {"headpose": "include", "handpose": "exclude"}}}},
|
|
{"id": "headpose", "name": "headpose.json", "type": "json", "portId": "headpose", "data": {"schemaVersion": "droboticflow.headpose/v1", "frames": [{"frameIndex": 0, "timestampSeconds": 0.5, "yawDeg": 0, "pitchDeg": 0, "rollDeg": 0}]}},
|
|
{"id": "handpose", "name": "handpose.json", "type": "json", "portId": "handpose", "data": {"schemaVersion": "droboticflow.wilor.hand_pose_result/v1", "result": {"frames": [{"frameIndex": 0, "timestampSeconds": 0.5}]}}},
|
|
{"id": "camera-calibration", "name": camera_calibration.name, "path": str(camera_calibration), "type": "file", "portId": "calibrations", "data": {}},
|
|
{"id": "imu-calibration", "name": imu_calibration.name, "path": str(imu_calibration), "type": "file", "portId": "calibrations", "data": {}},
|
|
]
|
|
outputs = [
|
|
{"id": "deliveryDirectory", "name": "deliveryDirectory"},
|
|
{"id": "deliveryManifest", "name": "deliveryManifest"},
|
|
{"id": "deliverySummary", "name": "deliverySummary"},
|
|
]
|
|
|
|
def output_binding(_node_data, index=0, name=""):
|
|
return next((item for item in outputs if item["id"] == name or item["name"] == name), outputs[index])
|
|
|
|
def source_path(item, name, _output, _archives=None):
|
|
return Path(item.get("path") or next(candidate["path"] for candidate in inputs if candidate["name"] == name))
|
|
|
|
def write(run_id, node_id, output_dir, name, payload, output=None):
|
|
path = Path(output_dir) / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": "json", "data": payload, "portId": output["id"], "portName": output["name"]}
|
|
|
|
def virtual(run_id, node_id, name, path, artifact_type, size_bytes, data=None, metadata=None, output=None):
|
|
return {"id": f"{run_id}-{name}", "name": name, "path": str(path), "type": artifact_type, "data": data or {}, "portId": output["id"], "portName": output["name"]}
|
|
|
|
monkeypatch.setattr(package_operator, "_ffprobe_frame_count", lambda path: 2)
|
|
node = {"outputs": outputs}
|
|
if sampled:
|
|
inputs[1]["data"]["finalAcceptance"].update(sampled=True,samplingDecision="selected",samplingProbability=1.0)
|
|
if raw_human:
|
|
node["hyperparams"]=[{"key":"PROCESSING_PROFILE","value":"raw_human"}]
|
|
inputs[1]["data"]["processingProfile"]="raw_human"
|
|
inputs[1]["data"]["finalAcceptance"]["processingProfile"]="raw_human"
|
|
inputs[1]["data"]["finalAcceptance"].pop("poseDelivery")
|
|
inputs=[item for item in inputs if item["portId"] not in {"headpose","handpose"}]
|
|
artifacts = package_operator.run(
|
|
"package-run", "package", tmp_path / "output", node, inputs,
|
|
{
|
|
"output_binding": output_binding,
|
|
"payloads_from_inputs": lambda items: [item.get("data", {}) for item in items],
|
|
"source_path_from_item": source_path,
|
|
"source_ref_from_artifact": lambda item: {"path": item.get("path")},
|
|
"write_json_artifact": write,
|
|
"build_virtual_artifact": virtual,
|
|
},
|
|
)
|
|
delivery = tmp_path / "output" / "delivery"
|
|
target_slice = delivery / "clips" / "slice-000001"
|
|
assert (target_slice / "left.mp4").read_bytes() == b"accepted-materialized-file"
|
|
assert (target_slice / "right.mp4").is_file()
|
|
assert (target_slice / "headpose.json").is_file() is (not raw_human)
|
|
assert not (target_slice / "handpose.json").exists()
|
|
sync = json.loads((target_slice / "synchronized-slice.json").read_text(encoding="utf-8"))
|
|
assert {item["name"] for item in sync["calibration"]} == {"calibration_camera.yaml", "calibration_imu.yaml"}
|
|
manifest = json.loads((delivery / "packages-manifest.json").read_text(encoding="utf-8"))
|
|
assert manifest["lineage"]["acceptedSliceDirectoryArtifactId"] == "accepted-directory"
|
|
assert manifest["poseDelivery"] == ({"headpose":"not_generated","handpose":"not_generated"} if raw_human else {"headpose":"include","handpose":"exclude"})
|
|
assert manifest["acceptanceSampling"] == {"sampled": sampled, "decision": "selected" if sampled else "not_selected", "probability": 1.0 if sampled else 0.5, "policyRevision": 2, "randomValue": 0.8, "decidedAt": None}
|
|
if raw_human:
|
|
assert manifest["processing"]["faceAnonymization"] == "not_applied"
|
|
assert manifest["coverage"]["contiguous"] is True
|
|
assert manifest["capture"]["captureId"] == "capture-a"
|
|
assert {item["name"] for item in manifest["rootEvidence"]} == {"capture-manifest.json", "quality-review.json", "acceptance-record.json", "sampling-evidence.json"}
|
|
assert json.loads((delivery / "quality-review.json").read_text(encoding="utf-8"))["reviews"][0]["decision"] == "approved"
|
|
assert {item["portId"] for item in artifacts} == {"deliveryDirectory", "deliveryManifest", "deliverySummary"}
|
|
|
|
|
|
def test_semantic_prelabel_maps_actions_to_template_version(tmp_path):
|
|
annotations = {
|
|
"annotationCount": 1,
|
|
"items": [
|
|
{
|
|
"clipId": "clip-1",
|
|
"annotation": {
|
|
"actions": [
|
|
{"label": "静止观察", "start": 0, "end": 2, "confidence": 0.9},
|
|
{"label": "快速移动", "start": 2, "end": 4, "confidence": 0.8},
|
|
]
|
|
},
|
|
}
|
|
],
|
|
}
|
|
artifacts = semantic_operator.run(
|
|
"run-2",
|
|
"semantic",
|
|
tmp_path,
|
|
{
|
|
"hyperparams": [
|
|
{"key": "TEMPLATE_VERSION", "value": "v3"},
|
|
{"key": "LABEL_SCHEMA_JSON", "value": json.dumps([{"id": "static", "name": "静止观察"}, {"id": "fast", "name": "快速移动"}], ensure_ascii=False)},
|
|
{"key": "PHRASES_JSON", "value": json.dumps(["手部"], ensure_ascii=False)},
|
|
{"key": "MINIMUM_CONFIDENCE", "value": "0.70"},
|
|
]
|
|
},
|
|
[{"data": annotations}],
|
|
helpers(tmp_path),
|
|
)
|
|
payload = artifacts[0]["data"]
|
|
assert payload["templateVersion"] == "v3"
|
|
assert payload["segmentCount"] == 2
|
|
assert [item["labelId"] for item in payload["items"][0]["semanticAnnotation"]["segments"]] == ["static", "fast"]
|