forked from longtao.wu/droboticflow
114 lines
4.5 KiB
Python
114 lines
4.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
|
|
|
|
def _run_legacy(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: Any,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
template_id = params.get("ANNOTATION_TEMPLATE_ID", "").strip()
|
|
supplier_id = params.get("ANNOTATION_SUPPLIER_ID", "").strip()
|
|
if not template_id:
|
|
raise RuntimeError("原生视频标注 Gate 需要 ANNOTATION_TEMPLATE_ID")
|
|
payloads = helpers["payloads_from_inputs"](inputs)
|
|
items = helpers["collect_items"](payloads)
|
|
if not items:
|
|
raise RuntimeError("原生视频标注 Gate 需要上游真实视频切片或预标输入")
|
|
return [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"annotation-gate-diagnostic.json",
|
|
{
|
|
"datasetType": "annotation-gate-diagnostic",
|
|
"runId": run_id,
|
|
"nodeId": node_id,
|
|
"templateId": template_id,
|
|
"supplierId": supplier_id,
|
|
"itemCount": len(items),
|
|
"items": items,
|
|
"interceptedByWorker": False,
|
|
},
|
|
output=helpers["output_binding"](node_data),
|
|
)
|
|
]
|
|
|
|
|
|
def _output(node_data: dict[str, Any], helpers: HelperMap, name: str) -> dict[str, Any] | None:
|
|
del helpers
|
|
for output in node_data.get("outputs", []) if isinstance(node_data.get("outputs"), list) else []:
|
|
if isinstance(output, dict) and name in {str(output.get("id", "")).strip(), str(output.get("name", "")).strip()}:
|
|
return output
|
|
return None
|
|
|
|
|
|
def _run_multiport(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: Any,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
template_id = params.get("ANNOTATION_TEMPLATE_ID", "").strip()
|
|
supplier_id = params.get("ANNOTATION_SUPPLIER_ID", "").strip()
|
|
if not template_id:
|
|
raise RuntimeError("原生视频标注 Gate 需要 ANNOTATION_TEMPLATE_ID")
|
|
# Only the Qwen ``clips`` port creates human work. HeadPose rendering is
|
|
# also a video input in v5.7, but it is review evidence, not another clip
|
|
# to cut, annotate, accept, and package.
|
|
clips = [item for item in inputs if str(item.get("portId") or item.get("portName") or "") == "clips"]
|
|
if not clips:
|
|
raise RuntimeError("原生视频标注 Gate 多端口路径需要 clips 视频集合")
|
|
for clip in clips:
|
|
clip["portId"] = str((_output(node_data, helpers, "approvedVideos") or {}).get("id", ""))
|
|
clip["portName"] = str((_output(node_data, helpers, "approvedVideos") or {}).get("name", ""))
|
|
payloads = helpers["payloads_from_inputs"](inputs)
|
|
prelabels = []
|
|
boundaries = []
|
|
for payload in payloads:
|
|
if isinstance(payload.get("items"), list):
|
|
prelabels.extend(payload["items"])
|
|
if isinstance(payload.get("boundaries"), list):
|
|
boundaries.extend(payload["boundaries"])
|
|
write = helpers["write_json_artifact"]
|
|
annotations = {
|
|
"datasetType": "annotation-gate-input",
|
|
"templateId": template_id,
|
|
"supplierId": supplier_id,
|
|
"itemCount": len(clips),
|
|
"items": prelabels,
|
|
}
|
|
rejected = {"datasetType": "annotation-gate-rejected", "itemCount": 0, "items": []}
|
|
summary = {"clipCount": len(clips), "prelabelCount": len(prelabels), "boundaryCount": len(boundaries), "templateId": template_id, "supplierId": supplier_id}
|
|
return [
|
|
*clips,
|
|
write(run_id, node_id, output_dir, "annotation-gate-annotations-dataset.json", annotations, output=_output(node_data, helpers, "annotations")),
|
|
write(run_id, node_id, output_dir, "annotation-gate-rejected-dataset.json", rejected, output=_output(node_data, helpers, "rejected")),
|
|
write(run_id, node_id, output_dir, "annotation-gate-aggregate-summary.json", summary, output=_output(node_data, helpers, "aggregateSummary")),
|
|
]
|
|
|
|
|
|
def run(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: Any,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
if _output(node_data, helpers, "approvedVideos") is not None:
|
|
return _run_multiport(run_id, node_id, output_dir, node_data, inputs, helpers)
|
|
return _run_legacy(run_id, node_id, output_dir, node_data, inputs, helpers)
|