Khunndev Cursor commited on
Commit
c9976d7
·
1 Parent(s): 190dbf0

Fix circle labels mismatching dock total after tiled detection.

Browse files
frontend/src/components/ResultCanvas.tsx CHANGED
@@ -60,10 +60,10 @@ function sortSpatially(detections: Detection[]): RankedDet[] {
60
  .map((item, i) => ({ d: item.d, label: i + 1 }))
61
  }
62
 
63
- function spatialLabelMap(detections: Detection[]): Map<string, number> {
64
- const map = new Map<string, number>()
65
  for (const { d, label } of sortSpatially(detections)) {
66
- map.set(d.id, label)
67
  }
68
  return map
69
  }
@@ -71,12 +71,12 @@ function spatialLabelMap(detections: Detection[]): Map<string, number> {
71
  function detectionCircleLabel(
72
  d: Detection,
73
  mode: 'count' | 'confidence',
74
- countLabels: Map<string, number>,
75
  ): string {
76
  if (mode === 'confidence') {
77
  return d.confidence.toFixed(2)
78
  }
79
- const n = countLabels.get(d.id)
80
  return n !== undefined ? String(n) : ''
81
  }
82
 
@@ -164,8 +164,7 @@ function drawCircle(
164
  ctx.stroke()
165
 
166
  // number label — centred in the circle using glyph bounding box
167
- const minLabelRadiusCss = 3
168
- if (showLabel && radius * displayPx >= minLabelRadiusCss && eased > 0) {
169
  const alpha = Math.min(1, eased * 2)
170
  const fs = fitLabelFontSize(ctx, label, radius, displayPx)
171
 
 
60
  .map((item, i) => ({ d: item.d, label: i + 1 }))
61
  }
62
 
63
+ function spatialLabelMap(detections: Detection[]): WeakMap<Detection, number> {
64
+ const map = new WeakMap<Detection, number>()
65
  for (const { d, label } of sortSpatially(detections)) {
66
+ map.set(d, label)
67
  }
68
  return map
69
  }
 
71
  function detectionCircleLabel(
72
  d: Detection,
73
  mode: 'count' | 'confidence',
74
+ countLabels: WeakMap<Detection, number>,
75
  ): string {
76
  if (mode === 'confidence') {
77
  return d.confidence.toFixed(2)
78
  }
79
+ const n = countLabels.get(d)
80
  return n !== undefined ? String(n) : ''
81
  }
82
 
 
164
  ctx.stroke()
165
 
166
  // number label — centred in the circle using glyph bounding box
167
+ if (showLabel && label && eased > 0) {
 
168
  const alpha = Math.min(1, eased * 2)
169
  const fs = fitLabelFontSize(ctx, label, radius, displayPx)
170
 
frontend/src/lib/inference.ts CHANGED
@@ -7,7 +7,7 @@ import {
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
 
@@ -262,9 +262,8 @@ export async function runDetection(
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
  }))
@@ -327,13 +326,12 @@ async function runTiledDetection(
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,
336
- }))
337
  onProgress?.(`Tiled detection: ${merged.length} pipes`)
338
  return merged
339
  }
 
7
  type EnhanceOptions,
8
  } from './imageEnhance'
9
  import { selectModelForImage } from './modelSelect'
10
+ import { nonMaxSuppression, postProcessPipeline, reassignDetectionIds } from './postprocess'
11
  import { canvasToTensor, letterboxCanvas, mapBoxToOriginal } from './preprocess'
12
  import type { Detection, LetterboxMeta } from './types'
13
 
 
262
  extraOutlierFilter: options?.filterOutliers ?? false,
263
  })
264
  options?.onProgress?.(`Detected ${processed.length} pipes in ${Math.round(performance.now() - start)}ms`)
265
+ return reassignDetectionIds(processed, 'det').map((d) => ({
266
  ...d,
 
267
  groupId: 0,
268
  confidence: d.confidence,
269
  }))
 
326
  }
327
  }
328
 
329
+ const merged = reassignDetectionIds(
330
+ postProcessPipeline(all, {
331
+ extraOutlierFilter: options?.filterOutliers ?? false,
332
+ }),
333
+ 'pipe',
334
+ ).map((d) => ({ ...d, groupId: 0 }))
 
335
  onProgress?.(`Tiled detection: ${merged.length} pipes`)
336
  return merged
337
  }
frontend/src/lib/postprocess.ts CHANGED
@@ -85,6 +85,13 @@ export function applyDetectionPostProcess(
85
  return out
86
  }
87
 
 
 
 
 
 
 
 
88
  export function postProcessPipeline(
89
  raw: RawDetection[],
90
  options?: { extraOutlierFilter?: boolean },
 
85
  return out
86
  }
87
 
88
+ export function reassignDetectionIds<T extends RawDetection>(
89
+ detections: T[],
90
+ prefix = 'pipe',
91
+ ): Array<T & { id: string }> {
92
+ return detections.map((d, i) => ({ ...d, id: `${prefix}-${i}` }))
93
+ }
94
+
95
  export function postProcessPipeline(
96
  raw: RawDetection[],
97
  options?: { extraOutlierFilter?: boolean },
frontend/src/lib/serverDetect.ts CHANGED
@@ -6,6 +6,7 @@
6
  * FastAPI `/detect` endpoint which runs the same model on the server CPU.
7
  */
8
  import type { Detection } from './types'
 
9
 
10
  interface ServerDetectResponse {
11
  detections: Array<{ xmin: number; ymin: number; xmax: number; ymax: number; confidence: number }>
@@ -105,15 +106,18 @@ async function serverDetectOnce(
105
  serverBackend = data.backend ?? 'server'
106
  options?.onProgress?.(`Detected ${data.count} pipes (${data.ms}ms, ${serverBackend})`)
107
 
108
- return data.detections.map((d, idx) => ({
109
- xmin: d.xmin,
110
- ymin: d.ymin,
111
- xmax: d.xmax,
112
- ymax: d.ymax,
113
- confidence: d.confidence,
114
- id: `srv-${idx}`,
115
- groupId: 0,
116
- }))
 
 
 
117
  }
118
 
119
  export async function serverDetect(
 
6
  * FastAPI `/detect` endpoint which runs the same model on the server CPU.
7
  */
8
  import type { Detection } from './types'
9
+ import { reassignDetectionIds } from './postprocess'
10
 
11
  interface ServerDetectResponse {
12
  detections: Array<{ xmin: number; ymin: number; xmax: number; ymax: number; confidence: number }>
 
106
  serverBackend = data.backend ?? 'server'
107
  options?.onProgress?.(`Detected ${data.count} pipes (${data.ms}ms, ${serverBackend})`)
108
 
109
+ return reassignDetectionIds(
110
+ data.detections.map((d) => ({
111
+ xmin: d.xmin,
112
+ ymin: d.ymin,
113
+ xmax: d.xmax,
114
+ ymax: d.ymax,
115
+ confidence: d.confidence,
116
+ id: '',
117
+ groupId: 0,
118
+ })),
119
+ 'srv',
120
+ )
121
  }
122
 
123
  export async function serverDetect(