Spaces:
Runtime error
Add wizard UX flow with circle-draw animation
Browse files- 4-step wizard: Capture -> Preview -> Counting -> Results
- StepCapture: hero screen with large camera/gallery CTAs and model-ready ring
- StepPreview: image preview + full settings + sticky count button
- StepCounting: fullscreen animation (pulsing rings, blurred image bg, sliding progress bar)
- StepResults: circle-draw animation via ResultCanvas (arc sweep per pipe, 8ms stagger)
- Circles appear one-by-one in spatial order (top->bottom, left->right)
- Each stroke sweeps clockwise from 12 o'clock over 220ms with ease-out
- Count display updates live via DOM ref (no React re-render overhead)
- Tap anywhere to skip animation; double-tap to reset zoom
- Pinch-to-zoom and pan enabled after animation completes
- Correction tools (GroupPanel, ToolBar) appear after animation
- Step dots progress indicator in header
- ResultCanvas replaces DetectionCanvas in results phase
Co-authored-by: Cursor <cursoragent@cursor.com>
- frontend/src/App.tsx +138 -153
- frontend/src/components/ResultCanvas.tsx +483 -0
- frontend/src/components/StepCapture.tsx +57 -0
- frontend/src/components/StepCounting.tsx +69 -0
- frontend/src/components/StepPreview.tsx +131 -0
- frontend/src/components/StepResults.tsx +145 -0
- frontend/src/lib/i18n.ts +20 -0
|
@@ -1,80 +1,75 @@
|
|
| 1 |
import { useEffect, useRef, useState } from 'react'
|
| 2 |
import { assignDiameterGroups, countByGroup } from './lib/clustering'
|
| 3 |
-
import { exportCanvasPng, exportCsv, exportJson } from './lib/export'
|
| 4 |
import { getActiveBackend, loadModel, runDetection } from './lib/inference'
|
| 5 |
import { t } from './lib/i18n'
|
| 6 |
-
import type { Detection, DiameterGroup, Lang
|
| 7 |
-
import {
|
| 8 |
-
import {
|
| 9 |
-
import {
|
| 10 |
-
import {
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
import { ResultsSummary } from './components/ResultsSummary'
|
| 14 |
-
import { SettingsPanel } from './components/SettingsPanel'
|
| 15 |
-
import { StickyActionBar } from './components/StickyActionBar'
|
| 16 |
-
import { ToolBar } from './components/ToolBar'
|
| 17 |
|
| 18 |
export default function App() {
|
|
|
|
| 19 |
const [lang, setLang] = useState<Lang>('th')
|
|
|
|
|
|
|
| 20 |
const [image, setImage] = useState<HTMLImageElement | null>(null)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
const [detections, setDetections] = useState<Detection[]>([])
|
| 22 |
const [groups, setGroups] = useState<DiameterGroup[]>([])
|
| 23 |
-
|
| 24 |
-
|
| 25 |
const [groupCount, setGroupCount] = useState<number | 'auto'>('auto')
|
| 26 |
-
const [status, setStatus] = useState('')
|
| 27 |
-
const [busy, setBusy] = useState(false)
|
| 28 |
-
const [modelReady, setModelReady] = useState(false)
|
| 29 |
-
const [useTiling, setUseTiling] = useState(false)
|
| 30 |
const [enhanceImage, setEnhanceImage] = useState(true)
|
|
|
|
| 31 |
const [filterOutliers, setFilterOutliers] = useState(false)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
const [
|
|
|
|
|
|
|
|
|
|
| 35 |
const fileRef = useRef<HTMLInputElement>(null)
|
| 36 |
const cameraRef = useRef<HTMLInputElement>(null)
|
| 37 |
|
|
|
|
|
|
|
| 38 |
useEffect(() => {
|
| 39 |
loadModel(setStatus)
|
| 40 |
-
.then(() => {
|
| 41 |
-
setModelReady(true)
|
| 42 |
-
setStatus('')
|
| 43 |
-
})
|
| 44 |
.catch((err: unknown) => setStatus(String(err)))
|
| 45 |
}, [])
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
const loadFile = (file: File) => {
|
| 51 |
const url = URL.createObjectURL(file)
|
| 52 |
const img = new Image()
|
| 53 |
img.onload = () => {
|
| 54 |
setImage(img)
|
|
|
|
|
|
|
| 55 |
setDetections([])
|
| 56 |
setGroups([])
|
| 57 |
-
|
| 58 |
-
setExportOpen(false)
|
| 59 |
-
URL.revokeObjectURL(url)
|
| 60 |
}
|
| 61 |
img.src = url
|
| 62 |
}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
setGroups([])
|
| 68 |
-
setTool('view')
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
const runCount = async () => {
|
| 72 |
-
if (!image)
|
| 73 |
-
return
|
| 74 |
-
}
|
| 75 |
setBusy(true)
|
| 76 |
-
setSettingsOpen(false)
|
| 77 |
setStatus(t(lang, 'processing'))
|
|
|
|
| 78 |
try {
|
| 79 |
const dets = await runDetection(image, {
|
| 80 |
useTiling,
|
|
@@ -86,134 +81,124 @@ export default function App() {
|
|
| 86 |
const clustered = assignDiameterGroups(dets, groupCount, lang)
|
| 87 |
setDetections(clustered.detections)
|
| 88 |
setGroups(clustered.groups)
|
| 89 |
-
setSelectedGroupId(clustered.groups[0]?.id ?? 0)
|
| 90 |
setStatus(`Done (${getActiveBackend()})`)
|
|
|
|
| 91 |
} catch (err) {
|
| 92 |
setStatus(String(err))
|
|
|
|
| 93 |
} finally {
|
| 94 |
setBusy(false)
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
const recluster = () => {
|
| 99 |
-
const
|
| 100 |
-
setDetections(
|
| 101 |
-
setGroups(
|
| 102 |
}
|
| 103 |
|
| 104 |
const handleExpectedChange = (groupId: number, value: number | undefined) => {
|
| 105 |
setGroups((prev) => prev.map((g) => (g.id === groupId ? { ...g, expected: value } : g)))
|
| 106 |
}
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
onToggleSettings={() => setSettingsOpen((v) => !v)}
|
| 116 |
-
/>
|
| 117 |
-
|
| 118 |
-
<main className="flex-1 overflow-y-auto px-4 pb-[calc(5.5rem+env(safe-area-inset-bottom))] pt-4">
|
| 119 |
-
{!image ? (
|
| 120 |
-
<EmptyState
|
| 121 |
-
lang={lang}
|
| 122 |
-
onCamera={() => cameraRef.current?.click()}
|
| 123 |
-
onGallery={() => fileRef.current?.click()}
|
| 124 |
-
/>
|
| 125 |
-
) : (
|
| 126 |
-
<div className="mx-auto flex w-full max-w-2xl flex-col gap-4">
|
| 127 |
-
<SettingsPanel
|
| 128 |
-
lang={lang}
|
| 129 |
-
open={settingsOpen}
|
| 130 |
-
groupCount={groupCount}
|
| 131 |
-
enhanceImage={enhanceImage}
|
| 132 |
-
useTiling={useTiling}
|
| 133 |
-
filterOutliers={filterOutliers}
|
| 134 |
-
onGroupCount={setGroupCount}
|
| 135 |
-
onEnhanceImage={setEnhanceImage}
|
| 136 |
-
onUseTiling={setUseTiling}
|
| 137 |
-
onFilterOutliers={setFilterOutliers}
|
| 138 |
-
onRecluster={recluster}
|
| 139 |
-
/>
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
image={image}
|
| 147 |
-
detections={detections}
|
| 148 |
-
groups={groups}
|
| 149 |
-
tool={tool}
|
| 150 |
-
selectedGroupId={selectedGroupId}
|
| 151 |
-
onUpdate={setDetections}
|
| 152 |
-
onCanvasReady={setExportCanvas}
|
| 153 |
-
/>
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
</div>
|
| 218 |
)
|
| 219 |
}
|
|
|
|
| 1 |
import { useEffect, useRef, useState } from 'react'
|
| 2 |
import { assignDiameterGroups, countByGroup } from './lib/clustering'
|
|
|
|
| 3 |
import { getActiveBackend, loadModel, runDetection } from './lib/inference'
|
| 4 |
import { t } from './lib/i18n'
|
| 5 |
+
import type { Detection, DiameterGroup, Lang } from './lib/types'
|
| 6 |
+
import { StepCapture } from './components/StepCapture'
|
| 7 |
+
import { StepCounting } from './components/StepCounting'
|
| 8 |
+
import { StepPreview } from './components/StepPreview'
|
| 9 |
+
import { StepResults } from './components/StepResults'
|
| 10 |
+
|
| 11 |
+
type Step = 'capture' | 'preview' | 'counting' | 'results'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
export default function App() {
|
| 14 |
+
const [step, setStep] = useState<Step>('capture')
|
| 15 |
const [lang, setLang] = useState<Lang>('th')
|
| 16 |
+
|
| 17 |
+
// image
|
| 18 |
const [image, setImage] = useState<HTMLImageElement | null>(null)
|
| 19 |
+
const [imageSrc, setImageSrc] = useState('')
|
| 20 |
+
const [imageDims, setImageDims] = useState({ w: 0, h: 0 })
|
| 21 |
+
|
| 22 |
+
// results
|
| 23 |
const [detections, setDetections] = useState<Detection[]>([])
|
| 24 |
const [groups, setGroups] = useState<DiameterGroup[]>([])
|
| 25 |
+
|
| 26 |
+
// settings
|
| 27 |
const [groupCount, setGroupCount] = useState<number | 'auto'>('auto')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
const [enhanceImage, setEnhanceImage] = useState(true)
|
| 29 |
+
const [useTiling, setUseTiling] = useState(false)
|
| 30 |
const [filterOutliers, setFilterOutliers] = useState(false)
|
| 31 |
+
|
| 32 |
+
// ui state
|
| 33 |
+
const [modelReady, setModelReady] = useState(false)
|
| 34 |
+
const [status, setStatus] = useState('')
|
| 35 |
+
const [busy, setBusy] = useState(false)
|
| 36 |
+
|
| 37 |
const fileRef = useRef<HTMLInputElement>(null)
|
| 38 |
const cameraRef = useRef<HTMLInputElement>(null)
|
| 39 |
|
| 40 |
+
const counts = countByGroup(detections, groups)
|
| 41 |
+
|
| 42 |
useEffect(() => {
|
| 43 |
loadModel(setStatus)
|
| 44 |
+
.then(() => { setModelReady(true); setStatus('') })
|
|
|
|
|
|
|
|
|
|
| 45 |
.catch((err: unknown) => setStatus(String(err)))
|
| 46 |
}, [])
|
| 47 |
|
| 48 |
+
// -------------------------------------------------------------------------
|
| 49 |
+
// file / camera
|
| 50 |
+
// -------------------------------------------------------------------------
|
| 51 |
const loadFile = (file: File) => {
|
| 52 |
const url = URL.createObjectURL(file)
|
| 53 |
const img = new Image()
|
| 54 |
img.onload = () => {
|
| 55 |
setImage(img)
|
| 56 |
+
setImageSrc(url)
|
| 57 |
+
setImageDims({ w: img.naturalWidth, h: img.naturalHeight })
|
| 58 |
setDetections([])
|
| 59 |
setGroups([])
|
| 60 |
+
setStep('preview')
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
img.src = url
|
| 63 |
}
|
| 64 |
|
| 65 |
+
// -------------------------------------------------------------------------
|
| 66 |
+
// counting
|
| 67 |
+
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
const runCount = async () => {
|
| 69 |
+
if (!image) return
|
|
|
|
|
|
|
| 70 |
setBusy(true)
|
|
|
|
| 71 |
setStatus(t(lang, 'processing'))
|
| 72 |
+
setStep('counting')
|
| 73 |
try {
|
| 74 |
const dets = await runDetection(image, {
|
| 75 |
useTiling,
|
|
|
|
| 81 |
const clustered = assignDiameterGroups(dets, groupCount, lang)
|
| 82 |
setDetections(clustered.detections)
|
| 83 |
setGroups(clustered.groups)
|
|
|
|
| 84 |
setStatus(`Done (${getActiveBackend()})`)
|
| 85 |
+
setStep('results')
|
| 86 |
} catch (err) {
|
| 87 |
setStatus(String(err))
|
| 88 |
+
setStep('preview')
|
| 89 |
} finally {
|
| 90 |
setBusy(false)
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
const recluster = () => {
|
| 95 |
+
const c = assignDiameterGroups(detections, groupCount, lang)
|
| 96 |
+
setDetections(c.detections)
|
| 97 |
+
setGroups(c.groups)
|
| 98 |
}
|
| 99 |
|
| 100 |
const handleExpectedChange = (groupId: number, value: number | undefined) => {
|
| 101 |
setGroups((prev) => prev.map((g) => (g.id === groupId ? { ...g, expected: value } : g)))
|
| 102 |
}
|
| 103 |
|
| 104 |
+
const resetToCapture = () => {
|
| 105 |
+
setStep('capture')
|
| 106 |
+
setDetections([])
|
| 107 |
+
setGroups([])
|
| 108 |
+
setImage(null)
|
| 109 |
+
setImageSrc('')
|
| 110 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
// -------------------------------------------------------------------------
|
| 113 |
+
// wizard step indicator dots
|
| 114 |
+
// -------------------------------------------------------------------------
|
| 115 |
+
const STEPS: Step[] = ['capture', 'preview', 'counting', 'results']
|
| 116 |
+
const stepIdx = STEPS.indexOf(step)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
return (
|
| 119 |
+
<div className="fixed inset-0 flex flex-col overflow-hidden bg-slate-950">
|
| 120 |
+
{/* minimal header */}
|
| 121 |
+
<header className="flex shrink-0 items-center justify-between border-b border-slate-800/60 px-4 pb-2.5 pt-[max(0.625rem,env(safe-area-inset-top))]">
|
| 122 |
+
<div className="flex items-center gap-2.5">
|
| 123 |
+
<img src="/icon.svg" alt="" className="h-7 w-7 rounded-lg" />
|
| 124 |
+
<span className="text-sm font-semibold text-white">{t(lang, 'title')}</span>
|
| 125 |
+
</div>
|
| 126 |
+
|
| 127 |
+
{/* step dots */}
|
| 128 |
+
<div className="flex items-center gap-1.5">
|
| 129 |
+
{STEPS.map((s, i) => (
|
| 130 |
+
<span
|
| 131 |
+
key={s}
|
| 132 |
+
className={`h-1.5 rounded-full transition-all duration-300 ${
|
| 133 |
+
i === stepIdx ? 'w-5 bg-emerald-400' : i < stepIdx ? 'w-1.5 bg-slate-500' : 'w-1.5 bg-slate-700'
|
| 134 |
+
}`}
|
| 135 |
+
/>
|
| 136 |
+
))}
|
| 137 |
+
</div>
|
| 138 |
+
|
| 139 |
+
<button
|
| 140 |
+
type="button"
|
| 141 |
+
className="rounded-lg bg-slate-800 px-3 py-1.5 text-xs font-medium text-slate-200 active:scale-95"
|
| 142 |
+
onClick={() => setLang(lang === 'th' ? 'en' : 'th')}
|
| 143 |
+
>
|
| 144 |
+
{lang === 'th' ? 'EN' : 'TH'}
|
| 145 |
+
</button>
|
| 146 |
+
</header>
|
| 147 |
+
|
| 148 |
+
{/* steps */}
|
| 149 |
+
{step === 'capture' && (
|
| 150 |
+
<StepCapture
|
| 151 |
+
lang={lang}
|
| 152 |
+
modelReady={modelReady}
|
| 153 |
+
onCamera={() => cameraRef.current?.click()}
|
| 154 |
+
onGallery={() => fileRef.current?.click()}
|
| 155 |
+
/>
|
| 156 |
+
)}
|
| 157 |
+
|
| 158 |
+
{step === 'preview' && image && (
|
| 159 |
+
<StepPreview
|
| 160 |
+
lang={lang}
|
| 161 |
+
imageSrc={imageSrc}
|
| 162 |
+
imageDims={imageDims}
|
| 163 |
+
busy={busy}
|
| 164 |
+
modelReady={modelReady}
|
| 165 |
+
groupCount={groupCount}
|
| 166 |
+
enhanceImage={enhanceImage}
|
| 167 |
+
useTiling={useTiling}
|
| 168 |
+
filterOutliers={filterOutliers}
|
| 169 |
+
onBack={resetToCapture}
|
| 170 |
+
onRetake={() => cameraRef.current?.click()}
|
| 171 |
+
onGallery={() => fileRef.current?.click()}
|
| 172 |
+
onCount={runCount}
|
| 173 |
+
onGroupCount={setGroupCount}
|
| 174 |
+
onEnhanceImage={setEnhanceImage}
|
| 175 |
+
onUseTiling={setUseTiling}
|
| 176 |
+
onFilterOutliers={setFilterOutliers}
|
| 177 |
+
onRecluster={recluster}
|
| 178 |
+
/>
|
| 179 |
+
)}
|
| 180 |
+
|
| 181 |
+
{step === 'counting' && (
|
| 182 |
+
<StepCounting lang={lang} status={status} imageSrc={imageSrc} />
|
| 183 |
+
)}
|
| 184 |
+
|
| 185 |
+
{step === 'results' && image && (
|
| 186 |
+
<StepResults
|
| 187 |
+
lang={lang}
|
| 188 |
+
image={image}
|
| 189 |
+
detections={detections}
|
| 190 |
+
groups={groups}
|
| 191 |
+
counts={counts}
|
| 192 |
+
onUpdate={setDetections}
|
| 193 |
+
onExpectedChange={handleExpectedChange}
|
| 194 |
+
onRecount={() => { setStep('preview') }}
|
| 195 |
+
onNewPhoto={resetToCapture}
|
| 196 |
+
/>
|
| 197 |
+
)}
|
| 198 |
+
|
| 199 |
+
{/* hidden file inputs */}
|
| 200 |
+
<input ref={fileRef} type="file" accept="image/*" className="hidden" onChange={(e) => e.target.files?.[0] && loadFile(e.target.files[0])} />
|
| 201 |
+
<input ref={cameraRef} type="file" accept="image/*" capture="environment" className="hidden" onChange={(e) => e.target.files?.[0] && loadFile(e.target.files[0])} />
|
| 202 |
</div>
|
| 203 |
)
|
| 204 |
}
|
|
@@ -0,0 +1,483 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* ResultCanvas
|
| 3 |
+
* Phase 1 – Animation: circles draw in one-by-one (arc sweep) with a count-up.
|
| 4 |
+
* Phase 2 – Interactive: zoom/pan + edit tools (same as DetectionCanvas).
|
| 5 |
+
* Tap during animation to skip to the final frame.
|
| 6 |
+
*/
|
| 7 |
+
import { useEffect, useRef, useState } from 'react'
|
| 8 |
+
import type { Detection, DiameterGroup, ToolMode } from '../lib/types'
|
| 9 |
+
|
| 10 |
+
// ---------------------------------------------------------------------------
|
| 11 |
+
// helpers
|
| 12 |
+
// ---------------------------------------------------------------------------
|
| 13 |
+
|
| 14 |
+
function hexToRgb(hex: string): [number, number, number] {
|
| 15 |
+
const v = parseInt(hex.replace('#', ''), 16)
|
| 16 |
+
return [(v >> 16) & 255, (v >> 8) & 255, v & 255]
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
function easeOut(t: number): number {
|
| 20 |
+
return 1 - (1 - t) * (1 - t)
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
interface SortedDet {
|
| 24 |
+
d: Detection
|
| 25 |
+
origIdx: number
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
function sortSpatially(detections: Detection[]): SortedDet[] {
|
| 29 |
+
return [...detections]
|
| 30 |
+
.map((d, i) => ({ d, origIdx: i }))
|
| 31 |
+
.sort((a, b) => {
|
| 32 |
+
const rowA = Math.round(((a.d.ymin + a.d.ymax) / 2) / 40)
|
| 33 |
+
const rowB = Math.round(((b.d.ymin + b.d.ymax) / 2) / 40)
|
| 34 |
+
if (rowA !== rowB) return rowA - rowB
|
| 35 |
+
return (a.d.xmin + a.d.xmax) / 2 - (b.d.xmin + b.d.xmax) / 2
|
| 36 |
+
})
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
function computeStagger(count: number): { staggerMs: number; durationMs: number } {
|
| 40 |
+
if (count <= 500) return { staggerMs: 8, durationMs: 220 }
|
| 41 |
+
if (count <= 1000) return { staggerMs: 4, durationMs: 150 }
|
| 42 |
+
return { staggerMs: 2, durationMs: 100 }
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
function drawCircle(
|
| 46 |
+
ctx: CanvasRenderingContext2D,
|
| 47 |
+
cx: number,
|
| 48 |
+
cy: number,
|
| 49 |
+
radius: number,
|
| 50 |
+
rgb: [number, number, number],
|
| 51 |
+
progress: number,
|
| 52 |
+
label: string,
|
| 53 |
+
showLabel: boolean,
|
| 54 |
+
) {
|
| 55 |
+
const [r, g, b] = rgb
|
| 56 |
+
const eased = easeOut(Math.min(1, progress))
|
| 57 |
+
const lw = Math.max(1.5, radius * 0.14)
|
| 58 |
+
|
| 59 |
+
ctx.save()
|
| 60 |
+
|
| 61 |
+
// semi-transparent fill (fades in)
|
| 62 |
+
ctx.beginPath()
|
| 63 |
+
ctx.arc(cx, cy, radius, 0, Math.PI * 2)
|
| 64 |
+
ctx.fillStyle = `rgba(${r},${g},${b},${0.30 * eased})`
|
| 65 |
+
ctx.fill()
|
| 66 |
+
|
| 67 |
+
// stroke arc sweeps clockwise from 12 o'clock
|
| 68 |
+
ctx.beginPath()
|
| 69 |
+
const endAngle =
|
| 70 |
+
progress >= 1 ? Math.PI * 2 : eased * Math.PI * 2
|
| 71 |
+
ctx.arc(cx, cy, radius, -Math.PI / 2, -Math.PI / 2 + endAngle)
|
| 72 |
+
ctx.strokeStyle = `rgba(${r},${g},${b},0.92)`
|
| 73 |
+
ctx.lineWidth = lw
|
| 74 |
+
ctx.lineCap = 'round'
|
| 75 |
+
ctx.stroke()
|
| 76 |
+
|
| 77 |
+
// number label (only when circle is complete)
|
| 78 |
+
if (progress >= 1 && showLabel && radius >= 7) {
|
| 79 |
+
const fs = Math.max(9, Math.min(radius * 0.85, 15))
|
| 80 |
+
ctx.font = `bold ${fs}px sans-serif`
|
| 81 |
+
ctx.textAlign = 'center'
|
| 82 |
+
ctx.textBaseline = 'middle'
|
| 83 |
+
ctx.fillStyle = 'rgba(0,0,0,0.55)'
|
| 84 |
+
ctx.fillText(label, cx + 0.6, cy + 0.6)
|
| 85 |
+
ctx.fillStyle = '#ffffff'
|
| 86 |
+
ctx.fillText(label, cx, cy)
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
ctx.restore()
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// ---------------------------------------------------------------------------
|
| 93 |
+
// component
|
| 94 |
+
// ---------------------------------------------------------------------------
|
| 95 |
+
|
| 96 |
+
interface Props {
|
| 97 |
+
image: HTMLImageElement
|
| 98 |
+
detections: Detection[]
|
| 99 |
+
groups: DiameterGroup[]
|
| 100 |
+
tool: ToolMode
|
| 101 |
+
selectedGroupId: number
|
| 102 |
+
onUpdate: (dets: Detection[]) => void
|
| 103 |
+
onCanvasReady?: (canvas: HTMLCanvasElement | null) => void
|
| 104 |
+
onAnimationComplete?: () => void
|
| 105 |
+
/** ref to a DOM element whose textContent will be updated with live count */
|
| 106 |
+
countDisplayRef?: React.RefObject<HTMLElement | null>
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
export function ResultCanvas({
|
| 110 |
+
image,
|
| 111 |
+
detections,
|
| 112 |
+
groups,
|
| 113 |
+
tool,
|
| 114 |
+
selectedGroupId,
|
| 115 |
+
onUpdate,
|
| 116 |
+
onCanvasReady,
|
| 117 |
+
onAnimationComplete,
|
| 118 |
+
countDisplayRef,
|
| 119 |
+
}: Props) {
|
| 120 |
+
const containerRef = useRef<HTMLDivElement>(null)
|
| 121 |
+
const canvasRef = useRef<HTMLCanvasElement>(null)
|
| 122 |
+
const [animating, setAnimating] = useState(true)
|
| 123 |
+
const skipRef = useRef(false)
|
| 124 |
+
const rafRef = useRef(0)
|
| 125 |
+
|
| 126 |
+
// zoom / pan state (used in interactive phase)
|
| 127 |
+
const transformRef = useRef({ scale: 1, tx: 0, ty: 0 })
|
| 128 |
+
const [displayScale, setDisplayScale] = useState(1)
|
| 129 |
+
const activeRef = useRef(new Map<number, { x: number; y: number }>())
|
| 130 |
+
const pinchRef = useRef<{ dist: number; mx: number; my: number } | null>(null)
|
| 131 |
+
const panStartRef = useRef<{ x: number; y: number; tx: number; ty: number } | null>(null)
|
| 132 |
+
const areaStartRef = useRef<{ x: number; y: number } | null>(null)
|
| 133 |
+
const areaRef = useRef<{ x1: number; y1: number; x2: number; y2: number } | null>(null)
|
| 134 |
+
const lastTapRef = useRef(0)
|
| 135 |
+
|
| 136 |
+
// -------------------------------------------------------------------------
|
| 137 |
+
// animation phase
|
| 138 |
+
// -------------------------------------------------------------------------
|
| 139 |
+
useEffect(() => {
|
| 140 |
+
if (detections.length === 0) {
|
| 141 |
+
setAnimating(false)
|
| 142 |
+
onAnimationComplete?.()
|
| 143 |
+
return
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
const canvas = canvasRef.current
|
| 147 |
+
if (!canvas) return
|
| 148 |
+
|
| 149 |
+
const ctx = canvas.getContext('2d')
|
| 150 |
+
if (!ctx) return
|
| 151 |
+
|
| 152 |
+
const maxW = canvas.parentElement?.clientWidth ?? image.naturalWidth
|
| 153 |
+
const ds = Math.min(1, maxW / image.naturalWidth)
|
| 154 |
+
canvas.width = Math.round(image.naturalWidth * ds)
|
| 155 |
+
canvas.height = Math.round(image.naturalHeight * ds)
|
| 156 |
+
|
| 157 |
+
onCanvasReady?.(canvas)
|
| 158 |
+
|
| 159 |
+
const sorted = sortSpatially(detections)
|
| 160 |
+
const { staggerMs, durationMs } = computeStagger(detections.length)
|
| 161 |
+
const totalMs = (sorted.length - 1) * staggerMs + durationMs
|
| 162 |
+
const dense = detections.length > 300
|
| 163 |
+
const showLabels = detections.length <= 500
|
| 164 |
+
|
| 165 |
+
let startTs: number | null = null
|
| 166 |
+
skipRef.current = false
|
| 167 |
+
|
| 168 |
+
function frame(ts: number) {
|
| 169 |
+
if (!canvas) return
|
| 170 |
+
if (startTs === null) startTs = ts
|
| 171 |
+
const elapsed = skipRef.current ? totalMs + 1 : ts - startTs
|
| 172 |
+
|
| 173 |
+
ctx!.clearRect(0, 0, canvas.width, canvas.height)
|
| 174 |
+
ctx!.drawImage(image, 0, 0, canvas.width, canvas.height)
|
| 175 |
+
|
| 176 |
+
let visibleCount = 0
|
| 177 |
+
|
| 178 |
+
sorted.forEach(({ d, origIdx }, animIdx) => {
|
| 179 |
+
const startAt = animIdx * staggerMs
|
| 180 |
+
if (elapsed < startAt) return
|
| 181 |
+
const progress = (elapsed - startAt) / durationMs
|
| 182 |
+
|
| 183 |
+
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 184 |
+
const color = group?.color ?? '#ef4444'
|
| 185 |
+
const rgb = hexToRgb(color)
|
| 186 |
+
|
| 187 |
+
const px = d.xmin * ds
|
| 188 |
+
const py = d.ymin * ds
|
| 189 |
+
const pw = (d.xmax - d.xmin) * ds
|
| 190 |
+
const ph = (d.ymax - d.ymin) * ds
|
| 191 |
+
const cx = px + pw / 2
|
| 192 |
+
const cy = py + ph / 2
|
| 193 |
+
const radius = Math.max(dense ? 4 : 6, Math.min(pw, ph) / 2)
|
| 194 |
+
|
| 195 |
+
drawCircle(ctx!, cx, cy, radius, rgb, progress, String(origIdx + 1), showLabels)
|
| 196 |
+
visibleCount++
|
| 197 |
+
})
|
| 198 |
+
|
| 199 |
+
// live count update (DOM-direct for perf)
|
| 200 |
+
if (countDisplayRef?.current) {
|
| 201 |
+
countDisplayRef.current.textContent = String(visibleCount)
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
if (elapsed < totalMs) {
|
| 205 |
+
rafRef.current = requestAnimationFrame(frame)
|
| 206 |
+
} else {
|
| 207 |
+
// final static redraw with all circles fully complete
|
| 208 |
+
ctx!.clearRect(0, 0, canvas.width, canvas.height)
|
| 209 |
+
ctx!.drawImage(image, 0, 0, canvas.width, canvas.height)
|
| 210 |
+
sorted.forEach(({ d, origIdx }) => {
|
| 211 |
+
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 212 |
+
const color = group?.color ?? '#ef4444'
|
| 213 |
+
const rgb = hexToRgb(color)
|
| 214 |
+
const px = d.xmin * ds
|
| 215 |
+
const py = d.ymin * ds
|
| 216 |
+
const pw = (d.xmax - d.xmin) * ds
|
| 217 |
+
const ph = (d.ymax - d.ymin) * ds
|
| 218 |
+
const cx = px + pw / 2
|
| 219 |
+
const cy = py + ph / 2
|
| 220 |
+
const radius = Math.max(dense ? 4 : 6, Math.min(pw, ph) / 2)
|
| 221 |
+
drawCircle(ctx!, cx, cy, radius, rgb, 1, String(origIdx + 1), showLabels)
|
| 222 |
+
})
|
| 223 |
+
if (countDisplayRef?.current) {
|
| 224 |
+
countDisplayRef.current.textContent = String(detections.length)
|
| 225 |
+
}
|
| 226 |
+
setAnimating(false)
|
| 227 |
+
onAnimationComplete?.()
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
rafRef.current = requestAnimationFrame(frame)
|
| 232 |
+
return () => cancelAnimationFrame(rafRef.current)
|
| 233 |
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
| 234 |
+
}, [image, detections, groups])
|
| 235 |
+
|
| 236 |
+
// -------------------------------------------------------------------------
|
| 237 |
+
// interactive phase – re-render on detections / groups change
|
| 238 |
+
// -------------------------------------------------------------------------
|
| 239 |
+
useEffect(() => {
|
| 240 |
+
if (animating) return
|
| 241 |
+
const canvas = canvasRef.current
|
| 242 |
+
if (!canvas || !image) return
|
| 243 |
+
const ctx = canvas.getContext('2d')
|
| 244 |
+
if (!ctx) return
|
| 245 |
+
|
| 246 |
+
const maxW = canvas.parentElement?.clientWidth ?? image.naturalWidth
|
| 247 |
+
const ds = Math.min(1, maxW / image.naturalWidth)
|
| 248 |
+
if (canvas.width !== Math.round(image.naturalWidth * ds)) {
|
| 249 |
+
canvas.width = Math.round(image.naturalWidth * ds)
|
| 250 |
+
canvas.height = Math.round(image.naturalHeight * ds)
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
| 254 |
+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
|
| 255 |
+
|
| 256 |
+
const dense = detections.length > 300
|
| 257 |
+
const showLabels = detections.length <= 500
|
| 258 |
+
|
| 259 |
+
detections.forEach((d, idx) => {
|
| 260 |
+
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 261 |
+
const color = group?.color ?? '#ef4444'
|
| 262 |
+
const rgb = hexToRgb(color)
|
| 263 |
+
const px = d.xmin * ds
|
| 264 |
+
const py = d.ymin * ds
|
| 265 |
+
const pw = (d.xmax - d.xmin) * ds
|
| 266 |
+
const ph = (d.ymax - d.ymin) * ds
|
| 267 |
+
const cx = px + pw / 2
|
| 268 |
+
const cy = py + ph / 2
|
| 269 |
+
const radius = Math.max(dense ? 4 : 6, Math.min(pw, ph) / 2)
|
| 270 |
+
drawCircle(ctx, cx, cy, radius, rgb, 1, String(idx + 1), showLabels)
|
| 271 |
+
})
|
| 272 |
+
|
| 273 |
+
const area = areaRef.current
|
| 274 |
+
if (area) {
|
| 275 |
+
ctx.save()
|
| 276 |
+
ctx.strokeStyle = 'rgba(255,255,255,0.9)'
|
| 277 |
+
ctx.fillStyle = 'rgba(255,255,255,0.08)'
|
| 278 |
+
ctx.lineWidth = 2
|
| 279 |
+
ctx.setLineDash([6, 4])
|
| 280 |
+
ctx.fillRect(area.x1 * ds, area.y1 * ds, (area.x2 - area.x1) * ds, (area.y2 - area.y1) * ds)
|
| 281 |
+
ctx.strokeRect(area.x1 * ds, area.y1 * ds, (area.x2 - area.x1) * ds, (area.y2 - area.y1) * ds)
|
| 282 |
+
ctx.restore()
|
| 283 |
+
}
|
| 284 |
+
}, [animating, image, detections, groups])
|
| 285 |
+
|
| 286 |
+
// -------------------------------------------------------------------------
|
| 287 |
+
// zoom / pan helpers (interactive phase only)
|
| 288 |
+
// -------------------------------------------------------------------------
|
| 289 |
+
const applyTransform = () => {
|
| 290 |
+
const canvas = canvasRef.current
|
| 291 |
+
if (!canvas) return
|
| 292 |
+
const { scale, tx, ty } = transformRef.current
|
| 293 |
+
canvas.style.transform = `translate(${tx}px,${ty}px) scale(${scale})`
|
| 294 |
+
setDisplayScale(scale)
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
const clampTransform = () => {
|
| 298 |
+
const canvas = canvasRef.current
|
| 299 |
+
const container = containerRef.current
|
| 300 |
+
if (!canvas || !container) return
|
| 301 |
+
const t = transformRef.current
|
| 302 |
+
if (t.scale <= 1) {
|
| 303 |
+
transformRef.current = { scale: 1, tx: 0, ty: 0 }
|
| 304 |
+
} else {
|
| 305 |
+
const cw = container.clientWidth
|
| 306 |
+
const ch = container.clientHeight
|
| 307 |
+
const lw = canvas.offsetWidth
|
| 308 |
+
const lh = canvas.offsetHeight
|
| 309 |
+
t.tx = Math.min(0, Math.max(cw - lw * t.scale, t.tx))
|
| 310 |
+
t.ty = Math.min(0, Math.max(ch - lh * t.scale, t.ty))
|
| 311 |
+
}
|
| 312 |
+
setDisplayScale(transformRef.current.scale)
|
| 313 |
+
applyTransform()
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
const resetZoom = () => {
|
| 317 |
+
transformRef.current = { scale: 1, tx: 0, ty: 0 }
|
| 318 |
+
setDisplayScale(1)
|
| 319 |
+
applyTransform()
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
const toImageCoords = (clientX: number, clientY: number) => {
|
| 323 |
+
const canvas = canvasRef.current!
|
| 324 |
+
const rect = canvas.getBoundingClientRect()
|
| 325 |
+
return {
|
| 326 |
+
x: ((clientX - rect.left) / rect.width) * image.naturalWidth,
|
| 327 |
+
y: ((clientY - rect.top) / rect.height) * image.naturalHeight,
|
| 328 |
+
}
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
const findHit = (x: number, y: number) => {
|
| 332 |
+
for (let i = detections.length - 1; i >= 0; i--) {
|
| 333 |
+
const d = detections[i]
|
| 334 |
+
if (x >= d.xmin && x <= d.xmax && y >= d.ymin && y <= d.ymax) return i
|
| 335 |
+
}
|
| 336 |
+
return -1
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
// -------------------------------------------------------------------------
|
| 340 |
+
// pointer event handlers
|
| 341 |
+
// -------------------------------------------------------------------------
|
| 342 |
+
const handlePointerDown = (e: React.PointerEvent) => {
|
| 343 |
+
// skip animation on tap
|
| 344 |
+
if (animating) {
|
| 345 |
+
skipRef.current = true
|
| 346 |
+
return
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
e.currentTarget.setPointerCapture(e.pointerId)
|
| 350 |
+
activeRef.current.set(e.pointerId, { x: e.clientX, y: e.clientY })
|
| 351 |
+
|
| 352 |
+
if (activeRef.current.size === 2) {
|
| 353 |
+
panStartRef.current = null
|
| 354 |
+
areaStartRef.current = null
|
| 355 |
+
const pts = [...activeRef.current.values()]
|
| 356 |
+
const dist = Math.hypot(pts[1].x - pts[0].x, pts[1].y - pts[0].y)
|
| 357 |
+
pinchRef.current = { dist, mx: (pts[0].x + pts[1].x) / 2, my: (pts[0].y + pts[1].y) / 2 }
|
| 358 |
+
return
|
| 359 |
+
}
|
| 360 |
+
if (activeRef.current.size > 2) return
|
| 361 |
+
|
| 362 |
+
const now = Date.now()
|
| 363 |
+
if (now - lastTapRef.current < 280) {
|
| 364 |
+
resetZoom()
|
| 365 |
+
lastTapRef.current = 0
|
| 366 |
+
return
|
| 367 |
+
}
|
| 368 |
+
lastTapRef.current = now
|
| 369 |
+
|
| 370 |
+
if (transformRef.current.scale > 1 || tool === 'view') {
|
| 371 |
+
panStartRef.current = { x: e.clientX, y: e.clientY, tx: transformRef.current.tx, ty: transformRef.current.ty }
|
| 372 |
+
return
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
const { x, y } = toImageCoords(e.clientX, e.clientY)
|
| 376 |
+
if (tool === 'area') {
|
| 377 |
+
areaStartRef.current = { x, y }
|
| 378 |
+
areaRef.current = { x1: x, y1: y, x2: x, y2: y }
|
| 379 |
+
return
|
| 380 |
+
}
|
| 381 |
+
const hit = findHit(x, y)
|
| 382 |
+
if (tool === 'delete' && hit >= 0) { onUpdate(detections.filter((_, i) => i !== hit)); return }
|
| 383 |
+
if (tool === 'reassign' && hit >= 0) { onUpdate(detections.map((d, i) => i === hit ? { ...d, groupId: selectedGroupId, manual: true } : d)); return }
|
| 384 |
+
if (tool === 'add') {
|
| 385 |
+
const s = 24
|
| 386 |
+
onUpdate([...detections, { id: `m-${Date.now()}`, xmin: x - s / 2, ymin: y - s / 2, xmax: x + s / 2, ymax: y + s / 2, confidence: 1, groupId: selectedGroupId, manual: true }])
|
| 387 |
+
}
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
const handlePointerMove = (e: React.PointerEvent) => {
|
| 391 |
+
if (animating || !activeRef.current.has(e.pointerId)) return
|
| 392 |
+
activeRef.current.set(e.pointerId, { x: e.clientX, y: e.clientY })
|
| 393 |
+
|
| 394 |
+
if (activeRef.current.size === 2 && pinchRef.current) {
|
| 395 |
+
const pts = [...activeRef.current.values()]
|
| 396 |
+
const dist = Math.hypot(pts[1].x - pts[0].x, pts[1].y - pts[0].y)
|
| 397 |
+
const mx = (pts[0].x + pts[1].x) / 2
|
| 398 |
+
const my = (pts[0].y + pts[1].y) / 2
|
| 399 |
+
const t = transformRef.current
|
| 400 |
+
const newScale = Math.max(1, Math.min(6, t.scale * (dist / pinchRef.current.dist)))
|
| 401 |
+
const container = containerRef.current
|
| 402 |
+
if (container) {
|
| 403 |
+
const cr = container.getBoundingClientRect()
|
| 404 |
+
const lx = mx - cr.left, ly = my - cr.top
|
| 405 |
+
const ox = (lx - t.tx) / t.scale, oy = (ly - t.ty) / t.scale
|
| 406 |
+
t.tx = lx - ox * newScale + (mx - pinchRef.current.mx)
|
| 407 |
+
t.ty = ly - oy * newScale + (my - pinchRef.current.my)
|
| 408 |
+
}
|
| 409 |
+
t.scale = newScale
|
| 410 |
+
clampTransform()
|
| 411 |
+
pinchRef.current = { dist, mx, my }
|
| 412 |
+
return
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
if (panStartRef.current && activeRef.current.size === 1) {
|
| 416 |
+
transformRef.current.tx = panStartRef.current.tx + e.clientX - panStartRef.current.x
|
| 417 |
+
transformRef.current.ty = panStartRef.current.ty + e.clientY - panStartRef.current.y
|
| 418 |
+
clampTransform()
|
| 419 |
+
return
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
if (areaStartRef.current && tool === 'area') {
|
| 423 |
+
const { x, y } = toImageCoords(e.clientX, e.clientY)
|
| 424 |
+
areaRef.current = { x1: Math.min(areaStartRef.current.x, x), y1: Math.min(areaStartRef.current.y, y), x2: Math.max(areaStartRef.current.x, x), y2: Math.max(areaStartRef.current.y, y) }
|
| 425 |
+
onUpdate([...detections])
|
| 426 |
+
}
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
const handlePointerUp = (e: React.PointerEvent) => {
|
| 430 |
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId)
|
| 431 |
+
if (!animating && tool === 'area' && areaRef.current && areaStartRef.current) {
|
| 432 |
+
const a = areaRef.current
|
| 433 |
+
if (a.x2 > a.x1 && a.y2 > a.y1) {
|
| 434 |
+
onUpdate(detections.map((d) => {
|
| 435 |
+
const cx = (d.xmin + d.xmax) / 2, cy = (d.ymin + d.ymax) / 2
|
| 436 |
+
return cx >= a.x1 && cx <= a.x2 && cy >= a.y1 && cy <= a.y2 ? { ...d, groupId: selectedGroupId, manual: true } : d
|
| 437 |
+
}))
|
| 438 |
+
}
|
| 439 |
+
}
|
| 440 |
+
activeRef.current.delete(e.pointerId)
|
| 441 |
+
if (activeRef.current.size < 2) pinchRef.current = null
|
| 442 |
+
if (activeRef.current.size === 0) { panStartRef.current = null; areaStartRef.current = null; areaRef.current = null; if (!animating) onUpdate([...detections]) }
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
return (
|
| 446 |
+
<div ref={containerRef} className="relative overflow-hidden rounded-2xl border border-slate-700/80 bg-black shadow-lg">
|
| 447 |
+
<canvas
|
| 448 |
+
ref={canvasRef}
|
| 449 |
+
style={{ transformOrigin: '0 0', display: 'block', width: '100%' }}
|
| 450 |
+
className="select-none touch-none"
|
| 451 |
+
onPointerDown={handlePointerDown}
|
| 452 |
+
onPointerMove={handlePointerMove}
|
| 453 |
+
onPointerUp={handlePointerUp}
|
| 454 |
+
onPointerCancel={handlePointerUp}
|
| 455 |
+
/>
|
| 456 |
+
|
| 457 |
+
{/* animation skip hint */}
|
| 458 |
+
{animating && (
|
| 459 |
+
<div className="pointer-events-none absolute bottom-3 left-1/2 -translate-x-1/2 rounded-full bg-black/60 px-3 py-1 text-xs text-white/70 backdrop-blur-sm">
|
| 460 |
+
Tap to skip
|
| 461 |
+
</div>
|
| 462 |
+
)}
|
| 463 |
+
|
| 464 |
+
{/* zoom badge */}
|
| 465 |
+
{!animating && displayScale > 1.05 && (
|
| 466 |
+
<button
|
| 467 |
+
type="button"
|
| 468 |
+
onClick={resetZoom}
|
| 469 |
+
className="absolute top-2 right-2 rounded-full bg-black/70 px-2.5 py-1 text-xs font-medium text-white/90 backdrop-blur-sm active:scale-95"
|
| 470 |
+
>
|
| 471 |
+
{displayScale.toFixed(1)}× ✕
|
| 472 |
+
</button>
|
| 473 |
+
)}
|
| 474 |
+
|
| 475 |
+
{/* zoom hint after animation */}
|
| 476 |
+
{!animating && displayScale <= 1.05 && detections.length > 0 && (
|
| 477 |
+
<div className="pointer-events-none absolute bottom-2 left-1/2 -translate-x-1/2 rounded-full bg-black/55 px-3 py-1 text-xs text-white/55 backdrop-blur-sm">
|
| 478 |
+
Pinch to zoom · double-tap reset
|
| 479 |
+
</div>
|
| 480 |
+
)}
|
| 481 |
+
</div>
|
| 482 |
+
)
|
| 483 |
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Camera, ImagePlus } from 'lucide-react'
|
| 2 |
+
import { t } from '../lib/i18n'
|
| 3 |
+
import type { Lang } from '../lib/types'
|
| 4 |
+
|
| 5 |
+
interface Props {
|
| 6 |
+
lang: Lang
|
| 7 |
+
modelReady: boolean
|
| 8 |
+
onCamera: () => void
|
| 9 |
+
onGallery: () => void
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export function StepCapture({ lang, modelReady, onCamera, onGallery }: Props) {
|
| 13 |
+
return (
|
| 14 |
+
<div className="flex flex-1 flex-col items-center justify-center px-6">
|
| 15 |
+
{/* logo mark */}
|
| 16 |
+
<div className="mb-8 flex flex-col items-center gap-3">
|
| 17 |
+
<div className="relative flex h-24 w-24 items-center justify-center rounded-3xl bg-gradient-to-br from-slate-800 to-slate-900 shadow-2xl ring-1 ring-slate-700">
|
| 18 |
+
<img src="/icon.svg" alt="" className="h-16 w-16" />
|
| 19 |
+
{/* model ready indicator */}
|
| 20 |
+
<span
|
| 21 |
+
className={`absolute -right-1 -top-1 h-4 w-4 rounded-full border-2 border-slate-950 ${
|
| 22 |
+
modelReady
|
| 23 |
+
? 'bg-emerald-400 shadow-[0_0_10px_rgba(52,211,153,0.7)]'
|
| 24 |
+
: 'animate-pulse bg-amber-400'
|
| 25 |
+
}`}
|
| 26 |
+
/>
|
| 27 |
+
</div>
|
| 28 |
+
<div className="text-center">
|
| 29 |
+
<h1 className="text-2xl font-bold tracking-tight text-white">{t(lang, 'title')}</h1>
|
| 30 |
+
<p className="mt-1 text-sm text-slate-400">{t(lang, 'captureSubtitle')}</p>
|
| 31 |
+
</div>
|
| 32 |
+
</div>
|
| 33 |
+
|
| 34 |
+
{/* action buttons */}
|
| 35 |
+
<div className="w-full max-w-xs space-y-3">
|
| 36 |
+
<button
|
| 37 |
+
type="button"
|
| 38 |
+
className="flex h-14 w-full items-center justify-center gap-3 rounded-2xl bg-blue-600 text-base font-bold text-white shadow-lg shadow-blue-900/40 active:scale-[0.98]"
|
| 39 |
+
onClick={onCamera}
|
| 40 |
+
>
|
| 41 |
+
<Camera size={22} />
|
| 42 |
+
{t(lang, 'takePhoto')}
|
| 43 |
+
</button>
|
| 44 |
+
<button
|
| 45 |
+
type="button"
|
| 46 |
+
className="flex h-14 w-full items-center justify-center gap-3 rounded-2xl bg-slate-800 text-base font-medium text-slate-200 ring-1 ring-slate-700 active:scale-[0.98]"
|
| 47 |
+
onClick={onGallery}
|
| 48 |
+
>
|
| 49 |
+
<ImagePlus size={22} />
|
| 50 |
+
{t(lang, 'chooseFile')}
|
| 51 |
+
</button>
|
| 52 |
+
</div>
|
| 53 |
+
|
| 54 |
+
<p className="mt-10 text-center text-xs text-slate-600">{t(lang, 'company')}</p>
|
| 55 |
+
</div>
|
| 56 |
+
)
|
| 57 |
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect, useState } from 'react'
|
| 2 |
+
import { t } from '../lib/i18n'
|
| 3 |
+
import type { Lang } from '../lib/types'
|
| 4 |
+
|
| 5 |
+
interface Props {
|
| 6 |
+
lang: Lang
|
| 7 |
+
status: string
|
| 8 |
+
imageSrc?: string
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
export function StepCounting({ lang, status, imageSrc }: Props) {
|
| 12 |
+
const [dots, setDots] = useState(0)
|
| 13 |
+
|
| 14 |
+
useEffect(() => {
|
| 15 |
+
const id = setInterval(() => setDots((d) => (d + 1) % 4), 500)
|
| 16 |
+
return () => clearInterval(id)
|
| 17 |
+
}, [])
|
| 18 |
+
|
| 19 |
+
return (
|
| 20 |
+
<div className="relative flex flex-1 flex-col items-center justify-center overflow-hidden">
|
| 21 |
+
{/* blurred image background */}
|
| 22 |
+
{imageSrc && (
|
| 23 |
+
<img
|
| 24 |
+
src={imageSrc}
|
| 25 |
+
alt=""
|
| 26 |
+
aria-hidden
|
| 27 |
+
className="absolute inset-0 h-full w-full object-cover opacity-15 blur-xl scale-110"
|
| 28 |
+
/>
|
| 29 |
+
)}
|
| 30 |
+
|
| 31 |
+
<div className="relative z-10 flex flex-col items-center gap-6 px-8 text-center">
|
| 32 |
+
{/* pulsing rings */}
|
| 33 |
+
<div className="relative h-28 w-28">
|
| 34 |
+
<span className="absolute inset-0 animate-[ping_1.4s_cubic-bezier(0,0,0.2,1)_infinite] rounded-full border-2 border-emerald-500/40" />
|
| 35 |
+
<span className="absolute inset-3 animate-[ping_1.4s_cubic-bezier(0,0,0.2,1)_infinite_0.4s] rounded-full border-2 border-emerald-500/30" />
|
| 36 |
+
<span className="absolute inset-6 animate-[ping_1.4s_cubic-bezier(0,0,0.2,1)_infinite_0.8s] rounded-full border-2 border-emerald-500/20" />
|
| 37 |
+
<span className="absolute inset-0 flex items-center justify-center">
|
| 38 |
+
<span className="h-14 w-14 rounded-full border-2 border-emerald-500/60 bg-emerald-500/10" />
|
| 39 |
+
</span>
|
| 40 |
+
<span className="absolute inset-0 flex items-center justify-center">
|
| 41 |
+
<img src="/icon.svg" alt="" className="h-10 w-10 opacity-80" />
|
| 42 |
+
</span>
|
| 43 |
+
</div>
|
| 44 |
+
|
| 45 |
+
<div>
|
| 46 |
+
<h2 className="text-xl font-bold text-white">
|
| 47 |
+
{t(lang, 'countingTitle')}{''.padEnd(dots, '.')}
|
| 48 |
+
</h2>
|
| 49 |
+
<p className="mt-2 min-h-[1.5rem] text-sm text-slate-400">
|
| 50 |
+
{status || t(lang, 'countingHint')}
|
| 51 |
+
</p>
|
| 52 |
+
</div>
|
| 53 |
+
|
| 54 |
+
{/* progress bar (indeterminate) */}
|
| 55 |
+
<div className="h-1.5 w-56 overflow-hidden rounded-full bg-slate-800">
|
| 56 |
+
<div className="h-full w-1/3 animate-[slide_1.5s_ease-in-out_infinite] rounded-full bg-emerald-500" />
|
| 57 |
+
</div>
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
<style>{`
|
| 61 |
+
@keyframes slide {
|
| 62 |
+
0% { transform: translateX(-100%) }
|
| 63 |
+
50% { transform: translateX(200%) }
|
| 64 |
+
100% { transform: translateX(200%) }
|
| 65 |
+
}
|
| 66 |
+
`}</style>
|
| 67 |
+
</div>
|
| 68 |
+
)
|
| 69 |
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Camera, ChevronLeft, ImagePlus, Loader2 } from 'lucide-react'
|
| 2 |
+
import { t } from '../lib/i18n'
|
| 3 |
+
import type { Lang } from '../lib/types'
|
| 4 |
+
import { SettingsPanel } from './SettingsPanel'
|
| 5 |
+
|
| 6 |
+
interface Props {
|
| 7 |
+
lang: Lang
|
| 8 |
+
imageSrc: string
|
| 9 |
+
imageDims: { w: number; h: number }
|
| 10 |
+
busy: boolean
|
| 11 |
+
modelReady: boolean
|
| 12 |
+
groupCount: number | 'auto'
|
| 13 |
+
enhanceImage: boolean
|
| 14 |
+
useTiling: boolean
|
| 15 |
+
filterOutliers: boolean
|
| 16 |
+
onBack: () => void
|
| 17 |
+
onRetake: () => void
|
| 18 |
+
onGallery: () => void
|
| 19 |
+
onCount: () => void
|
| 20 |
+
onGroupCount: (v: number | 'auto') => void
|
| 21 |
+
onEnhanceImage: (v: boolean) => void
|
| 22 |
+
onUseTiling: (v: boolean) => void
|
| 23 |
+
onFilterOutliers: (v: boolean) => void
|
| 24 |
+
onRecluster: () => void
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
export function StepPreview({
|
| 28 |
+
lang,
|
| 29 |
+
imageSrc,
|
| 30 |
+
imageDims,
|
| 31 |
+
busy,
|
| 32 |
+
modelReady,
|
| 33 |
+
groupCount,
|
| 34 |
+
enhanceImage,
|
| 35 |
+
useTiling,
|
| 36 |
+
filterOutliers,
|
| 37 |
+
onBack,
|
| 38 |
+
onRetake,
|
| 39 |
+
onGallery,
|
| 40 |
+
onCount,
|
| 41 |
+
onGroupCount,
|
| 42 |
+
onEnhanceImage,
|
| 43 |
+
onUseTiling,
|
| 44 |
+
onFilterOutliers,
|
| 45 |
+
onRecluster,
|
| 46 |
+
}: Props) {
|
| 47 |
+
const ar = imageDims.h / Math.max(imageDims.w, 1)
|
| 48 |
+
// cap preview height at 50vh
|
| 49 |
+
const maxPreviewH = Math.min(ar * 100, 50)
|
| 50 |
+
|
| 51 |
+
return (
|
| 52 |
+
<div className="flex flex-1 flex-col overflow-hidden">
|
| 53 |
+
{/* top bar */}
|
| 54 |
+
<div className="flex items-center justify-between px-4 py-2">
|
| 55 |
+
<button
|
| 56 |
+
type="button"
|
| 57 |
+
className="flex items-center gap-1 rounded-xl bg-slate-800 px-3 py-2 text-sm font-medium text-slate-200 active:scale-95"
|
| 58 |
+
onClick={onBack}
|
| 59 |
+
>
|
| 60 |
+
<ChevronLeft size={16} />
|
| 61 |
+
{t(lang, 'previewBack')}
|
| 62 |
+
</button>
|
| 63 |
+
<div className="flex gap-2">
|
| 64 |
+
<button
|
| 65 |
+
type="button"
|
| 66 |
+
className="flex h-9 w-9 items-center justify-center rounded-xl bg-slate-800 text-slate-300 active:scale-95"
|
| 67 |
+
onClick={onRetake}
|
| 68 |
+
aria-label={t(lang, 'retake')}
|
| 69 |
+
>
|
| 70 |
+
<Camera size={17} />
|
| 71 |
+
</button>
|
| 72 |
+
<button
|
| 73 |
+
type="button"
|
| 74 |
+
className="flex h-9 w-9 items-center justify-center rounded-xl bg-slate-800 text-slate-300 active:scale-95"
|
| 75 |
+
onClick={onGallery}
|
| 76 |
+
aria-label={t(lang, 'chooseFile')}
|
| 77 |
+
>
|
| 78 |
+
<ImagePlus size={17} />
|
| 79 |
+
</button>
|
| 80 |
+
</div>
|
| 81 |
+
</div>
|
| 82 |
+
|
| 83 |
+
{/* image preview */}
|
| 84 |
+
<div
|
| 85 |
+
className="mx-4 overflow-hidden rounded-2xl border border-slate-700/60 bg-black shadow-md"
|
| 86 |
+
style={{ height: `${maxPreviewH}vh` }}
|
| 87 |
+
>
|
| 88 |
+
<img
|
| 89 |
+
src={imageSrc}
|
| 90 |
+
alt="Preview"
|
| 91 |
+
className="h-full w-full object-contain"
|
| 92 |
+
/>
|
| 93 |
+
</div>
|
| 94 |
+
|
| 95 |
+
{/* image info */}
|
| 96 |
+
<p className="mt-1.5 px-4 text-xs text-slate-500">
|
| 97 |
+
{imageDims.w} × {imageDims.h} px
|
| 98 |
+
</p>
|
| 99 |
+
|
| 100 |
+
{/* scrollable settings */}
|
| 101 |
+
<div className="flex-1 overflow-y-auto px-4 pb-2 pt-3">
|
| 102 |
+
<SettingsPanel
|
| 103 |
+
lang={lang}
|
| 104 |
+
open={true}
|
| 105 |
+
groupCount={groupCount}
|
| 106 |
+
enhanceImage={enhanceImage}
|
| 107 |
+
useTiling={useTiling}
|
| 108 |
+
filterOutliers={filterOutliers}
|
| 109 |
+
onGroupCount={onGroupCount}
|
| 110 |
+
onEnhanceImage={onEnhanceImage}
|
| 111 |
+
onUseTiling={onUseTiling}
|
| 112 |
+
onFilterOutliers={onFilterOutliers}
|
| 113 |
+
onRecluster={onRecluster}
|
| 114 |
+
/>
|
| 115 |
+
</div>
|
| 116 |
+
|
| 117 |
+
{/* sticky count button */}
|
| 118 |
+
<div className="border-t border-slate-800/80 px-4 pb-[max(1rem,env(safe-area-inset-bottom))] pt-3">
|
| 119 |
+
<button
|
| 120 |
+
type="button"
|
| 121 |
+
disabled={busy || !modelReady}
|
| 122 |
+
className="flex h-14 w-full items-center justify-center gap-3 rounded-2xl bg-emerald-600 text-base font-bold text-white shadow-lg shadow-emerald-900/40 active:scale-[0.98] disabled:opacity-50"
|
| 123 |
+
onClick={onCount}
|
| 124 |
+
>
|
| 125 |
+
{busy ? <Loader2 className="animate-spin" size={22} /> : null}
|
| 126 |
+
{t(lang, 'countNow')}
|
| 127 |
+
</button>
|
| 128 |
+
</div>
|
| 129 |
+
</div>
|
| 130 |
+
)
|
| 131 |
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useRef, useState } from 'react'
|
| 2 |
+
import { Download, Plus, RotateCcw } from 'lucide-react'
|
| 3 |
+
import { t } from '../lib/i18n'
|
| 4 |
+
import type { Detection, DiameterGroup, Lang, ToolMode } from '../lib/types'
|
| 5 |
+
import { ResultCanvas } from './ResultCanvas'
|
| 6 |
+
import { GroupPanel } from './GroupPanel'
|
| 7 |
+
import { ToolBar } from './ToolBar'
|
| 8 |
+
import { ExportSheet } from './ExportSheet'
|
| 9 |
+
import { exportCanvasPng, exportCsv, exportJson } from '../lib/export'
|
| 10 |
+
|
| 11 |
+
interface Props {
|
| 12 |
+
lang: Lang
|
| 13 |
+
image: HTMLImageElement
|
| 14 |
+
detections: Detection[]
|
| 15 |
+
groups: DiameterGroup[]
|
| 16 |
+
counts: Record<number, number>
|
| 17 |
+
onUpdate: (dets: Detection[]) => void
|
| 18 |
+
onExpectedChange: (groupId: number, value: number | undefined) => void
|
| 19 |
+
onRecount: () => void
|
| 20 |
+
onNewPhoto: () => void
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
export function StepResults({
|
| 24 |
+
lang,
|
| 25 |
+
image,
|
| 26 |
+
detections,
|
| 27 |
+
groups,
|
| 28 |
+
counts,
|
| 29 |
+
onUpdate,
|
| 30 |
+
onExpectedChange,
|
| 31 |
+
onRecount,
|
| 32 |
+
onNewPhoto,
|
| 33 |
+
}: Props) {
|
| 34 |
+
const [tool, setTool] = useState<ToolMode>('view')
|
| 35 |
+
const [selectedGroupId, setSelectedGroupId] = useState(groups[0]?.id ?? 0)
|
| 36 |
+
const [animDone, setAnimDone] = useState(false)
|
| 37 |
+
const [exportOpen, setExportOpen] = useState(false)
|
| 38 |
+
const countDisplayRef = useRef<HTMLSpanElement>(null)
|
| 39 |
+
const canvasRef = useRef<HTMLCanvasElement | null>(null)
|
| 40 |
+
|
| 41 |
+
return (
|
| 42 |
+
<div className="flex flex-1 flex-col overflow-hidden">
|
| 43 |
+
{/* top bar */}
|
| 44 |
+
<div className="flex items-center justify-between px-4 py-2">
|
| 45 |
+
<button
|
| 46 |
+
type="button"
|
| 47 |
+
className="flex h-9 items-center gap-1.5 rounded-xl bg-slate-800 px-3 text-sm font-medium text-slate-200 active:scale-95"
|
| 48 |
+
onClick={onNewPhoto}
|
| 49 |
+
>
|
| 50 |
+
<Plus size={15} className="rotate-45" />
|
| 51 |
+
{t(lang, 'resultsBack')}
|
| 52 |
+
</button>
|
| 53 |
+
|
| 54 |
+
{/* live count hero */}
|
| 55 |
+
<div className="flex flex-col items-center">
|
| 56 |
+
<span
|
| 57 |
+
ref={countDisplayRef}
|
| 58 |
+
className="text-3xl font-bold tabular-nums leading-none text-white"
|
| 59 |
+
>
|
| 60 |
+
{detections.length}
|
| 61 |
+
</span>
|
| 62 |
+
<span className="text-[10px] uppercase tracking-widest text-slate-500">{t(lang, 'total')}</span>
|
| 63 |
+
</div>
|
| 64 |
+
|
| 65 |
+
<div className="flex gap-2">
|
| 66 |
+
<button
|
| 67 |
+
type="button"
|
| 68 |
+
className="flex h-9 w-9 items-center justify-center rounded-xl bg-slate-800 text-slate-300 active:scale-95"
|
| 69 |
+
onClick={() => setExportOpen(true)}
|
| 70 |
+
aria-label={t(lang, 'export')}
|
| 71 |
+
>
|
| 72 |
+
<Download size={17} />
|
| 73 |
+
</button>
|
| 74 |
+
<button
|
| 75 |
+
type="button"
|
| 76 |
+
className="flex h-9 w-9 items-center justify-center rounded-xl bg-emerald-700 text-white active:scale-95"
|
| 77 |
+
onClick={onRecount}
|
| 78 |
+
aria-label={t(lang, 'recount')}
|
| 79 |
+
>
|
| 80 |
+
<RotateCcw size={17} />
|
| 81 |
+
</button>
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
|
| 85 |
+
{/* scrollable body */}
|
| 86 |
+
<div className="flex-1 overflow-y-auto px-4 pb-[max(1.5rem,env(safe-area-inset-bottom))]">
|
| 87 |
+
<div className="space-y-4">
|
| 88 |
+
{/* group summary chips – quick overview */}
|
| 89 |
+
{groups.length > 1 && (
|
| 90 |
+
<div className="flex flex-wrap gap-2">
|
| 91 |
+
{groups.map((g) => (
|
| 92 |
+
<span
|
| 93 |
+
key={g.id}
|
| 94 |
+
className="inline-flex items-center gap-1.5 rounded-full bg-slate-800/80 px-3 py-1 text-sm font-medium text-slate-200 ring-1 ring-slate-700"
|
| 95 |
+
>
|
| 96 |
+
<span className="h-2.5 w-2.5 rounded-full" style={{ background: g.color }} />
|
| 97 |
+
{g.label}: {counts[g.id] ?? 0}
|
| 98 |
+
</span>
|
| 99 |
+
))}
|
| 100 |
+
</div>
|
| 101 |
+
)}
|
| 102 |
+
|
| 103 |
+
{/* canvas with animation */}
|
| 104 |
+
<ResultCanvas
|
| 105 |
+
image={image}
|
| 106 |
+
detections={detections}
|
| 107 |
+
groups={groups}
|
| 108 |
+
tool={tool}
|
| 109 |
+
selectedGroupId={selectedGroupId}
|
| 110 |
+
onUpdate={onUpdate}
|
| 111 |
+
onCanvasReady={(c) => { canvasRef.current = c }}
|
| 112 |
+
onAnimationComplete={() => setAnimDone(true)}
|
| 113 |
+
countDisplayRef={countDisplayRef}
|
| 114 |
+
/>
|
| 115 |
+
|
| 116 |
+
{/* correction tools (appear after animation) */}
|
| 117 |
+
{animDone && (
|
| 118 |
+
<>
|
| 119 |
+
<GroupPanel
|
| 120 |
+
lang={lang}
|
| 121 |
+
groups={groups}
|
| 122 |
+
counts={counts}
|
| 123 |
+
selectedGroupId={selectedGroupId}
|
| 124 |
+
onSelectGroup={setSelectedGroupId}
|
| 125 |
+
onExpectedChange={onExpectedChange}
|
| 126 |
+
/>
|
| 127 |
+
<ToolBar lang={lang} tool={tool} onToolChange={setTool} />
|
| 128 |
+
</>
|
| 129 |
+
)}
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
|
| 133 |
+
<ExportSheet
|
| 134 |
+
lang={lang}
|
| 135 |
+
open={exportOpen}
|
| 136 |
+
onClose={() => setExportOpen(false)}
|
| 137 |
+
onExportPng={() => canvasRef.current && exportCanvasPng(canvasRef.current)}
|
| 138 |
+
onExportJson={() => exportJson(detections, groups, counts)}
|
| 139 |
+
onExportCsv={() => exportCsv(detections, groups)}
|
| 140 |
+
canExportPng={!!canvasRef.current}
|
| 141 |
+
canExportData={detections.length > 0}
|
| 142 |
+
/>
|
| 143 |
+
</div>
|
| 144 |
+
)
|
| 145 |
+
}
|
|
@@ -4,6 +4,16 @@ const messages = {
|
|
| 4 |
en: {
|
| 5 |
title: 'NPI Pipe Count',
|
| 6 |
subtitle: 'Offline AI pipe counting',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
takePhoto: 'Take Photo',
|
| 8 |
chooseFile: 'Choose Image',
|
| 9 |
count: 'Count Pipes',
|
|
@@ -42,6 +52,16 @@ const messages = {
|
|
| 42 |
th: {
|
| 43 |
title: 'NPI นับท่อ',
|
| 44 |
subtitle: 'นับท่อออฟไลน์ด้วย AI',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
takePhoto: 'ถ่ายภาพ',
|
| 46 |
chooseFile: 'เลือกรูป',
|
| 47 |
count: 'นับท่อ',
|
|
|
|
| 4 |
en: {
|
| 5 |
title: 'NPI Pipe Count',
|
| 6 |
subtitle: 'Offline AI pipe counting',
|
| 7 |
+
// wizard
|
| 8 |
+
captureSubtitle: 'Take a photo or choose from gallery',
|
| 9 |
+
previewTitle: 'Configure & Count',
|
| 10 |
+
previewBack: 'Back',
|
| 11 |
+
countNow: 'Count Pipes',
|
| 12 |
+
retake: 'Retake',
|
| 13 |
+
countingTitle: 'Counting pipes…',
|
| 14 |
+
countingHint: 'Tap anywhere to skip preview animation',
|
| 15 |
+
resultsBack: 'New',
|
| 16 |
+
tapToSkip: 'Tap to skip animation',
|
| 17 |
takePhoto: 'Take Photo',
|
| 18 |
chooseFile: 'Choose Image',
|
| 19 |
count: 'Count Pipes',
|
|
|
|
| 52 |
th: {
|
| 53 |
title: 'NPI นับท่อ',
|
| 54 |
subtitle: 'นับท่อออฟไลน์ด้วย AI',
|
| 55 |
+
// wizard
|
| 56 |
+
captureSubtitle: 'ถ่ายภาพหรือเลือกจากคลัง',
|
| 57 |
+
previewTitle: 'ตั้งค่าและนับ',
|
| 58 |
+
previewBack: 'ย้อนกลับ',
|
| 59 |
+
countNow: 'นับท่อ',
|
| 60 |
+
retake: 'ถ่ายใหม่',
|
| 61 |
+
countingTitle: 'กำลังนับท่อ…',
|
| 62 |
+
countingHint: 'แตะเพื่อข้ามการแสดงผล',
|
| 63 |
+
resultsBack: 'ใหม่',
|
| 64 |
+
tapToSkip: 'แตะเพื่อข้ามแอนิเมชั่น',
|
| 65 |
takePhoto: 'ถ่ายภาพ',
|
| 66 |
chooseFile: 'เลือกรูป',
|
| 67 |
count: 'นับท่อ',
|