Spaces:
Runtime error
Runtime error
File size: 4,228 Bytes
0e0cc3e 4608266 0e0cc3e b505c60 4608266 b505c60 4608266 b505c60 4608266 b505c60 4608266 b505c60 4608266 b505c60 4608266 b505c60 4608266 b505c60 4608266 b505c60 4608266 0e0cc3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 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
})
}
|