forked from longtao.wu/droboticflow
171 lines
6.8 KiB
Python
171 lines
6.8 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".bmp", ".webp"}
|
|
|
|
|
|
def _int_param(params: dict[str, str], key: str, default: int) -> int:
|
|
raw = str(params.get(key, str(default)) or str(default)).strip()
|
|
try:
|
|
value = int(raw)
|
|
except ValueError as exc:
|
|
raise RuntimeError(f"{key} must be an integer") from exc
|
|
if value <= 0:
|
|
raise RuntimeError(f"{key} must be > 0")
|
|
return value
|
|
|
|
|
|
def _find_annotation_file(source_path: pathlib.Path) -> pathlib.Path | None:
|
|
if source_path.is_file() and source_path.suffix.lower() == ".json":
|
|
return source_path
|
|
preferred = sorted((source_path / "annotations").glob("*.json")) if source_path.is_dir() else []
|
|
if preferred:
|
|
return preferred[0]
|
|
candidates = sorted(source_path.rglob("*.json")) if source_path.is_dir() else []
|
|
return candidates[0] if candidates else None
|
|
|
|
|
|
def _image_roots(source_path: pathlib.Path, annotation_path: pathlib.Path, params: dict[str, str]) -> list[pathlib.Path]:
|
|
roots: list[pathlib.Path] = []
|
|
image_root = str(params.get("IMAGE_ROOT", "") or "").strip()
|
|
if image_root:
|
|
roots.append(pathlib.Path(image_root).expanduser())
|
|
base = source_path if source_path.is_dir() else source_path.parent
|
|
roots.extend([base / "images", annotation_path.parent.parent / "images", annotation_path.parent, base])
|
|
unique: list[pathlib.Path] = []
|
|
seen: set[str] = set()
|
|
for root in roots:
|
|
key = str(root.resolve()) if root.exists() else str(root)
|
|
if key not in seen:
|
|
seen.add(key)
|
|
unique.append(root)
|
|
return unique
|
|
|
|
|
|
def _resolve_image(file_name: str, roots: list[pathlib.Path]) -> pathlib.Path | None:
|
|
value = str(file_name or "").strip()
|
|
if not value:
|
|
return None
|
|
candidate = pathlib.Path(value)
|
|
if candidate.is_absolute() and candidate.exists():
|
|
return candidate
|
|
for root in roots:
|
|
resolved = root / value
|
|
if resolved.exists():
|
|
return resolved
|
|
return None
|
|
|
|
|
|
def _bbox_issue(annotation: dict[str, Any], image: dict[str, Any]) -> str:
|
|
bbox = annotation.get("bbox")
|
|
if not isinstance(bbox, list) or len(bbox) != 4:
|
|
return "invalid_bbox_shape"
|
|
try:
|
|
x, y, width, height = (float(item) for item in bbox)
|
|
except (TypeError, ValueError):
|
|
return "invalid_bbox_number"
|
|
if width <= 0 or height <= 0:
|
|
return "non_positive_bbox"
|
|
image_width = float(image.get("width") or 0)
|
|
image_height = float(image.get("height") or 0)
|
|
if image_width > 0 and image_height > 0:
|
|
if x < 0 or y < 0 or x + width > image_width + 1e-6 or y + height > image_height + 1e-6:
|
|
return "bbox_outside_image"
|
|
return ""
|
|
|
|
|
|
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)
|
|
source_path = helpers["resolve_source_path"](params, inputs)
|
|
if source_path is None:
|
|
raise RuntimeError("COCO 标注校验需要 SOURCE_PATH 指向 annotations JSON 或数据集目录")
|
|
max_issues = _int_param(params, "MAX_ISSUES", 100)
|
|
annotation_path = _find_annotation_file(source_path)
|
|
if annotation_path is None:
|
|
raise RuntimeError(f"{source_path} does not contain COCO annotation json")
|
|
payload = json.loads(annotation_path.read_text(encoding="utf-8"))
|
|
if not isinstance(payload, dict):
|
|
raise RuntimeError(f"{annotation_path} is not a COCO object")
|
|
images = payload.get("images", [])
|
|
annotations = payload.get("annotations", [])
|
|
categories = payload.get("categories", [])
|
|
if not isinstance(images, list) or not isinstance(annotations, list) or not isinstance(categories, list):
|
|
raise RuntimeError("COCO payload must contain images, annotations, and categories arrays")
|
|
|
|
image_by_id = {item.get("id"): item for item in images if isinstance(item, dict)}
|
|
category_by_id = {item.get("id"): item for item in categories if isinstance(item, dict)}
|
|
roots = _image_roots(source_path, annotation_path, params)
|
|
existing_images = 0
|
|
missing_images: list[dict[str, Any]] = []
|
|
for image in image_by_id.values():
|
|
resolved = _resolve_image(str(image.get("file_name", "")), roots)
|
|
if resolved is None:
|
|
missing_images.append({"id": image.get("id"), "fileName": image.get("file_name", "")})
|
|
else:
|
|
existing_images += 1
|
|
|
|
annotation_issues: list[dict[str, Any]] = []
|
|
class_counts: dict[str, int] = {}
|
|
segmentation_count = 0
|
|
for annotation in annotations:
|
|
if not isinstance(annotation, dict):
|
|
annotation_issues.append({"code": "invalid_annotation_object"})
|
|
continue
|
|
image = image_by_id.get(annotation.get("image_id"))
|
|
category = category_by_id.get(annotation.get("category_id"))
|
|
if image is None:
|
|
annotation_issues.append({"code": "unknown_image_id", "annotationId": annotation.get("id")})
|
|
continue
|
|
if category is None:
|
|
annotation_issues.append({"code": "unknown_category_id", "annotationId": annotation.get("id")})
|
|
bbox_code = _bbox_issue(annotation, image)
|
|
if bbox_code:
|
|
annotation_issues.append({"code": bbox_code, "annotationId": annotation.get("id"), "imageId": image.get("id")})
|
|
category_name = str((category or {}).get("name") or annotation.get("category_id"))
|
|
class_counts[category_name] = class_counts.get(category_name, 0) + 1
|
|
if annotation.get("segmentation"):
|
|
segmentation_count += 1
|
|
|
|
issues = []
|
|
if missing_images:
|
|
issues.append({"code": "missing_image_files", "count": len(missing_images), "examples": missing_images[:max_issues]})
|
|
if annotation_issues:
|
|
issues.append({"code": "invalid_annotations", "count": len(annotation_issues), "examples": annotation_issues[:max_issues]})
|
|
result = {
|
|
"datasetType": "coco-annotation-validation",
|
|
"sourcePath": str(source_path),
|
|
"annotationPath": str(annotation_path),
|
|
"valid": not issues,
|
|
"issueCount": len(issues),
|
|
"issues": issues,
|
|
"imageCount": len(image_by_id),
|
|
"existingImageCount": existing_images,
|
|
"missingImageCount": len(missing_images),
|
|
"annotationCount": len(annotations),
|
|
"categoryCount": len(category_by_id),
|
|
"segmentationAnnotationCount": segmentation_count,
|
|
"classCounts": class_counts,
|
|
}
|
|
return [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"coco-annotation-validation.json",
|
|
result,
|
|
output=helpers["output_binding"](node_data),
|
|
)
|
|
]
|