forked from longtao.wu/droboticflow
113 lines
4.6 KiB
Python
113 lines
4.6 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from backend.operators.builtin.orbbec_data_normalize import operator as orbbec_operator
|
|
from backend.operators.builtin.semantic_prelabel import operator as semantic_operator
|
|
|
|
|
|
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_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"]
|