2026-08-17 20:44:16 +08:00

110 lines
4.1 KiB
Python

from __future__ import annotations
from typing import Any, Callable
HelperMap = dict[str, Callable[..., Any]]
def _label_index(raw_labels: list[Any]) -> tuple[dict[str, dict[str, Any]], list[dict[str, Any]]]:
index: dict[str, dict[str, Any]] = {}
labels: list[dict[str, Any]] = []
for position, raw in enumerate(raw_labels):
if isinstance(raw, str):
item = {"id": f"label-{position + 1}", "name": raw.strip(), "aliases": []}
elif isinstance(raw, dict):
item = {
"id": str(raw.get("id") or f"label-{position + 1}").strip(),
"name": str(raw.get("name") or "").strip(),
"aliases": [str(value).strip() for value in raw.get("aliases", []) if str(value).strip()],
}
else:
continue
if not item["name"]:
continue
labels.append(item)
for candidate in [item["id"], item["name"], *item["aliases"]]:
index[candidate.lower()] = item
return index, labels
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)
template_version = str(params.get("TEMPLATE_VERSION", "v1")).strip() or "v1"
raw_labels = helpers["parse_json_param"](params, "LABEL_SCHEMA_JSON", [], list)
phrases = helpers["parse_json_param"](params, "PHRASES_JSON", [], list)
minimum_confidence = helpers["parse_unit_interval_param"](params, "MINIMUM_CONFIDENCE", 0.0)
label_by_alias, labels = _label_index(raw_labels)
if not labels:
raise RuntimeError("LABEL_SCHEMA_JSON must define at least one semantic label")
payload = helpers["first_payload_with_keys"](inputs, "annotationCount", "items")
if payload is None:
raise RuntimeError("版本化语义预标需要上游视频标注结果")
items: list[dict[str, Any]] = []
segment_count = 0
for item in payload.get("items", []):
if not isinstance(item, dict):
continue
annotation = item.get("annotation") if isinstance(item.get("annotation"), dict) else {}
segments = []
for position, action in enumerate(annotation.get("actions", [])):
if not isinstance(action, dict):
continue
confidence = float(action.get("confidence", 0.0) or 0.0)
if confidence < minimum_confidence:
continue
raw_label = str(action.get("label", "")).strip()
definition = label_by_alias.get(raw_label.lower())
if definition is None:
continue
segments.append(
{
"id": f"{item.get('clipId', 'clip')}-segment-{position + 1}",
"labelId": definition["id"],
"label": definition["name"],
"start": float(action.get("start", 0.0) or 0.0),
"end": float(action.get("end", 0.0) or 0.0),
"confidence": confidence,
"phrases": [str(value) for value in phrases],
"source": "automatic_prelabel",
}
)
segment_count += len(segments)
items.append(
{
**item,
"semanticAnnotation": {
"schemaVersion": "droboticflow.semantic.annotation/v1",
"templateVersion": template_version,
"segments": segments,
},
}
)
manifest = {
"schemaVersion": "droboticflow.semantic.prelabels/v1",
"datasetType": "semantic-prelabels",
"templateVersion": template_version,
"labels": labels,
"phrases": phrases,
"annotationCount": len(items),
"segmentCount": segment_count,
"items": items,
}
return [
helpers["write_json_artifact"](
run_id,
node_id,
output_dir,
"semantic-prelabels.json",
manifest,
output=helpers["output_binding"](node_data),
)
]