import type { Detection, DiameterGroup } from './types' export function exportJson( detections: Detection[], groups: DiameterGroup[], counts: Record, ): void { const payload = { total: detections.length, groups: groups.map((g) => ({ id: g.id, label: g.label, count: counts[g.id] ?? 0, expected: g.expected ?? null, })), detections: detections.map((d, i) => ({ id: i + 1, groupId: d.groupId, confidence: d.confidence, xmin: Math.round(d.xmin), ymin: Math.round(d.ymin), xmax: Math.round(d.xmax), ymax: Math.round(d.ymax), manual: d.manual ?? false, })), exportedAt: new Date().toISOString(), } downloadBlob(JSON.stringify(payload, null, 2), 'pipe-count.json', 'application/json') } export function exportCsv( detections: Detection[], groups: DiameterGroup[], ): void { const groupMap = Object.fromEntries(groups.map((g) => [g.id, g.label])) const lines = ['id,group,group_label,xmin,ymin,xmax,ymax,confidence,manual'] detections.forEach((d, i) => { lines.push( [ i + 1, d.groupId, `"${groupMap[d.groupId] ?? ''}"`, Math.round(d.xmin), Math.round(d.ymin), Math.round(d.xmax), Math.round(d.ymax), d.confidence.toFixed(3), d.manual ? 1 : 0, ].join(','), ) }) downloadBlob(lines.join('\n'), 'pipe-count.csv', 'text/csv') } export function exportCanvasPng(canvas: HTMLCanvasElement): void { canvas.toBlob((blob) => { if (!blob) { return } const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = 'pipe-count-result.png' a.click() URL.revokeObjectURL(url) }) } function downloadBlob(content: string, filename: string, type: string): void { const blob = new Blob([content], { type }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = filename a.click() URL.revokeObjectURL(url) }