Spaces:
Runtime error
Runtime error
Khunndev Cursor commited on
Commit ·
ebe2f4e
1
Parent(s): d64e218
Default to server-side counting with configurable inference mode in settings
Browse files- frontend/src/App.tsx +95 -67
- frontend/src/components/SettingsPanel.tsx +50 -6
- frontend/src/components/StepPreview.tsx +10 -1
- frontend/src/lib/i18n.ts +14 -0
- frontend/src/lib/inferenceSettings.ts +21 -0
- frontend/src/lib/types.ts +3 -0
frontend/src/App.tsx
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
import { useEffect, useRef, useState } from 'react'
|
| 2 |
import { assignDiameterGroups, countByGroup } from './lib/clustering'
|
| 3 |
import { getActiveBackend, isLowMemoryDevice, loadModel, runDetection } from './lib/inference'
|
| 4 |
-
import {
|
|
|
|
| 5 |
import { t } from './lib/i18n'
|
| 6 |
-
import type { Detection, DiameterGroup, Lang } from './lib/types'
|
| 7 |
import { StepCapture } from './components/StepCapture'
|
| 8 |
import { StepCounting } from './components/StepCounting'
|
| 9 |
import { StepPreview } from './components/StepPreview'
|
|
@@ -11,11 +12,7 @@ import { StepResults } from './components/StepResults'
|
|
| 11 |
|
| 12 |
type Step = 'capture' | 'preview' | 'counting' | 'results'
|
| 13 |
|
| 14 |
-
//
|
| 15 |
-
// - checking: still probing capabilities
|
| 16 |
-
// - local: in-browser ONNX (WebGPU/WASM)
|
| 17 |
-
// - server: local unavailable, server-side fallback reachable
|
| 18 |
-
// - none: neither works (offline + can't load locally)
|
| 19 |
type Engine = 'checking' | 'local' | 'server' | 'none'
|
| 20 |
|
| 21 |
const STEPS: Step[] = ['capture', 'preview', 'counting', 'results']
|
|
@@ -25,8 +22,6 @@ export default function App() {
|
|
| 25 |
const [navDir, setNavDir] = useState<'push' | 'pop'>('push')
|
| 26 |
const [lang, setLang] = useState<Lang>('th')
|
| 27 |
|
| 28 |
-
// Navigate between steps, deriving the slide direction from step order so the
|
| 29 |
-
// transition reads like a native push (forward) / pop (back) navigation.
|
| 30 |
const navigate = (next: Step) => {
|
| 31 |
setNavDir(STEPS.indexOf(next) >= STEPS.indexOf(step) ? 'push' : 'pop')
|
| 32 |
setStep(next)
|
|
@@ -42,7 +37,8 @@ export default function App() {
|
|
| 42 |
const [detections, setDetections] = useState<Detection[]>([])
|
| 43 |
const [groups, setGroups] = useState<DiameterGroup[]>([])
|
| 44 |
|
| 45 |
-
// settings
|
|
|
|
| 46 |
const [groupCount, setGroupCount] = useState<number | 'auto'>(1)
|
| 47 |
const [useTiling, setUseTiling] = useState(false)
|
| 48 |
const [filterOutliers, setFilterOutliers] = useState(false)
|
|
@@ -56,42 +52,67 @@ export default function App() {
|
|
| 56 |
|
| 57 |
const fileRef = useRef<HTMLInputElement>(null)
|
| 58 |
const cameraRef = useRef<HTMLInputElement>(null)
|
|
|
|
|
|
|
| 59 |
|
| 60 |
const counts = countByGroup(detections, groups)
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
const initEngine = () => {
|
| 65 |
setModelError('')
|
| 66 |
setEngine('checking')
|
| 67 |
-
|
| 68 |
-
isServerDetectAvailable().then(setServerAvailable).catch(() => setServerAvailable(false))
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
setEngine('local')
|
| 73 |
setStatus('')
|
| 74 |
-
})
|
| 75 |
-
|
| 76 |
-
|
| 77 |
setStatus('')
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
useEffect(() => {
|
| 92 |
-
initEngine()
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
// -------------------------------------------------------------------------
|
| 97 |
// file / camera
|
|
@@ -112,7 +133,7 @@ export default function App() {
|
|
| 112 |
}
|
| 113 |
|
| 114 |
// -------------------------------------------------------------------------
|
| 115 |
-
// counting —
|
| 116 |
// -------------------------------------------------------------------------
|
| 117 |
const detectLocal = (img: HTMLImageElement) =>
|
| 118 |
runDetection(img, {
|
|
@@ -131,30 +152,44 @@ export default function App() {
|
|
| 131 |
setBusy(true)
|
| 132 |
setStatus(t(lang, 'processing'))
|
| 133 |
navigate('counting')
|
|
|
|
|
|
|
|
|
|
| 134 |
try {
|
| 135 |
let dets: Detection[]
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
// during local inference. When the server fallback is reachable, offload
|
| 139 |
-
// these heavy jobs to it instead of risking a crash.
|
| 140 |
-
const megapixels = (imageDims.w * imageDims.h) / 1_000_000
|
| 141 |
-
const offloadHeavy =
|
| 142 |
-
engine === 'local' && serverAvailable && !!imageFile && isLowMemoryDevice() && megapixels >= 4
|
| 143 |
-
|
| 144 |
-
if (engine === 'server' || offloadHeavy) {
|
| 145 |
if (!imageFile) throw new Error('Image unavailable for server detection')
|
| 146 |
-
dets = await detectServer(imageFile)
|
| 147 |
-
} else {
|
| 148 |
-
// local engine — but if it fails at runtime, transparently fall back
|
| 149 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
dets = await detectLocal(image)
|
| 151 |
-
|
| 152 |
-
if (
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
| 159 |
}
|
| 160 |
}
|
|
@@ -162,7 +197,7 @@ export default function App() {
|
|
| 162 |
const clustered = assignDiameterGroups(dets, groupCount, lang)
|
| 163 |
setDetections(clustered.detections)
|
| 164 |
setGroups(clustered.groups)
|
| 165 |
-
setStatus(`Done (${
|
| 166 |
navigate('results')
|
| 167 |
} catch (err) {
|
| 168 |
setStatus(err instanceof Error ? err.message : String(err))
|
|
@@ -191,14 +226,10 @@ export default function App() {
|
|
| 191 |
setImageFile(null)
|
| 192 |
}
|
| 193 |
|
| 194 |
-
// -------------------------------------------------------------------------
|
| 195 |
-
// header step indicator
|
| 196 |
-
// -------------------------------------------------------------------------
|
| 197 |
const stepIdx = STEPS.indexOf(step)
|
| 198 |
|
| 199 |
return (
|
| 200 |
<div className="fixed inset-0 flex flex-col overflow-hidden bg-[var(--bg)]">
|
| 201 |
-
{/* ── Header ──────────────────────────────────────────────────── */}
|
| 202 |
<header
|
| 203 |
className="relative flex shrink-0 items-center justify-between bg-[var(--bg)] px-4"
|
| 204 |
style={{
|
|
@@ -206,7 +237,6 @@ export default function App() {
|
|
| 206 |
paddingBottom: '0.625rem',
|
| 207 |
}}
|
| 208 |
>
|
| 209 |
-
{/* App brand */}
|
| 210 |
<div className="flex items-center gap-2.5">
|
| 211 |
<div className="relative">
|
| 212 |
<img src="/icon.svg" alt="" className="h-7 w-7 rounded-xl" />
|
|
@@ -227,7 +257,6 @@ export default function App() {
|
|
| 227 |
</span>
|
| 228 |
</div>
|
| 229 |
|
| 230 |
-
{/* Step progress pills */}
|
| 231 |
<div className="flex items-center gap-1">
|
| 232 |
{STEPS.map((s, i) => (
|
| 233 |
<span
|
|
@@ -243,7 +272,6 @@ export default function App() {
|
|
| 243 |
))}
|
| 244 |
</div>
|
| 245 |
|
| 246 |
-
{/* Lang toggle */}
|
| 247 |
<button
|
| 248 |
type="button"
|
| 249 |
className="rounded-lg bg-white/8 px-3 py-1.5 text-xs font-semibold text-white/70 transition-all duration-150 active:scale-95 active:bg-white/15"
|
|
@@ -252,11 +280,9 @@ export default function App() {
|
|
| 252 |
{lang === 'th' ? 'EN' : 'ภ'}
|
| 253 |
</button>
|
| 254 |
|
| 255 |
-
{/* Bottom separator — subtle gradient line */}
|
| 256 |
<div className="absolute inset-x-0 bottom-0 h-px bg-gradient-to-r from-transparent via-white/10 to-transparent" />
|
| 257 |
</header>
|
| 258 |
|
| 259 |
-
{/* ── Step views — key forces remount → triggers directional nav animation ── */}
|
| 260 |
<div key={step} className={`${navDir === 'push' ? 'nav-push' : 'nav-pop'} flex flex-1 flex-col overflow-hidden`}>
|
| 261 |
{step === 'capture' && (
|
| 262 |
<StepCapture
|
|
@@ -275,8 +301,10 @@ export default function App() {
|
|
| 275 |
imageSrc={imageSrc}
|
| 276 |
imageDims={imageDims}
|
| 277 |
busy={busy}
|
| 278 |
-
modelReady={
|
| 279 |
groupCount={groupCount}
|
|
|
|
|
|
|
| 280 |
useTiling={useTiling}
|
| 281 |
filterOutliers={filterOutliers}
|
| 282 |
onBack={resetToCapture}
|
|
@@ -284,6 +312,7 @@ export default function App() {
|
|
| 284 |
onGallery={() => fileRef.current?.click()}
|
| 285 |
onCount={runCount}
|
| 286 |
onGroupCount={setGroupCount}
|
|
|
|
| 287 |
onUseTiling={setUseTiling}
|
| 288 |
onFilterOutliers={setFilterOutliers}
|
| 289 |
onRecluster={recluster}
|
|
@@ -309,7 +338,6 @@ export default function App() {
|
|
| 309 |
)}
|
| 310 |
</div>
|
| 311 |
|
| 312 |
-
{/* ── Hidden file inputs ─────────────────────────────────────── */}
|
| 313 |
<input
|
| 314 |
ref={fileRef}
|
| 315 |
type="file"
|
|
|
|
| 1 |
+
import { useCallback, useEffect, useRef, useState } from 'react'
|
| 2 |
import { assignDiameterGroups, countByGroup } from './lib/clustering'
|
| 3 |
import { getActiveBackend, isLowMemoryDevice, loadModel, runDetection } from './lib/inference'
|
| 4 |
+
import { loadInferenceMode, saveInferenceMode } from './lib/inferenceSettings'
|
| 5 |
+
import { getServerBackend, isServerDetectAvailable, serverDetect } from './lib/serverDetect'
|
| 6 |
import { t } from './lib/i18n'
|
| 7 |
+
import type { Detection, DiameterGroup, InferenceMode, Lang } from './lib/types'
|
| 8 |
import { StepCapture } from './components/StepCapture'
|
| 9 |
import { StepCounting } from './components/StepCounting'
|
| 10 |
import { StepPreview } from './components/StepPreview'
|
|
|
|
| 12 |
|
| 13 |
type Step = 'capture' | 'preview' | 'counting' | 'results'
|
| 14 |
|
| 15 |
+
// Runtime capability — which engine is actually ready right now.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
type Engine = 'checking' | 'local' | 'server' | 'none'
|
| 17 |
|
| 18 |
const STEPS: Step[] = ['capture', 'preview', 'counting', 'results']
|
|
|
|
| 22 |
const [navDir, setNavDir] = useState<'push' | 'pop'>('push')
|
| 23 |
const [lang, setLang] = useState<Lang>('th')
|
| 24 |
|
|
|
|
|
|
|
| 25 |
const navigate = (next: Step) => {
|
| 26 |
setNavDir(STEPS.indexOf(next) >= STEPS.indexOf(step) ? 'push' : 'pop')
|
| 27 |
setStep(next)
|
|
|
|
| 37 |
const [detections, setDetections] = useState<Detection[]>([])
|
| 38 |
const [groups, setGroups] = useState<DiameterGroup[]>([])
|
| 39 |
|
| 40 |
+
// settings — server is the default counting mode
|
| 41 |
+
const [inferenceMode, setInferenceMode] = useState<InferenceMode>(loadInferenceMode)
|
| 42 |
const [groupCount, setGroupCount] = useState<number | 'auto'>(1)
|
| 43 |
const [useTiling, setUseTiling] = useState(false)
|
| 44 |
const [filterOutliers, setFilterOutliers] = useState(false)
|
|
|
|
| 52 |
|
| 53 |
const fileRef = useRef<HTMLInputElement>(null)
|
| 54 |
const cameraRef = useRef<HTMLInputElement>(null)
|
| 55 |
+
const inferenceModeRef = useRef(inferenceMode)
|
| 56 |
+
inferenceModeRef.current = inferenceMode
|
| 57 |
|
| 58 |
const counts = countByGroup(detections, groups)
|
| 59 |
+
const canCount = engine === 'local' || engine === 'server'
|
| 60 |
|
| 61 |
+
const initEngine = useCallback(async () => {
|
| 62 |
+
const mode = inferenceModeRef.current
|
|
|
|
| 63 |
setModelError('')
|
| 64 |
setEngine('checking')
|
| 65 |
+
setStatus(mode === 'local' ? t(lang, 'loadingModel') : '')
|
|
|
|
| 66 |
|
| 67 |
+
const serverOk = await isServerDetectAvailable().catch(() => false)
|
| 68 |
+
setServerAvailable(serverOk)
|
| 69 |
+
|
| 70 |
+
if (mode === 'server') {
|
| 71 |
+
if (serverOk) {
|
| 72 |
+
setEngine('server')
|
| 73 |
+
setStatus('')
|
| 74 |
+
return
|
| 75 |
+
}
|
| 76 |
+
// server preferred but unreachable — try loading the on-device model
|
| 77 |
+
try {
|
| 78 |
+
await loadModel(setStatus)
|
| 79 |
setEngine('local')
|
| 80 |
setStatus('')
|
| 81 |
+
} catch (err: unknown) {
|
| 82 |
+
setEngine('none')
|
| 83 |
+
setModelError(err instanceof Error ? err.message : String(err))
|
| 84 |
setStatus('')
|
| 85 |
+
}
|
| 86 |
+
return
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// local mode — load on-device model; keep server as runtime fallback
|
| 90 |
+
try {
|
| 91 |
+
await loadModel(setStatus)
|
| 92 |
+
setEngine('local')
|
| 93 |
+
setStatus('')
|
| 94 |
+
} catch (err: unknown) {
|
| 95 |
+
const message = err instanceof Error ? err.message : String(err)
|
| 96 |
+
if (serverOk) {
|
| 97 |
+
setEngine('server')
|
| 98 |
+
setModelError('')
|
| 99 |
+
setStatus('')
|
| 100 |
+
} else {
|
| 101 |
+
setEngine('none')
|
| 102 |
+
setModelError(message)
|
| 103 |
+
setStatus('')
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
}, [lang])
|
| 107 |
|
| 108 |
useEffect(() => {
|
| 109 |
+
void initEngine()
|
| 110 |
+
}, [initEngine, inferenceMode])
|
| 111 |
+
|
| 112 |
+
const handleInferenceMode = (mode: InferenceMode) => {
|
| 113 |
+
saveInferenceMode(mode)
|
| 114 |
+
setInferenceMode(mode)
|
| 115 |
+
}
|
| 116 |
|
| 117 |
// -------------------------------------------------------------------------
|
| 118 |
// file / camera
|
|
|
|
| 133 |
}
|
| 134 |
|
| 135 |
// -------------------------------------------------------------------------
|
| 136 |
+
// counting — follows the user's inference mode setting
|
| 137 |
// -------------------------------------------------------------------------
|
| 138 |
const detectLocal = (img: HTMLImageElement) =>
|
| 139 |
runDetection(img, {
|
|
|
|
| 152 |
setBusy(true)
|
| 153 |
setStatus(t(lang, 'processing'))
|
| 154 |
navigate('counting')
|
| 155 |
+
|
| 156 |
+
let usedBackend = inferenceMode === 'server' && serverAvailable ? 'server' : getActiveBackend()
|
| 157 |
+
|
| 158 |
try {
|
| 159 |
let dets: Detection[]
|
| 160 |
+
const useServer = inferenceMode === 'server' && serverAvailable
|
| 161 |
|
| 162 |
+
if (useServer) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
if (!imageFile) throw new Error('Image unavailable for server detection')
|
|
|
|
|
|
|
|
|
|
| 164 |
try {
|
| 165 |
+
dets = await detectServer(imageFile)
|
| 166 |
+
usedBackend = getServerBackend()
|
| 167 |
+
} catch (serverErr) {
|
| 168 |
+
setStatus(t(lang, 'fallbackLocal'))
|
| 169 |
dets = await detectLocal(image)
|
| 170 |
+
usedBackend = getActiveBackend()
|
| 171 |
+
if (usedBackend === 'loading') throw serverErr
|
| 172 |
+
}
|
| 173 |
+
} else {
|
| 174 |
+
const megapixels = (imageDims.w * imageDims.h) / 1_000_000
|
| 175 |
+
const offloadHeavy =
|
| 176 |
+
serverAvailable && !!imageFile && isLowMemoryDevice() && megapixels >= 4
|
| 177 |
+
|
| 178 |
+
if (offloadHeavy) {
|
| 179 |
+
dets = await detectServer(imageFile!)
|
| 180 |
+
usedBackend = getServerBackend()
|
| 181 |
+
} else {
|
| 182 |
+
try {
|
| 183 |
+
dets = await detectLocal(image)
|
| 184 |
+
usedBackend = getActiveBackend()
|
| 185 |
+
} catch (localErr) {
|
| 186 |
+
if (serverAvailable && imageFile) {
|
| 187 |
+
setStatus(t(lang, 'fallbackServer'))
|
| 188 |
+
dets = await detectServer(imageFile)
|
| 189 |
+
usedBackend = getServerBackend()
|
| 190 |
+
} else {
|
| 191 |
+
throw localErr
|
| 192 |
+
}
|
| 193 |
}
|
| 194 |
}
|
| 195 |
}
|
|
|
|
| 197 |
const clustered = assignDiameterGroups(dets, groupCount, lang)
|
| 198 |
setDetections(clustered.detections)
|
| 199 |
setGroups(clustered.groups)
|
| 200 |
+
setStatus(`Done (${usedBackend})`)
|
| 201 |
navigate('results')
|
| 202 |
} catch (err) {
|
| 203 |
setStatus(err instanceof Error ? err.message : String(err))
|
|
|
|
| 226 |
setImageFile(null)
|
| 227 |
}
|
| 228 |
|
|
|
|
|
|
|
|
|
|
| 229 |
const stepIdx = STEPS.indexOf(step)
|
| 230 |
|
| 231 |
return (
|
| 232 |
<div className="fixed inset-0 flex flex-col overflow-hidden bg-[var(--bg)]">
|
|
|
|
| 233 |
<header
|
| 234 |
className="relative flex shrink-0 items-center justify-between bg-[var(--bg)] px-4"
|
| 235 |
style={{
|
|
|
|
| 237 |
paddingBottom: '0.625rem',
|
| 238 |
}}
|
| 239 |
>
|
|
|
|
| 240 |
<div className="flex items-center gap-2.5">
|
| 241 |
<div className="relative">
|
| 242 |
<img src="/icon.svg" alt="" className="h-7 w-7 rounded-xl" />
|
|
|
|
| 257 |
</span>
|
| 258 |
</div>
|
| 259 |
|
|
|
|
| 260 |
<div className="flex items-center gap-1">
|
| 261 |
{STEPS.map((s, i) => (
|
| 262 |
<span
|
|
|
|
| 272 |
))}
|
| 273 |
</div>
|
| 274 |
|
|
|
|
| 275 |
<button
|
| 276 |
type="button"
|
| 277 |
className="rounded-lg bg-white/8 px-3 py-1.5 text-xs font-semibold text-white/70 transition-all duration-150 active:scale-95 active:bg-white/15"
|
|
|
|
| 280 |
{lang === 'th' ? 'EN' : 'ภ'}
|
| 281 |
</button>
|
| 282 |
|
|
|
|
| 283 |
<div className="absolute inset-x-0 bottom-0 h-px bg-gradient-to-r from-transparent via-white/10 to-transparent" />
|
| 284 |
</header>
|
| 285 |
|
|
|
|
| 286 |
<div key={step} className={`${navDir === 'push' ? 'nav-push' : 'nav-pop'} flex flex-1 flex-col overflow-hidden`}>
|
| 287 |
{step === 'capture' && (
|
| 288 |
<StepCapture
|
|
|
|
| 301 |
imageSrc={imageSrc}
|
| 302 |
imageDims={imageDims}
|
| 303 |
busy={busy}
|
| 304 |
+
modelReady={canCount}
|
| 305 |
groupCount={groupCount}
|
| 306 |
+
inferenceMode={inferenceMode}
|
| 307 |
+
serverAvailable={serverAvailable}
|
| 308 |
useTiling={useTiling}
|
| 309 |
filterOutliers={filterOutliers}
|
| 310 |
onBack={resetToCapture}
|
|
|
|
| 312 |
onGallery={() => fileRef.current?.click()}
|
| 313 |
onCount={runCount}
|
| 314 |
onGroupCount={setGroupCount}
|
| 315 |
+
onInferenceMode={handleInferenceMode}
|
| 316 |
onUseTiling={setUseTiling}
|
| 317 |
onFilterOutliers={setFilterOutliers}
|
| 318 |
onRecluster={recluster}
|
|
|
|
| 338 |
)}
|
| 339 |
</div>
|
| 340 |
|
|
|
|
| 341 |
<input
|
| 342 |
ref={fileRef}
|
| 343 |
type="file"
|
frontend/src/components/SettingsPanel.tsx
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
import { t } from '../lib/i18n'
|
| 2 |
-
import type { Lang } from '../lib/types'
|
| 3 |
|
| 4 |
interface Props {
|
| 5 |
lang: Lang
|
| 6 |
open: boolean
|
| 7 |
groupCount: number | 'auto'
|
|
|
|
|
|
|
| 8 |
useTiling: boolean
|
| 9 |
filterOutliers: boolean
|
| 10 |
onGroupCount: (value: number | 'auto') => void
|
|
|
|
| 11 |
onUseTiling: (value: boolean) => void
|
| 12 |
onFilterOutliers: (value: boolean) => void
|
| 13 |
onRecluster: () => void
|
|
@@ -39,7 +42,6 @@ function Toggle({
|
|
| 39 |
<span className="text-xs text-white/35">{description}</span>
|
| 40 |
)}
|
| 41 |
</span>
|
| 42 |
-
{/* Native-size iOS toggle */}
|
| 43 |
<span
|
| 44 |
className={`relative h-[31px] w-[51px] shrink-0 rounded-full transition-colors duration-200 ${
|
| 45 |
checked ? 'bg-[#34c759]' : 'bg-white/20'
|
|
@@ -57,20 +59,20 @@ function Toggle({
|
|
| 57 |
)
|
| 58 |
}
|
| 59 |
|
| 60 |
-
/** Inset separator line between list rows */
|
| 61 |
function Separator() {
|
| 62 |
-
return
|
| 63 |
-
<div className="ml-4 h-px bg-white/8" />
|
| 64 |
-
)
|
| 65 |
}
|
| 66 |
|
| 67 |
export function SettingsPanel({
|
| 68 |
lang,
|
| 69 |
open,
|
| 70 |
groupCount,
|
|
|
|
|
|
|
| 71 |
useTiling,
|
| 72 |
filterOutliers,
|
| 73 |
onGroupCount,
|
|
|
|
| 74 |
onUseTiling,
|
| 75 |
onFilterOutliers,
|
| 76 |
onRecluster,
|
|
@@ -79,6 +81,48 @@ export function SettingsPanel({
|
|
| 79 |
|
| 80 |
return (
|
| 81 |
<div className="space-y-4">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
{/* ── Group count section ──────────────────────────────────── */}
|
| 83 |
<section>
|
| 84 |
<p className="mb-2 px-1 text-xs font-medium uppercase tracking-widest text-white/35">
|
|
|
|
| 1 |
import { t } from '../lib/i18n'
|
| 2 |
+
import type { InferenceMode, Lang } from '../lib/types'
|
| 3 |
|
| 4 |
interface Props {
|
| 5 |
lang: Lang
|
| 6 |
open: boolean
|
| 7 |
groupCount: number | 'auto'
|
| 8 |
+
inferenceMode: InferenceMode
|
| 9 |
+
serverAvailable: boolean
|
| 10 |
useTiling: boolean
|
| 11 |
filterOutliers: boolean
|
| 12 |
onGroupCount: (value: number | 'auto') => void
|
| 13 |
+
onInferenceMode: (mode: InferenceMode) => void
|
| 14 |
onUseTiling: (value: boolean) => void
|
| 15 |
onFilterOutliers: (value: boolean) => void
|
| 16 |
onRecluster: () => void
|
|
|
|
| 42 |
<span className="text-xs text-white/35">{description}</span>
|
| 43 |
)}
|
| 44 |
</span>
|
|
|
|
| 45 |
<span
|
| 46 |
className={`relative h-[31px] w-[51px] shrink-0 rounded-full transition-colors duration-200 ${
|
| 47 |
checked ? 'bg-[#34c759]' : 'bg-white/20'
|
|
|
|
| 59 |
)
|
| 60 |
}
|
| 61 |
|
|
|
|
| 62 |
function Separator() {
|
| 63 |
+
return <div className="ml-4 h-px bg-white/8" />
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
export function SettingsPanel({
|
| 67 |
lang,
|
| 68 |
open,
|
| 69 |
groupCount,
|
| 70 |
+
inferenceMode,
|
| 71 |
+
serverAvailable,
|
| 72 |
useTiling,
|
| 73 |
filterOutliers,
|
| 74 |
onGroupCount,
|
| 75 |
+
onInferenceMode,
|
| 76 |
onUseTiling,
|
| 77 |
onFilterOutliers,
|
| 78 |
onRecluster,
|
|
|
|
| 81 |
|
| 82 |
return (
|
| 83 |
<div className="space-y-4">
|
| 84 |
+
{/* ── Inference mode ─────────────────────────────────────────── */}
|
| 85 |
+
<section>
|
| 86 |
+
<p className="mb-2 px-1 text-xs font-medium uppercase tracking-widest text-white/35">
|
| 87 |
+
{t(lang, 'inferenceMode')}
|
| 88 |
+
</p>
|
| 89 |
+
<div className="overflow-hidden rounded-2xl bg-white/6 ring-1 ring-white/8 p-3">
|
| 90 |
+
<div className="flex gap-2">
|
| 91 |
+
{(['server', 'local'] as InferenceMode[]).map((mode) => {
|
| 92 |
+
const active = inferenceMode === mode
|
| 93 |
+
const disabled = mode === 'server' && !serverAvailable
|
| 94 |
+
return (
|
| 95 |
+
<button
|
| 96 |
+
key={mode}
|
| 97 |
+
type="button"
|
| 98 |
+
disabled={disabled}
|
| 99 |
+
className={`flex flex-1 flex-col items-center gap-0.5 rounded-xl px-3 py-2.5 text-center transition-all duration-150 active:scale-[0.98] disabled:opacity-40 ${
|
| 100 |
+
active
|
| 101 |
+
? mode === 'server'
|
| 102 |
+
? 'bg-sky-500 text-white shadow-md shadow-sky-900/40'
|
| 103 |
+
: 'bg-emerald-600 text-white shadow-md shadow-emerald-900/40'
|
| 104 |
+
: 'bg-white/8 text-white/65 active:bg-white/15'
|
| 105 |
+
}`}
|
| 106 |
+
onClick={() => onInferenceMode(mode)}
|
| 107 |
+
>
|
| 108 |
+
<span className="text-sm font-semibold">
|
| 109 |
+
{mode === 'server' ? t(lang, 'inferenceServer') : t(lang, 'inferenceLocal')}
|
| 110 |
+
</span>
|
| 111 |
+
<span className={`text-[10px] leading-tight ${active ? 'text-white/80' : 'text-white/35'}`}>
|
| 112 |
+
{mode === 'server' ? t(lang, 'inferenceServerHint') : t(lang, 'inferenceLocalHint')}
|
| 113 |
+
</span>
|
| 114 |
+
</button>
|
| 115 |
+
)
|
| 116 |
+
})}
|
| 117 |
+
</div>
|
| 118 |
+
{!serverAvailable && inferenceMode === 'local' && (
|
| 119 |
+
<p className="mt-2 px-1 text-center text-[11px] text-amber-400/80">
|
| 120 |
+
{t(lang, 'serverUnavailable')}
|
| 121 |
+
</p>
|
| 122 |
+
)}
|
| 123 |
+
</div>
|
| 124 |
+
</section>
|
| 125 |
+
|
| 126 |
{/* ── Group count section ──────────────────────────────────── */}
|
| 127 |
<section>
|
| 128 |
<p className="mb-2 px-1 text-xs font-medium uppercase tracking-widest text-white/35">
|
frontend/src/components/StepPreview.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 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 {
|
|
@@ -10,6 +10,8 @@ interface Props {
|
|
| 10 |
busy: boolean
|
| 11 |
modelReady: boolean
|
| 12 |
groupCount: number | 'auto'
|
|
|
|
|
|
|
| 13 |
useTiling: boolean
|
| 14 |
filterOutliers: boolean
|
| 15 |
onBack: () => void
|
|
@@ -17,6 +19,7 @@ interface Props {
|
|
| 17 |
onGallery: () => void
|
| 18 |
onCount: () => void
|
| 19 |
onGroupCount: (v: number | 'auto') => void
|
|
|
|
| 20 |
onUseTiling: (v: boolean) => void
|
| 21 |
onFilterOutliers: (v: boolean) => void
|
| 22 |
onRecluster: () => void
|
|
@@ -29,6 +32,8 @@ export function StepPreview({
|
|
| 29 |
busy,
|
| 30 |
modelReady,
|
| 31 |
groupCount,
|
|
|
|
|
|
|
| 32 |
useTiling,
|
| 33 |
filterOutliers,
|
| 34 |
onBack,
|
|
@@ -36,6 +41,7 @@ export function StepPreview({
|
|
| 36 |
onGallery,
|
| 37 |
onCount,
|
| 38 |
onGroupCount,
|
|
|
|
| 39 |
onUseTiling,
|
| 40 |
onFilterOutliers,
|
| 41 |
onRecluster,
|
|
@@ -95,9 +101,12 @@ export function StepPreview({
|
|
| 95 |
lang={lang}
|
| 96 |
open={true}
|
| 97 |
groupCount={groupCount}
|
|
|
|
|
|
|
| 98 |
useTiling={useTiling}
|
| 99 |
filterOutliers={filterOutliers}
|
| 100 |
onGroupCount={onGroupCount}
|
|
|
|
| 101 |
onUseTiling={onUseTiling}
|
| 102 |
onFilterOutliers={onFilterOutliers}
|
| 103 |
onRecluster={onRecluster}
|
|
|
|
| 1 |
import { Camera, ChevronLeft, ImagePlus, Loader2 } from 'lucide-react'
|
| 2 |
import { t } from '../lib/i18n'
|
| 3 |
+
import type { InferenceMode, Lang } from '../lib/types'
|
| 4 |
import { SettingsPanel } from './SettingsPanel'
|
| 5 |
|
| 6 |
interface Props {
|
|
|
|
| 10 |
busy: boolean
|
| 11 |
modelReady: boolean
|
| 12 |
groupCount: number | 'auto'
|
| 13 |
+
inferenceMode: InferenceMode
|
| 14 |
+
serverAvailable: boolean
|
| 15 |
useTiling: boolean
|
| 16 |
filterOutliers: boolean
|
| 17 |
onBack: () => void
|
|
|
|
| 19 |
onGallery: () => void
|
| 20 |
onCount: () => void
|
| 21 |
onGroupCount: (v: number | 'auto') => void
|
| 22 |
+
onInferenceMode: (mode: InferenceMode) => void
|
| 23 |
onUseTiling: (v: boolean) => void
|
| 24 |
onFilterOutliers: (v: boolean) => void
|
| 25 |
onRecluster: () => void
|
|
|
|
| 32 |
busy,
|
| 33 |
modelReady,
|
| 34 |
groupCount,
|
| 35 |
+
inferenceMode,
|
| 36 |
+
serverAvailable,
|
| 37 |
useTiling,
|
| 38 |
filterOutliers,
|
| 39 |
onBack,
|
|
|
|
| 41 |
onGallery,
|
| 42 |
onCount,
|
| 43 |
onGroupCount,
|
| 44 |
+
onInferenceMode,
|
| 45 |
onUseTiling,
|
| 46 |
onFilterOutliers,
|
| 47 |
onRecluster,
|
|
|
|
| 101 |
lang={lang}
|
| 102 |
open={true}
|
| 103 |
groupCount={groupCount}
|
| 104 |
+
inferenceMode={inferenceMode}
|
| 105 |
+
serverAvailable={serverAvailable}
|
| 106 |
useTiling={useTiling}
|
| 107 |
filterOutliers={filterOutliers}
|
| 108 |
onGroupCount={onGroupCount}
|
| 109 |
+
onInferenceMode={onInferenceMode}
|
| 110 |
onUseTiling={onUseTiling}
|
| 111 |
onFilterOutliers={onFilterOutliers}
|
| 112 |
onRecluster={onRecluster}
|
frontend/src/lib/i18n.ts
CHANGED
|
@@ -25,6 +25,13 @@ const messages = {
|
|
| 25 |
retry: 'Retry',
|
| 26 |
cloudMode: 'Cloud mode',
|
| 27 |
fallbackServer: 'Switching to server…',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
processing: 'Processing...',
|
| 29 |
pleaseWait: 'This may take a moment on large photos.',
|
| 30 |
total: 'Total pipes',
|
|
@@ -77,6 +84,13 @@ const messages = {
|
|
| 77 |
retry: 'ลองใหม่',
|
| 78 |
cloudMode: 'โหมดเซิร์ฟเวอร์',
|
| 79 |
fallbackServer: 'กำลังสลับไปเซิร์ฟเวอร์…',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
processing: 'กำลังประมวลผล...',
|
| 81 |
pleaseWait: 'รูปขนาดใหญ่อาจใช้เวลาสักครู่',
|
| 82 |
total: 'จำนวนท่อรวม',
|
|
|
|
| 25 |
retry: 'Retry',
|
| 26 |
cloudMode: 'Cloud mode',
|
| 27 |
fallbackServer: 'Switching to server…',
|
| 28 |
+
fallbackLocal: 'Switching to this device…',
|
| 29 |
+
inferenceMode: 'Counting mode',
|
| 30 |
+
inferenceServer: 'Server',
|
| 31 |
+
inferenceLocal: 'This device',
|
| 32 |
+
inferenceServerHint: 'Recommended — works on all phones',
|
| 33 |
+
inferenceLocalHint: 'Offline — needs a capable device',
|
| 34 |
+
serverUnavailable: 'Server unavailable',
|
| 35 |
processing: 'Processing...',
|
| 36 |
pleaseWait: 'This may take a moment on large photos.',
|
| 37 |
total: 'Total pipes',
|
|
|
|
| 84 |
retry: 'ลองใหม่',
|
| 85 |
cloudMode: 'โหมดเซิร์ฟเวอร์',
|
| 86 |
fallbackServer: 'กำลังสลับไปเซิร์ฟเวอร์…',
|
| 87 |
+
fallbackLocal: 'กำลังสลับไปอุปกรณ์นี้…',
|
| 88 |
+
inferenceMode: 'โหมดการนับ',
|
| 89 |
+
inferenceServer: 'เซิร์ฟเวอร์',
|
| 90 |
+
inferenceLocal: 'อุปกรณ์นี้',
|
| 91 |
+
inferenceServerHint: 'แนะนำ — ใช้ได้ทุกเครื่อง',
|
| 92 |
+
inferenceLocalHint: 'ออฟไลน์ — ต้องใช้เครื่องที่รองรับ',
|
| 93 |
+
serverUnavailable: 'เซิร์ฟเวอร์ไม่พร้อม',
|
| 94 |
processing: 'กำลังประมวลผล...',
|
| 95 |
pleaseWait: 'รูปขนาดใหญ่อาจใช้เวลาสักครู่',
|
| 96 |
total: 'จำนวนท่อรวม',
|
frontend/src/lib/inferenceSettings.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { InferenceMode } from './types'
|
| 2 |
+
|
| 3 |
+
const STORAGE_KEY = 'pipecount-inference-mode'
|
| 4 |
+
|
| 5 |
+
export function loadInferenceMode(): InferenceMode {
|
| 6 |
+
try {
|
| 7 |
+
const v = localStorage.getItem(STORAGE_KEY)
|
| 8 |
+
if (v === 'local' || v === 'server') return v
|
| 9 |
+
} catch {
|
| 10 |
+
/* private browsing / blocked storage */
|
| 11 |
+
}
|
| 12 |
+
return 'server'
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
export function saveInferenceMode(mode: InferenceMode): void {
|
| 16 |
+
try {
|
| 17 |
+
localStorage.setItem(STORAGE_KEY, mode)
|
| 18 |
+
} catch {
|
| 19 |
+
/* ignore */
|
| 20 |
+
}
|
| 21 |
+
}
|
frontend/src/lib/types.ts
CHANGED
|
@@ -23,6 +23,9 @@ export type ToolMode = 'view' | 'delete' | 'add' | 'reassign' | 'area'
|
|
| 23 |
|
| 24 |
export type Lang = 'en' | 'th'
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
export interface LetterboxMeta {
|
| 27 |
scale: number
|
| 28 |
padX: number
|
|
|
|
| 23 |
|
| 24 |
export type Lang = 'en' | 'th'
|
| 25 |
|
| 26 |
+
/** Where pipe detection runs: server (default) or in-browser ONNX. */
|
| 27 |
+
export type InferenceMode = 'server' | 'local'
|
| 28 |
+
|
| 29 |
export interface LetterboxMeta {
|
| 30 |
scale: number
|
| 31 |
padX: number
|