Suppress duplicate YOLO tile detections
This commit is contained in:
@@ -138,7 +138,7 @@ class DetectionService:
|
||||
|
||||
if model.model_id == resolved_settings.yolo_model_id:
|
||||
try:
|
||||
detections = DetectionService._run_configured_yolo(
|
||||
detections, postprocess_summary = DetectionService._run_configured_yolo(
|
||||
db=db,
|
||||
project_id=project_id,
|
||||
dataset_id=dataset_id,
|
||||
@@ -165,7 +165,7 @@ class DetectionService:
|
||||
error_code=exc.code,
|
||||
message=exc.message,
|
||||
)
|
||||
DetectionService._mark_success(db, analysis_run, job, detection_count=len(detections))
|
||||
DetectionService._mark_success(db, analysis_run, job, detection_count=len(detections), extra_result=postprocess_summary)
|
||||
return DetectionRunResponse(
|
||||
analysis_run_id=analysis_run.id,
|
||||
job_id=job.id,
|
||||
@@ -454,8 +454,10 @@ class DetectionService:
|
||||
db.refresh(job)
|
||||
|
||||
@staticmethod
|
||||
def _mark_success(db, analysis_run: AnalysisRun, job: Job, detection_count: int) -> None:
|
||||
def _mark_success(db, analysis_run: AnalysisRun, job: Job, detection_count: int, extra_result: dict[str, Any] | None = None) -> None:
|
||||
result = {"detection_count": detection_count}
|
||||
if extra_result:
|
||||
result.update(extra_result)
|
||||
analysis_run.status = "success"
|
||||
analysis_run.finished_at = DetectionService._now()
|
||||
analysis_run.result_json = result
|
||||
@@ -536,13 +538,13 @@ class DetectionService:
|
||||
class_filter: list[str],
|
||||
settings: Settings,
|
||||
yolo_adapter_class: Type[YoloDetectionAdapter],
|
||||
) -> list[Detection]:
|
||||
) -> tuple[list[Detection], dict[str, Any]]:
|
||||
manifest = DetectionService._load_tile_manifest(tile_manifest_path, settings.yolo_max_tiles)
|
||||
model_path = Path(settings.yolo_model_path or "").expanduser()
|
||||
adapter = yolo_adapter_class(settings)
|
||||
model = adapter.load_model(model_path)
|
||||
allowed_classes = {DetectionService._canonical_class_name(value) for value in class_filter if DetectionService._canonical_class_name(value)}
|
||||
persisted: list[Detection] = []
|
||||
candidates: list[dict[str, Any]] = []
|
||||
manifest_crs = manifest.get("crs") or manifest.get("source_crs") or manifest.get("dataset_crs") or "EPSG:4326"
|
||||
for tile in manifest["tiles"]:
|
||||
tile_path = DetectionService._resolve_tile_path(tile, Path(tile_manifest_path or "").expanduser())
|
||||
@@ -561,37 +563,87 @@ class DetectionService:
|
||||
properties = dict(raw.get("properties") or {})
|
||||
if model_class_name and model_class_name != class_name:
|
||||
properties.setdefault("model_class_name", model_class_name)
|
||||
detection = Detection(
|
||||
id=uuid.uuid4(),
|
||||
project_id=project_id,
|
||||
dataset_id=dataset_id,
|
||||
analysis_run_id=analysis_run.id,
|
||||
job_id=job.id,
|
||||
model_name=model_name,
|
||||
model_version=model_version,
|
||||
class_name=class_name,
|
||||
confidence=confidence,
|
||||
geometry=from_shape(geometry, srid=4326),
|
||||
bbox_json={
|
||||
"x_min": float(bbox[0]),
|
||||
"y_min": float(bbox[1]),
|
||||
"x_max": float(bbox[2]),
|
||||
"y_max": float(bbox[3]),
|
||||
},
|
||||
source_tile_path=str(tile_path),
|
||||
properties_json={**properties, "tile_index": tile.get("index")},
|
||||
candidates.append(
|
||||
{
|
||||
"class_name": class_name,
|
||||
"confidence": confidence,
|
||||
"geometry": geometry,
|
||||
"bbox": bbox,
|
||||
"source_tile_path": str(tile_path),
|
||||
"properties": {**properties, "tile_index": tile.get("index")},
|
||||
}
|
||||
)
|
||||
db.add(detection)
|
||||
persisted.append(detection)
|
||||
filtered_candidates = DetectionService._suppress_duplicate_candidates(
|
||||
candidates,
|
||||
iou_threshold=float(settings.yolo_duplicate_iou_threshold),
|
||||
)
|
||||
persisted: list[Detection] = []
|
||||
for candidate in filtered_candidates:
|
||||
bbox = candidate["bbox"]
|
||||
detection = Detection(
|
||||
id=uuid.uuid4(),
|
||||
project_id=project_id,
|
||||
dataset_id=dataset_id,
|
||||
analysis_run_id=analysis_run.id,
|
||||
job_id=job.id,
|
||||
model_name=model_name,
|
||||
model_version=model_version,
|
||||
class_name=candidate["class_name"],
|
||||
confidence=candidate["confidence"],
|
||||
geometry=from_shape(candidate["geometry"], srid=4326),
|
||||
bbox_json={
|
||||
"x_min": float(bbox[0]),
|
||||
"y_min": float(bbox[1]),
|
||||
"x_max": float(bbox[2]),
|
||||
"y_max": float(bbox[3]),
|
||||
},
|
||||
source_tile_path=candidate["source_tile_path"],
|
||||
properties_json=candidate["properties"],
|
||||
)
|
||||
db.add(detection)
|
||||
persisted.append(detection)
|
||||
db.commit()
|
||||
for detection in persisted:
|
||||
db.refresh(detection)
|
||||
return persisted
|
||||
return persisted, {
|
||||
"raw_detection_count": len(candidates),
|
||||
"suppressed_detection_count": len(candidates) - len(filtered_candidates),
|
||||
"duplicate_iou_threshold": float(settings.yolo_duplicate_iou_threshold),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _canonical_class_name(value: Any) -> str:
|
||||
return str(value or "").strip().casefold()
|
||||
|
||||
@staticmethod
|
||||
def _suppress_duplicate_candidates(candidates: list[dict[str, Any]], iou_threshold: float) -> list[dict[str, Any]]:
|
||||
if iou_threshold <= 0 or len(candidates) < 2:
|
||||
return candidates
|
||||
kept: list[dict[str, Any]] = []
|
||||
for candidate in sorted(candidates, key=lambda item: float(item["confidence"]), reverse=True):
|
||||
duplicate = False
|
||||
for kept_candidate in kept:
|
||||
if candidate["class_name"] != kept_candidate["class_name"]:
|
||||
continue
|
||||
if DetectionService._geometry_iou(candidate["geometry"], kept_candidate["geometry"]) >= iou_threshold:
|
||||
duplicate = True
|
||||
break
|
||||
if not duplicate:
|
||||
kept.append(candidate)
|
||||
return kept
|
||||
|
||||
@staticmethod
|
||||
def _geometry_iou(left, right) -> float:
|
||||
if left.is_empty or right.is_empty:
|
||||
return 0.0
|
||||
intersection_area = left.intersection(right).area
|
||||
if intersection_area <= 0:
|
||||
return 0.0
|
||||
union_area = left.union(right).area
|
||||
if union_area <= 0:
|
||||
return 0.0
|
||||
return intersection_area / union_area
|
||||
|
||||
@staticmethod
|
||||
def _load_tile_manifest(tile_manifest_path: str | None, max_tiles: int) -> dict[str, Any]:
|
||||
if not tile_manifest_path:
|
||||
|
||||
Reference in New Issue
Block a user