Spaces:
Runtime error
Runtime error
Khunndev Cursor commited on
Commit ·
190dbf0
1
Parent(s): 6544822
Add inference post-processing: conf 0.6, IoU 0.35, box filter, and center merge.
Browse files- Dockerfile +1 -1
- detect.py +10 -6
- frontend/src/lib/constants.ts +10 -3
- frontend/src/lib/inference.ts +17 -29
- frontend/src/lib/postprocess.ts +94 -0
- postprocess.py +90 -0
Dockerfile
CHANGED
|
@@ -17,7 +17,7 @@ RUN apt-get update \
|
|
| 17 |
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
COPY requirements-server.txt .
|
| 19 |
RUN pip install --no-cache-dir -r requirements-server.txt
|
| 20 |
-
COPY main.py detect.py detect_limiter.py ./
|
| 21 |
COPY --from=frontend-build /frontend/dist ./static
|
| 22 |
RUN useradd -m appuser && chown -R appuser:appuser /app
|
| 23 |
USER appuser
|
|
|
|
| 17 |
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
COPY requirements-server.txt .
|
| 19 |
RUN pip install --no-cache-dir -r requirements-server.txt
|
| 20 |
+
COPY main.py detect.py detect_limiter.py postprocess.py ./
|
| 21 |
COPY --from=frontend-build /frontend/dist ./static
|
| 22 |
RUN useradd -m appuser && chown -R appuser:appuser /app
|
| 23 |
USER appuser
|
detect.py
CHANGED
|
@@ -11,13 +11,15 @@ from pathlib import Path
|
|
| 11 |
from threading import Lock
|
| 12 |
from typing import Any
|
| 13 |
|
|
|
|
|
|
|
| 14 |
STATIC_PATH = Path(__file__).resolve().parent / "static"
|
| 15 |
MODEL_DIR = STATIC_PATH / "models"
|
| 16 |
|
| 17 |
INFERENCE_SIZE = 1024
|
| 18 |
INFERENCE_SIZE_SMALL = 640
|
| 19 |
-
CONF_THRESHOLD = 0.
|
| 20 |
-
IOU_THRESHOLD = 0.
|
| 21 |
MAX_DET = 4000
|
| 22 |
TILE_OVERLAP = 0.28
|
| 23 |
|
|
@@ -229,10 +231,12 @@ def detect_bytes(
|
|
| 229 |
keep = _nms(boxes, scores)
|
| 230 |
boxes = [boxes[i] for i in keep]
|
| 231 |
scores = [scores[i] for i in keep]
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
ms = (time.perf_counter() - t0) * 1000
|
| 238 |
detections = [
|
|
|
|
| 11 |
from threading import Lock
|
| 12 |
from typing import Any
|
| 13 |
|
| 14 |
+
from postprocess import apply_detection_postprocess
|
| 15 |
+
|
| 16 |
STATIC_PATH = Path(__file__).resolve().parent / "static"
|
| 17 |
MODEL_DIR = STATIC_PATH / "models"
|
| 18 |
|
| 19 |
INFERENCE_SIZE = 1024
|
| 20 |
INFERENCE_SIZE_SMALL = 640
|
| 21 |
+
CONF_THRESHOLD = 0.6
|
| 22 |
+
IOU_THRESHOLD = 0.35
|
| 23 |
MAX_DET = 4000
|
| 24 |
TILE_OVERLAP = 0.28
|
| 25 |
|
|
|
|
| 231 |
keep = _nms(boxes, scores)
|
| 232 |
boxes = [boxes[i] for i in keep]
|
| 233 |
scores = [scores[i] for i in keep]
|
| 234 |
+
boxes, scores = apply_detection_postprocess(
|
| 235 |
+
boxes,
|
| 236 |
+
scores,
|
| 237 |
+
extra_outlier_filter=filter_outliers_flag,
|
| 238 |
+
filter_outliers_fn=_filter_outliers,
|
| 239 |
+
)
|
| 240 |
|
| 241 |
ms = (time.perf_counter() - t0) * 1000
|
| 242 |
detections = [
|
frontend/src/lib/constants.ts
CHANGED
|
@@ -3,11 +3,18 @@ export const MODEL_640_URL = '/models/countmodel-640.onnx'
|
|
| 3 |
export const MODEL_INT8_URL = '/models/countmodel-int8.onnx'
|
| 4 |
export const INFERENCE_SIZE = 1024
|
| 5 |
export const INFERENCE_SIZE_SMALL = 640
|
| 6 |
-
/**
|
| 7 |
-
export const CONFIDENCE_THRESHOLD = 0.
|
| 8 |
-
export const IOU_THRESHOLD = 0.
|
| 9 |
export const TILE_OVERLAP = 0.28
|
| 10 |
export const MAX_DETECTIONS = 4000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
export const GROUP_COLORS = [
|
| 13 |
'#ef4444',
|
|
|
|
| 3 |
export const MODEL_INT8_URL = '/models/countmodel-int8.onnx'
|
| 4 |
export const INFERENCE_SIZE = 1024
|
| 5 |
export const INFERENCE_SIZE_SMALL = 640
|
| 6 |
+
/** Precision-focused defaults — raise conf / lower NMS IoU to cut duplicate boxes. */
|
| 7 |
+
export const CONFIDENCE_THRESHOLD = 0.6
|
| 8 |
+
export const IOU_THRESHOLD = 0.35
|
| 9 |
export const TILE_OVERLAP = 0.28
|
| 10 |
export const MAX_DETECTIONS = 4000
|
| 11 |
+
/** Median-area multipliers for box-size filter (step 3). */
|
| 12 |
+
export const BOX_SIZE_LOWER_MULT = 0.35
|
| 13 |
+
export const BOX_SIZE_UPPER_MULT = 2.8
|
| 14 |
+
/** Center-cluster merge radius scales with median pipe width (step 4). */
|
| 15 |
+
export const CENTER_MERGE_MIN_PX = 12
|
| 16 |
+
export const CENTER_MERGE_MAX_PX = 40
|
| 17 |
+
export const CENTER_MERGE_WIDTH_RATIO = 0.4
|
| 18 |
|
| 19 |
export const GROUP_COLORS = [
|
| 20 |
'#ef4444',
|
frontend/src/lib/inference.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
| 7 |
type EnhanceOptions,
|
| 8 |
} from './imageEnhance'
|
| 9 |
import { selectModelForImage } from './modelSelect'
|
| 10 |
-
import {
|
| 11 |
import { canvasToTensor, letterboxCanvas, mapBoxToOriginal } from './preprocess'
|
| 12 |
import type { Detection, LetterboxMeta } from './types'
|
| 13 |
|
|
@@ -164,21 +164,13 @@ async function inferCanvas(
|
|
| 164 |
offsetY: number,
|
| 165 |
regionW: number,
|
| 166 |
regionH: number,
|
| 167 |
-
filterOutliersFlag: boolean,
|
| 168 |
): Promise<Detection[]> {
|
| 169 |
const { canvas, meta } = letterboxCanvas(source, inferenceSize)
|
| 170 |
const tensor = canvasToTensor(canvas, inferenceSize)
|
| 171 |
const input = new ort.Tensor('float32', tensor, [1, 3, inferenceSize, inferenceSize])
|
| 172 |
const results = await session.run({ images: input })
|
| 173 |
const output = results.output.data as Float32Array
|
| 174 |
-
const dets = decodeOutput(
|
| 175 |
-
output,
|
| 176 |
-
meta,
|
| 177 |
-
regionW,
|
| 178 |
-
regionH,
|
| 179 |
-
CONFIDENCE_THRESHOLD,
|
| 180 |
-
filterOutliersFlag,
|
| 181 |
-
)
|
| 182 |
return dets.map((d) => ({
|
| 183 |
...d,
|
| 184 |
xmin: d.xmin + offsetX,
|
|
@@ -194,7 +186,6 @@ export function decodeOutput(
|
|
| 194 |
imageW: number,
|
| 195 |
imageH: number,
|
| 196 |
confThreshold = CONFIDENCE_THRESHOLD,
|
| 197 |
-
applyOutlierFilter = false,
|
| 198 |
): Detection[] {
|
| 199 |
const rows = output.length / 6
|
| 200 |
const raw = []
|
|
@@ -222,10 +213,7 @@ export function decodeOutput(
|
|
| 222 |
raw.push({ ...box, confidence })
|
| 223 |
}
|
| 224 |
|
| 225 |
-
|
| 226 |
-
if (applyOutlierFilter) {
|
| 227 |
-
filtered = filterOutliers(filtered)
|
| 228 |
-
}
|
| 229 |
return filtered.map((d, idx) => ({
|
| 230 |
...d,
|
| 231 |
id: `det-${idx}-${Date.now()}`,
|
|
@@ -269,18 +257,17 @@ export async function runDetection(
|
|
| 269 |
|
| 270 |
options?.onProgress?.(enhance ? 'Enhancing image...' : 'Preparing image...')
|
| 271 |
const source = prepareSource(image, enhance, options?.enhanceOptions)
|
| 272 |
-
const dets = await inferCanvas(
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
return dets
|
| 284 |
}
|
| 285 |
|
| 286 |
async function runTiledDetection(
|
|
@@ -335,13 +322,14 @@ async function runTiledDetection(
|
|
| 335 |
y1,
|
| 336 |
cropW,
|
| 337 |
cropH,
|
| 338 |
-
options?.filterOutliers ?? false,
|
| 339 |
)
|
| 340 |
all.push(...dets)
|
| 341 |
}
|
| 342 |
}
|
| 343 |
|
| 344 |
-
const merged =
|
|
|
|
|
|
|
| 345 |
...d,
|
| 346 |
id: `merged-${idx}`,
|
| 347 |
groupId: 0,
|
|
|
|
| 7 |
type EnhanceOptions,
|
| 8 |
} from './imageEnhance'
|
| 9 |
import { selectModelForImage } from './modelSelect'
|
| 10 |
+
import { nonMaxSuppression, postProcessPipeline } from './postprocess'
|
| 11 |
import { canvasToTensor, letterboxCanvas, mapBoxToOriginal } from './preprocess'
|
| 12 |
import type { Detection, LetterboxMeta } from './types'
|
| 13 |
|
|
|
|
| 164 |
offsetY: number,
|
| 165 |
regionW: number,
|
| 166 |
regionH: number,
|
|
|
|
| 167 |
): Promise<Detection[]> {
|
| 168 |
const { canvas, meta } = letterboxCanvas(source, inferenceSize)
|
| 169 |
const tensor = canvasToTensor(canvas, inferenceSize)
|
| 170 |
const input = new ort.Tensor('float32', tensor, [1, 3, inferenceSize, inferenceSize])
|
| 171 |
const results = await session.run({ images: input })
|
| 172 |
const output = results.output.data as Float32Array
|
| 173 |
+
const dets = decodeOutput(output, meta, regionW, regionH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
return dets.map((d) => ({
|
| 175 |
...d,
|
| 176 |
xmin: d.xmin + offsetX,
|
|
|
|
| 186 |
imageW: number,
|
| 187 |
imageH: number,
|
| 188 |
confThreshold = CONFIDENCE_THRESHOLD,
|
|
|
|
| 189 |
): Detection[] {
|
| 190 |
const rows = output.length / 6
|
| 191 |
const raw = []
|
|
|
|
| 213 |
raw.push({ ...box, confidence })
|
| 214 |
}
|
| 215 |
|
| 216 |
+
const filtered = nonMaxSuppression(raw, IOU_THRESHOLD, MAX_DETECTIONS)
|
|
|
|
|
|
|
|
|
|
| 217 |
return filtered.map((d, idx) => ({
|
| 218 |
...d,
|
| 219 |
id: `det-${idx}-${Date.now()}`,
|
|
|
|
| 257 |
|
| 258 |
options?.onProgress?.(enhance ? 'Enhancing image...' : 'Preparing image...')
|
| 259 |
const source = prepareSource(image, enhance, options?.enhanceOptions)
|
| 260 |
+
const dets = await inferCanvas(source, session, inferenceSize, 0, 0, w, h)
|
| 261 |
+
const processed = postProcessPipeline(dets, {
|
| 262 |
+
extraOutlierFilter: options?.filterOutliers ?? false,
|
| 263 |
+
})
|
| 264 |
+
options?.onProgress?.(`Detected ${processed.length} pipes in ${Math.round(performance.now() - start)}ms`)
|
| 265 |
+
return processed.map((d, idx) => ({
|
| 266 |
+
...d,
|
| 267 |
+
id: `det-${idx}-${Date.now()}`,
|
| 268 |
+
groupId: 0,
|
| 269 |
+
confidence: d.confidence,
|
| 270 |
+
}))
|
|
|
|
| 271 |
}
|
| 272 |
|
| 273 |
async function runTiledDetection(
|
|
|
|
| 322 |
y1,
|
| 323 |
cropW,
|
| 324 |
cropH,
|
|
|
|
| 325 |
)
|
| 326 |
all.push(...dets)
|
| 327 |
}
|
| 328 |
}
|
| 329 |
|
| 330 |
+
const merged = postProcessPipeline(all, {
|
| 331 |
+
extraOutlierFilter: options?.filterOutliers ?? false,
|
| 332 |
+
}).map((d, idx) => ({
|
| 333 |
...d,
|
| 334 |
id: `merged-${idx}`,
|
| 335 |
groupId: 0,
|
frontend/src/lib/postprocess.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Shared detection post-processing (no retrain).
|
| 3 |
+
* Pipeline: NMS → box-size filter → center-cluster merge → optional extra outlier pass.
|
| 4 |
+
*/
|
| 5 |
+
import type { RawDetection } from './nms'
|
| 6 |
+
import { filterOutliers, nonMaxSuppression } from './nms'
|
| 7 |
+
import {
|
| 8 |
+
BOX_SIZE_LOWER_MULT,
|
| 9 |
+
BOX_SIZE_UPPER_MULT,
|
| 10 |
+
CENTER_MERGE_MAX_PX,
|
| 11 |
+
CENTER_MERGE_MIN_PX,
|
| 12 |
+
CENTER_MERGE_WIDTH_RATIO,
|
| 13 |
+
IOU_THRESHOLD,
|
| 14 |
+
MAX_DETECTIONS,
|
| 15 |
+
} from './constants'
|
| 16 |
+
|
| 17 |
+
export { nonMaxSuppression }
|
| 18 |
+
|
| 19 |
+
function centerMergeEps(detections: RawDetection[]): number {
|
| 20 |
+
if (detections.length === 0) return 20
|
| 21 |
+
const widths = detections.map((d) => d.xmax - d.xmin).sort((a, b) => a - b)
|
| 22 |
+
const medianW = widths[Math.floor(widths.length / 2)]
|
| 23 |
+
return Math.max(
|
| 24 |
+
CENTER_MERGE_MIN_PX,
|
| 25 |
+
Math.min(CENTER_MERGE_MAX_PX, medianW * CENTER_MERGE_WIDTH_RATIO),
|
| 26 |
+
)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
/** Drop boxes much smaller/larger than the typical pipe end in this image. */
|
| 30 |
+
export function filterBoxSize(
|
| 31 |
+
detections: RawDetection[],
|
| 32 |
+
lowerMult = BOX_SIZE_LOWER_MULT,
|
| 33 |
+
upperMult = BOX_SIZE_UPPER_MULT,
|
| 34 |
+
): RawDetection[] {
|
| 35 |
+
if (detections.length < 3) return detections
|
| 36 |
+
return filterOutliers(detections, lowerMult, upperMult)
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* Merge duplicate detections on one pipe (keep highest confidence per cluster).
|
| 41 |
+
* DBSCAN-like single-linkage with eps scaled to median box width.
|
| 42 |
+
*/
|
| 43 |
+
export function mergeCenterClusters(
|
| 44 |
+
detections: RawDetection[],
|
| 45 |
+
epsPx?: number,
|
| 46 |
+
): RawDetection[] {
|
| 47 |
+
if (detections.length <= 1) return detections
|
| 48 |
+
|
| 49 |
+
const eps = epsPx ?? centerMergeEps(detections)
|
| 50 |
+
const epsSq = eps * eps
|
| 51 |
+
const sorted = [...detections].sort((a, b) => b.confidence - a.confidence)
|
| 52 |
+
const kept: RawDetection[] = []
|
| 53 |
+
const centers: Array<{ x: number; y: number }> = []
|
| 54 |
+
|
| 55 |
+
for (const det of sorted) {
|
| 56 |
+
const cx = (det.xmin + det.xmax) / 2
|
| 57 |
+
const cy = (det.ymin + det.ymax) / 2
|
| 58 |
+
let duplicate = false
|
| 59 |
+
for (const c of centers) {
|
| 60 |
+
const dx = cx - c.x
|
| 61 |
+
const dy = cy - c.y
|
| 62 |
+
if (dx * dx + dy * dy < epsSq) {
|
| 63 |
+
duplicate = true
|
| 64 |
+
break
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
if (!duplicate) {
|
| 68 |
+
kept.push(det)
|
| 69 |
+
centers.push({ x: cx, y: cy })
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
return kept
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
export function applyDetectionPostProcess(
|
| 77 |
+
detections: RawDetection[],
|
| 78 |
+
options?: { extraOutlierFilter?: boolean },
|
| 79 |
+
): RawDetection[] {
|
| 80 |
+
let out = filterBoxSize(detections)
|
| 81 |
+
out = mergeCenterClusters(out)
|
| 82 |
+
if (options?.extraOutlierFilter) {
|
| 83 |
+
out = filterOutliers(out)
|
| 84 |
+
}
|
| 85 |
+
return out
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
export function postProcessPipeline(
|
| 89 |
+
raw: RawDetection[],
|
| 90 |
+
options?: { extraOutlierFilter?: boolean },
|
| 91 |
+
): RawDetection[] {
|
| 92 |
+
const nms = nonMaxSuppression(raw, IOU_THRESHOLD, MAX_DETECTIONS)
|
| 93 |
+
return applyDetectionPostProcess(nms, options)
|
| 94 |
+
}
|
postprocess.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Detection post-processing shared by server inference (no retrain)."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
BOX_SIZE_LOWER_MULT = 0.35
|
| 5 |
+
BOX_SIZE_UPPER_MULT = 2.8
|
| 6 |
+
CENTER_MERGE_MIN_PX = 12.0
|
| 7 |
+
CENTER_MERGE_MAX_PX = 40.0
|
| 8 |
+
CENTER_MERGE_WIDTH_RATIO = 0.4
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def _center_merge_eps(boxes: list) -> float:
|
| 12 |
+
if len(boxes) < 2:
|
| 13 |
+
return 20.0
|
| 14 |
+
widths = sorted(b[2] - b[0] for b in boxes)
|
| 15 |
+
median_w = widths[len(widths) // 2]
|
| 16 |
+
return max(
|
| 17 |
+
CENTER_MERGE_MIN_PX,
|
| 18 |
+
min(CENTER_MERGE_MAX_PX, median_w * CENTER_MERGE_WIDTH_RATIO),
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def filter_box_size(
|
| 23 |
+
boxes: list,
|
| 24 |
+
scores: list,
|
| 25 |
+
lower_mult: float = BOX_SIZE_LOWER_MULT,
|
| 26 |
+
upper_mult: float = BOX_SIZE_UPPER_MULT,
|
| 27 |
+
) -> tuple[list, list]:
|
| 28 |
+
if len(boxes) < 3:
|
| 29 |
+
return boxes, scores
|
| 30 |
+
areas = [(b[2] - b[0]) * (b[3] - b[1]) for b in boxes]
|
| 31 |
+
sorted_areas = sorted(areas)
|
| 32 |
+
median = sorted_areas[len(sorted_areas) // 2]
|
| 33 |
+
lo = median * lower_mult
|
| 34 |
+
hi = median * upper_mult
|
| 35 |
+
out_boxes, out_scores = [], []
|
| 36 |
+
for b, s, a in zip(boxes, scores, areas):
|
| 37 |
+
if lo <= a <= hi:
|
| 38 |
+
out_boxes.append(b)
|
| 39 |
+
out_scores.append(s)
|
| 40 |
+
return out_boxes, out_scores
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def merge_center_clusters(
|
| 44 |
+
boxes: list,
|
| 45 |
+
scores: list,
|
| 46 |
+
eps_px: float | None = None,
|
| 47 |
+
) -> tuple[list, list]:
|
| 48 |
+
if len(boxes) <= 1:
|
| 49 |
+
return boxes, scores
|
| 50 |
+
|
| 51 |
+
eps = eps_px if eps_px is not None else _center_merge_eps(boxes)
|
| 52 |
+
eps_sq = eps * eps
|
| 53 |
+
order = sorted(range(len(boxes)), key=lambda i: scores[i], reverse=True)
|
| 54 |
+
kept_boxes: list = []
|
| 55 |
+
kept_scores: list = []
|
| 56 |
+
centers: list[tuple[float, float]] = []
|
| 57 |
+
|
| 58 |
+
for i in order:
|
| 59 |
+
b = boxes[i]
|
| 60 |
+
cx = (b[0] + b[2]) / 2
|
| 61 |
+
cy = (b[1] + b[3]) / 2
|
| 62 |
+
duplicate = False
|
| 63 |
+
for px, py in centers:
|
| 64 |
+
dx = cx - px
|
| 65 |
+
dy = cy - py
|
| 66 |
+
if dx * dx + dy * dy < eps_sq:
|
| 67 |
+
duplicate = True
|
| 68 |
+
break
|
| 69 |
+
if not duplicate:
|
| 70 |
+
kept_boxes.append(b)
|
| 71 |
+
kept_scores.append(scores[i])
|
| 72 |
+
centers.append((cx, cy))
|
| 73 |
+
|
| 74 |
+
return kept_boxes, kept_scores
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def apply_detection_postprocess(
|
| 78 |
+
boxes: list,
|
| 79 |
+
scores: list,
|
| 80 |
+
*,
|
| 81 |
+
extra_outlier_filter: bool = False,
|
| 82 |
+
filter_outliers_fn=None,
|
| 83 |
+
) -> tuple[list, list]:
|
| 84 |
+
boxes, scores = filter_box_size(boxes, scores)
|
| 85 |
+
boxes, scores = merge_center_clusters(boxes, scores)
|
| 86 |
+
if extra_outlier_filter and filter_outliers_fn and boxes:
|
| 87 |
+
keep = filter_outliers_fn(boxes)
|
| 88 |
+
boxes = [boxes[i] for i in keep]
|
| 89 |
+
scores = [scores[i] for i in keep]
|
| 90 |
+
return boxes, scores
|