Spaces:
Runtime error
Runtime error
Download frontend/src/lib/export.ts from npisb/pipecountapp-dev: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/spaces/npisb/pipecountapp-dev/resolve/a34f5755f8fee2e18fc00a54ec4d76811bf2def0/frontend/src/lib/export.ts
- Command line
-
hf download hf://spaces/npisb/pipecountapp-dev@a34f5755f8fee2e18fc00a54ec4d76811bf2def0/frontend/src/lib/export.ts
-
curl -L -o export.ts https://huggingface.co/spaces/npisb/pipecountapp-dev/resolve/a34f5755f8fee2e18fc00a54ec4d76811bf2def0/frontend/src/lib/export.ts
2.06 kB
| import type { Detection, DiameterGroup } from './types' | |
| export function exportJson( | |
| detections: Detection[], | |
| groups: DiameterGroup[], | |
| counts: Record<number, number>, | |
| ): 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) | |
| } | |