Spaces:
Runtime error
Runtime error
Khunndev
Sisyphus
Make client NMS and centroid dedup O(n) with a spatial grid for dense bundles.
4608266 Download frontend/src/lib/nms.ts from npisb/pipecountapp-dev: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/spaces/npisb/pipecountapp-dev/resolve/main/frontend/src/lib/nms.ts
- Command line
-
hf download hf://spaces/npisb/pipecountapp-dev/frontend/src/lib/nms.ts
-
curl -L -o nms.ts https://huggingface.co/spaces/npisb/pipecountapp-dev/resolve/main/frontend/src/lib/nms.ts
4.23 kB
| import type { BBox } from './types' | |
| export interface RawDetection extends BBox { | |
| confidence: number | |
| } | |
| function iou(a: BBox, b: BBox): number { | |
| const x1 = Math.max(a.xmin, b.xmin) | |
| const y1 = Math.max(a.ymin, b.ymin) | |
| const x2 = Math.min(a.xmax, b.xmax) | |
| const y2 = Math.min(a.ymax, b.ymax) | |
| const inter = Math.max(0, x2 - x1) * Math.max(0, y2 - y1) | |
| const areaA = (a.xmax - a.xmin) * (a.ymax - a.ymin) | |
| const areaB = (b.xmax - b.xmin) * (b.ymax - b.ymin) | |
| return inter / (areaA + areaB - inter + 1e-6) | |
| } | |
| export function nonMaxSuppression( | |
| detections: RawDetection[], | |
| iouThreshold: number, | |
| maxDet: number, | |
| ): RawDetection[] { | |
| return nmsGrid(detections, iou, iouThreshold, maxDet) | |
| } | |
| /** Intersection-Over-Smaller: metric(a,b) = interArea / min(areaA, areaB). */ | |
| function ios(a: BBox, b: BBox): number { | |
| const x1 = Math.max(a.xmin, b.xmin) | |
| const y1 = Math.max(a.ymin, b.ymin) | |
| const x2 = Math.min(a.xmax, b.xmax) | |
| const y2 = Math.min(a.ymax, b.ymax) | |
| const inter = Math.max(0, x2 - x1) * Math.max(0, y2 - y1) | |
| const areaA = (a.xmax - a.xmin) * (a.ymax - a.ymin) | |
| const areaB = (b.xmax - b.xmin) * (b.ymax - b.ymin) | |
| const smaller = Math.min(areaA, areaB) | |
| if (smaller <= 0) return 0 | |
| return inter / smaller | |
| } | |
| type Metric = (a: BBox, b: BBox) => number | |
| /** | |
| * Greedy NMS accelerated by a uniform spatial grid. EXACT — it only skips | |
| * pairs that cannot share a grid cell, and two boxes can only have IoU/IOS > 0 | |
| * if they geometrically overlap (hence share a cell). Each kept box is inserted | |
| * into every cell its bbox covers; a candidate checks the buckets overlapping | |
| * its own bbox. For uniformly sized detections (pipe ends) this is ~O(n) | |
| * instead of O(n^2) — dense bundles of 2000+ pipes were spending seconds in the | |
| * brute-force scan on phones. | |
| */ | |
| function nmsGrid( | |
| detections: RawDetection[], | |
| metric: Metric, | |
| threshold: number, | |
| maxDet: number, | |
| ): RawDetection[] { | |
| if (detections.length === 0) return detections | |
| const sorted = [...detections].sort((a, b) => b.confidence - a.confidence) | |
| // Cell size from the median box so a typical detection occupies ~1 cell. | |
| const sizes = sorted | |
| .map((d) => Math.min(d.xmax - d.xmin, d.ymax - d.ymin)) | |
| .sort((a, b) => a - b) | |
| const cell = Math.max(1, sizes[Math.floor(sizes.length / 2)]) | |
| const grid = new Map<number, RawDetection[]>() | |
| const visitCells = (box: BBox, cb: (key: number) => void) => { | |
| const x0 = Math.floor(box.xmin / cell) | |
| const x1 = Math.floor((box.xmax - 1e-6) / cell) | |
| const y0 = Math.floor(box.ymin / cell) | |
| const y1 = Math.floor((box.ymax - 1e-6) / cell) | |
| for (let gx = x0; gx <= x1; gx++) { | |
| for (let gy = y0; gy <= y1; gy++) { | |
| cb(gx * 100000 + gy) | |
| } | |
| } | |
| } | |
| const kept: RawDetection[] = [] | |
| for (const det of sorted) { | |
| let skip = false | |
| const seen = new Set<RawDetection>() | |
| visitCells(det, (key) => { | |
| if (skip) return | |
| const bucket = grid.get(key) | |
| if (!bucket) return | |
| for (const b of bucket) { | |
| if (seen.has(b)) continue | |
| seen.add(b) | |
| if (metric(det, b) > threshold) { | |
| skip = true | |
| return | |
| } | |
| } | |
| }) | |
| if (!skip) { | |
| kept.push(det) | |
| visitCells(det, (key) => { | |
| let bucket = grid.get(key) | |
| if (!bucket) { | |
| bucket = [] | |
| grid.set(key, bucket) | |
| } | |
| bucket.push(det) | |
| }) | |
| } | |
| if (kept.length >= maxDet) break | |
| } | |
| return kept | |
| } | |
| /** Greedy NMS using the intersection-over-smaller metric (good for dedup of nested boxes). */ | |
| export function nmsIOS( | |
| detections: RawDetection[], | |
| iosThreshold: number, | |
| maxDet: number, | |
| ): RawDetection[] { | |
| return nmsGrid(detections, ios, iosThreshold, maxDet) | |
| } | |
| export function filterOutliers( | |
| detections: RawDetection[], | |
| lowerMult = 0.25, | |
| upperMult = 4, | |
| ): RawDetection[] { | |
| if (detections.length < 3) { | |
| return detections | |
| } | |
| const areas = detections.map((d) => (d.xmax - d.xmin) * (d.ymax - d.ymin)) | |
| const sorted = [...areas].sort((a, b) => a - b) | |
| const median = sorted[Math.floor(sorted.length / 2)] | |
| return detections.filter((_, i) => { | |
| const area = areas[i] | |
| return area >= median * lowerMult && area <= median * upperMult | |
| }) | |
| } | |