forked from longtao.wu/droboticflow
96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
import pathlib
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
|
|
|
|
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]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
payload = helpers["first_payload_with_keys"](inputs, "clipCount", "items")
|
|
if payload is None:
|
|
raise RuntimeError("视频标注需要上游切片结果输入")
|
|
frame_interval = helpers["parse_positive_float_param"](params, "FRAME_INTERVAL_SECONDS", 1.0)
|
|
clip_artifacts = {
|
|
str(item.get("name", "")).strip(): item
|
|
for item in inputs
|
|
if str(item.get("name", "")).strip().lower().endswith(".mp4")
|
|
}
|
|
archive_sources = payload.get("archiveSources", {}) if isinstance(payload.get("archiveSources"), dict) else {}
|
|
items = []
|
|
for clip in payload.get("items", []):
|
|
if not isinstance(clip, dict):
|
|
continue
|
|
clip_name = str(clip.get("clipName", "")).strip() or pathlib.Path(str(clip.get("clipPath", ""))).name
|
|
artifact = clip_artifacts.get(clip_name)
|
|
clip_path: pathlib.Path | None = None
|
|
if artifact is not None:
|
|
try:
|
|
clip_path = helpers["source_path_from_item"](
|
|
{"source": helpers["source_ref_from_artifact"](artifact), "path": artifact.get("path", "")},
|
|
clip_name,
|
|
output_dir,
|
|
archive_sources,
|
|
)
|
|
except RuntimeError:
|
|
clip_path = None
|
|
if clip_path is None:
|
|
direct_clip_path = str(clip.get("clipPath", "")).strip()
|
|
if direct_clip_path and pathlib.Path(direct_clip_path).exists():
|
|
clip_path = pathlib.Path(direct_clip_path)
|
|
if clip_path is None:
|
|
clip_path = helpers["materialize_clip_from_source"](clip, output_dir, archive_sources)
|
|
if clip_path is None or not clip_path.exists():
|
|
raise RuntimeError(f"无法获取真实输入文件: {clip_name}")
|
|
annotation = helpers["annotate_video_clip"](clip_path, frame_interval)
|
|
annotation_filename = f"{pathlib.Path(clip_name).stem}_annotation.json"
|
|
annotation_payload = {
|
|
"clipId": clip.get("clipId", pathlib.Path(clip_name).stem),
|
|
"sourceVideo": clip.get("sourceVideo", ""),
|
|
**annotation,
|
|
}
|
|
items.append({**clip, "annotation": annotation_payload, "annotationFile": annotation_filename})
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir / "annotations",
|
|
annotation_filename,
|
|
annotation_payload,
|
|
output=helpers["output_binding"](node_data),
|
|
)
|
|
manifest = {
|
|
"datasetType": "x-platform-annotations",
|
|
"annotationCount": len(items),
|
|
"items": items,
|
|
}
|
|
binding = helpers["output_binding"](node_data)
|
|
artifacts = [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"annotations-manifest.json",
|
|
manifest,
|
|
output=binding,
|
|
)
|
|
]
|
|
artifacts.extend(
|
|
helpers["collect_output_artifacts"](
|
|
run_id,
|
|
node_id,
|
|
output_dir / "annotations",
|
|
node_data=node_data,
|
|
output=binding,
|
|
)
|
|
)
|
|
return artifacts
|