Spaces:
Runtime error
Runtime error
Khunndev Sisyphus commited on
Commit ·
cd8f1ba
1
Parent(s): cac4938
Drop the lossy 2000px resize now that parallel tiling and O(n) post-processing handle speed.
Browse filesUltraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
- frontend/src/App.tsx +4 -25
- frontend/src/lib/resizeImage.ts +0 -80
frontend/src/App.tsx
CHANGED
|
@@ -8,7 +8,6 @@ import { loadShowConfidenceLabels, saveShowConfidenceLabels } from './lib/debugS
|
|
| 8 |
import { getServerBackend, isServerTimeoutError, probeServerDetect, serverDetect } from './lib/serverDetect'
|
| 9 |
import { t } from './lib/i18n'
|
| 10 |
import { applyCrop, type CropRect } from './lib/cropImage'
|
| 11 |
-
import { resizeImageIfNeeded } from './lib/resizeImage'
|
| 12 |
import type { Detection, DiameterGroup, InferenceMode, Lang } from './lib/types'
|
| 13 |
import { StepCapture } from './components/StepCapture'
|
| 14 |
import { StepCrop } from './components/StepCrop'
|
|
@@ -163,32 +162,12 @@ export default function App() {
|
|
| 163 |
const loadFile = (file: File) => {
|
| 164 |
const url = URL.createObjectURL(file)
|
| 165 |
const img = new Image()
|
| 166 |
-
img.onload =
|
| 167 |
revokeIfBlob(sourceSrc)
|
| 168 |
revokeIfBlob(imageSrc)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
// path; falls back to the original on any failure so the user is never
|
| 173 |
-
// blocked.
|
| 174 |
-
let sourceImg: HTMLImageElement = img
|
| 175 |
-
let sourceUrl: string = url
|
| 176 |
-
let sourceFl: File = file
|
| 177 |
-
try {
|
| 178 |
-
const resized = await resizeImageIfNeeded(img, file)
|
| 179 |
-
if (resized) {
|
| 180 |
-
URL.revokeObjectURL(url)
|
| 181 |
-
sourceImg = resized.image
|
| 182 |
-
sourceUrl = resized.url
|
| 183 |
-
sourceFl = resized.file
|
| 184 |
-
}
|
| 185 |
-
} catch (err) {
|
| 186 |
-
console.warn('[resize] failed, using original image', err)
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
setSourceImage(sourceImg)
|
| 190 |
-
setSourceSrc(sourceUrl)
|
| 191 |
-
setSourceFile(sourceFl)
|
| 192 |
setImage(null)
|
| 193 |
setImageFile(null)
|
| 194 |
setImageSrc('')
|
|
|
|
| 8 |
import { getServerBackend, isServerTimeoutError, probeServerDetect, serverDetect } from './lib/serverDetect'
|
| 9 |
import { t } from './lib/i18n'
|
| 10 |
import { applyCrop, type CropRect } from './lib/cropImage'
|
|
|
|
| 11 |
import type { Detection, DiameterGroup, InferenceMode, Lang } from './lib/types'
|
| 12 |
import { StepCapture } from './components/StepCapture'
|
| 13 |
import { StepCrop } from './components/StepCrop'
|
|
|
|
| 162 |
const loadFile = (file: File) => {
|
| 163 |
const url = URL.createObjectURL(file)
|
| 164 |
const img = new Image()
|
| 165 |
+
img.onload = () => {
|
| 166 |
revokeIfBlob(sourceSrc)
|
| 167 |
revokeIfBlob(imageSrc)
|
| 168 |
+
setSourceImage(img)
|
| 169 |
+
setSourceSrc(url)
|
| 170 |
+
setSourceFile(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
setImage(null)
|
| 172 |
setImageFile(null)
|
| 173 |
setImageSrc('')
|
frontend/src/lib/resizeImage.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
/**
|
| 2 |
-
* Cap the long side of an uploaded photo before it enters the pipeline.
|
| 3 |
-
*
|
| 4 |
-
* The detection model only ingests 1024×1024 tiles, so megapixel phone photos
|
| 5 |
-
* (4000px+) waste effort: they trigger up to 5×5 tiling (25 inference passes)
|
| 6 |
-
* and bloat server uploads. Resizing to a sensible cap cuts tile count and
|
| 7 |
-
* transfer size while keeping more than enough detail for pipe-end detection,
|
| 8 |
-
* because each tile is still letterboxed up to 1024 anyway.
|
| 9 |
-
*
|
| 10 |
-
* Uses the Canvas 2D API with high-quality smoothing — same approach as
|
| 11 |
-
* cropImage.ts / preprocess.ts, so no new dependency is introduced.
|
| 12 |
-
*/
|
| 13 |
-
|
| 14 |
-
export const MAX_SOURCE_DIMENSION = 2000
|
| 15 |
-
|
| 16 |
-
export interface ResizedImage {
|
| 17 |
-
image: HTMLImageElement
|
| 18 |
-
file: File
|
| 19 |
-
url: string
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
/**
|
| 23 |
-
* Downscale `image` so its longest side is <= `maxDimension`, preserving aspect
|
| 24 |
-
* ratio. Re-encodes as JPEG (0.92) — this also normalises HEIC/PNG/etc. into a
|
| 25 |
-
* format every downstream step already handles.
|
| 26 |
-
*
|
| 27 |
-
* Returns `null` when the image is already within the cap (caller should keep
|
| 28 |
-
* the original). Throws if the canvas can't encode the result.
|
| 29 |
-
*/
|
| 30 |
-
export async function resizeImageIfNeeded(
|
| 31 |
-
image: HTMLImageElement,
|
| 32 |
-
originalFile: File,
|
| 33 |
-
maxDimension: number = MAX_SOURCE_DIMENSION,
|
| 34 |
-
): Promise<ResizedImage | null> {
|
| 35 |
-
const w = image.naturalWidth
|
| 36 |
-
const h = image.naturalHeight
|
| 37 |
-
const longest = Math.max(w, h)
|
| 38 |
-
if (longest <= maxDimension || w <= 0 || h <= 0) {
|
| 39 |
-
return null
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
const scale = maxDimension / longest
|
| 43 |
-
const newW = Math.max(1, Math.round(w * scale))
|
| 44 |
-
const newH = Math.max(1, Math.round(h * scale))
|
| 45 |
-
|
| 46 |
-
const canvas = document.createElement('canvas')
|
| 47 |
-
canvas.width = newW
|
| 48 |
-
canvas.height = newH
|
| 49 |
-
const ctx = canvas.getContext('2d')
|
| 50 |
-
if (!ctx) throw new Error('Canvas not supported')
|
| 51 |
-
|
| 52 |
-
// High-quality bilinear/bicubic downsample — preserves edges the detector
|
| 53 |
-
// relies on, matching the "don't lose sharpness" requirement.
|
| 54 |
-
ctx.imageSmoothingEnabled = true
|
| 55 |
-
ctx.imageSmoothingQuality = 'high'
|
| 56 |
-
ctx.drawImage(image, 0, 0, newW, newH)
|
| 57 |
-
|
| 58 |
-
const blob = await new Promise<Blob>((resolve, reject) => {
|
| 59 |
-
canvas.toBlob(
|
| 60 |
-
(b) => (b ? resolve(b) : reject(new Error('Failed to encode resized image'))),
|
| 61 |
-
'image/jpeg',
|
| 62 |
-
0.92,
|
| 63 |
-
)
|
| 64 |
-
})
|
| 65 |
-
|
| 66 |
-
const baseName = (originalFile.name || 'photo').replace(/\.[^.]+$/, '')
|
| 67 |
-
const file = new File([blob], `${baseName}.jpg`, { type: 'image/jpeg' })
|
| 68 |
-
const url = URL.createObjectURL(blob)
|
| 69 |
-
const resizedImage = await new Promise<HTMLImageElement>((resolve, reject) => {
|
| 70 |
-
const img = new Image()
|
| 71 |
-
img.onload = () => resolve(img)
|
| 72 |
-
img.onerror = () => {
|
| 73 |
-
URL.revokeObjectURL(url)
|
| 74 |
-
reject(new Error('Failed to load resized image'))
|
| 75 |
-
}
|
| 76 |
-
img.src = url
|
| 77 |
-
})
|
| 78 |
-
|
| 79 |
-
return { image: resizedImage, file, url }
|
| 80 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|