Warn on low-resolution image upload before extraction
Probes a staged bare image's own naturalWidth (fired, not awaited, so staging stays synchronous) and flags one below LOW_RESOLUTION_WIDTH_PX - deliberately the same threshold raw_ocr_extract._MIN_OCR_WIDTH_PX uses server-side to decide whether to upscale before OCR, kept in sync rather than two independently-chosen numbers. Inline warning under the staged file's name, not a blocking modal - both real choices (pick a different file, or proceed anyway) are already on screen. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
99dc3d8bb4
commit
206f78979b
|
|
@ -21,6 +21,15 @@ defineProps({
|
|||
isDragOver: { type: Boolean, default: false },
|
||||
isProcessing: { type: Boolean, required: true },
|
||||
phaseText: { type: String, required: true },
|
||||
// Billing/usage note aside - this is about EXTRACTION QUALITY, a
|
||||
// real reported case: a low-resolution photo/screenshot upload can
|
||||
// read so poorly via OCR that the result needs careful manual review
|
||||
// even after the backend's own upscaling correction (see CLAUDE.md's
|
||||
// "Two real bugs found investigating a specific real-world statement
|
||||
// report" entry). {[name:size]: {width, height}}, keyed the same way
|
||||
// chosenFiles' own dedup already is - see useFileUpload.js's own
|
||||
// lowResolutionWarnings docstring.
|
||||
lowResolutionWarnings: { type: Object, default: () => ({}) },
|
||||
})
|
||||
const emit = defineEmits(['file-input-change', 'drag-over', 'drag-leave', 'drop', 'remove-file', 'extract'])
|
||||
|
||||
|
|
@ -53,10 +62,29 @@ const extractionStore = useExtractionStore()
|
|||
pulled back out before Extract is ever clicked. -->
|
||||
<ul v-if="chosenFiles.length" class="staged-files">
|
||||
<li v-for="(f, i) in chosenFiles" :key="f.name + ':' + f.size">
|
||||
<span class="staged-file-name"><i class="fa-solid fa-file-lines"></i>{{ f.name }}</span>
|
||||
<button type="button" class="staged-file-remove" title="Remove this file" @click="emit('remove-file', i)">
|
||||
<i class="fa-solid fa-xmark"></i>
|
||||
</button>
|
||||
<div class="staged-file-row">
|
||||
<span class="staged-file-name"><i class="fa-solid fa-file-lines"></i>{{ f.name }}</span>
|
||||
<button type="button" class="staged-file-remove" title="Remove this file" @click="emit('remove-file', i)">
|
||||
<i class="fa-solid fa-xmark"></i>
|
||||
</button>
|
||||
</div>
|
||||
<!--
|
||||
A real reported case: a low-resolution photo/screenshot can
|
||||
read so poorly via OCR that the result needs careful manual
|
||||
review even after the backend's own automatic upscaling
|
||||
correction - surfaced here, at the moment the user can still
|
||||
easily pick a different file, rather than only discovered
|
||||
after a confusing/wrong Preview. Both of the choices the user
|
||||
actually has are already right here: the x above removes
|
||||
this file (pick a clearer one instead), or Extract below
|
||||
proceeds with it as-is, accepting the risk.
|
||||
-->
|
||||
<div v-if="lowResolutionWarnings[f.name + ':' + f.size]" class="staged-file-warning">
|
||||
<i class="fa-solid fa-triangle-exclamation"></i>
|
||||
Low resolution ({{ lowResolutionWarnings[f.name + ':' + f.size].width }}×{{ lowResolutionWarnings[f.name + ':' + f.size].height }}px) -
|
||||
the app will try to correct for this, but text recognition may still suffer and the result may need manual
|
||||
review/correction. Consider a clearer or higher-resolution photo instead, or continue if you accept the risk.
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
|
|
|
|||
|
|
@ -11,12 +11,56 @@ import { ref } from 'vue'
|
|||
import { useExtractionStore } from '../stores/extractionStore'
|
||||
|
||||
const ACCEPTED_FILE_RE = /\.(pdf|jpe?g|png)$/i
|
||||
const IMAGE_FILE_RE = /\.(jpe?g|png)$/i
|
||||
// Mirrors the backend's own raw_ocr_extract._MIN_OCR_WIDTH_PX exactly -
|
||||
// that's the width below which the backend already upscales before OCR
|
||||
// (see CLAUDE.md's "Two real bugs found investigating a specific real-
|
||||
// world statement report" entry) - this is the matching FRONTEND-side
|
||||
// warning threshold, surfaced at upload time rather than only
|
||||
// discovered after a Preview/extraction comes back wrong. A PDF isn't
|
||||
// checked at all - a scanned PDF page is rendered server-side at a
|
||||
// fixed resolution regardless of whatever the original scan's own
|
||||
// resolution was, so there's nothing meaningful to read client-side
|
||||
// for it the way a bare image's own naturalWidth directly is one.
|
||||
const LOW_RESOLUTION_WIDTH_PX = 1600
|
||||
|
||||
/**
|
||||
* The real natural pixel width of an image file, or null if it
|
||||
* couldn't be read (never rejects - a failed probe just means no
|
||||
* warning is shown, not that the file is blocked).
|
||||
*/
|
||||
function probeImageWidth(file) {
|
||||
return new Promise((resolve) => {
|
||||
const url = URL.createObjectURL(file)
|
||||
const img = new Image()
|
||||
img.onload = () => {
|
||||
resolve({ width: img.naturalWidth, height: img.naturalHeight })
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
img.onerror = () => {
|
||||
resolve(null)
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
img.src = url
|
||||
})
|
||||
}
|
||||
|
||||
export function useFileUpload() {
|
||||
const extractionStore = useExtractionStore()
|
||||
|
||||
const chosenFiles = ref([])
|
||||
const isDragOver = ref(false)
|
||||
/**
|
||||
* {["name:size"]: {width, height}} for every staged image found to be
|
||||
* below LOW_RESOLUTION_WIDTH_PX - keyed the same way chosenFiles'
|
||||
* own dedup already is, so FileUploadZone.vue can look a warning up
|
||||
* for a given staged file with no extra bookkeeping. Populated
|
||||
* asynchronously (probing an image's real dimensions takes a load),
|
||||
* so a file can briefly show with no warning before one appears a
|
||||
* moment later - an honest reflection of when the check actually
|
||||
* resolves, not worth blocking the staged-file display on.
|
||||
*/
|
||||
const lowResolutionWarnings = ref({})
|
||||
|
||||
/**
|
||||
* Validates and ADDS files to whatever's already staged for the next
|
||||
|
|
@ -47,18 +91,36 @@ export function useFileUpload() {
|
|||
if (!staged.has(key)) {
|
||||
chosenFiles.value.push(f)
|
||||
staged.add(key)
|
||||
// Fire-and-forget - never awaited here, so staging itself stays
|
||||
// synchronous and instant; the warning (if any) just appears a
|
||||
// moment later once the probe resolves. Only a bare image is
|
||||
// checked at all (see LOW_RESOLUTION_WIDTH_PX's own comment for
|
||||
// why a PDF is skipped).
|
||||
if (IMAGE_FILE_RE.test(f.name)) {
|
||||
probeImageWidth(f).then((dims) => {
|
||||
if (dims && dims.width < LOW_RESOLUTION_WIDTH_PX) {
|
||||
lowResolutionWarnings.value = { ...lowResolutionWarnings.value, [key]: dims }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes one staged file (by its position in chosenFiles) before it's ever uploaded. */
|
||||
function removeChosenFile(index) {
|
||||
const f = chosenFiles.value[index]
|
||||
chosenFiles.value.splice(index, 1)
|
||||
if (f) {
|
||||
const { [f.name + ':' + f.size]: _removed, ...rest } = lowResolutionWarnings.value
|
||||
lowResolutionWarnings.value = rest
|
||||
}
|
||||
}
|
||||
|
||||
/** Empties the staging list - called once a batch has been handed off to extraction, so those files aren't left sitting there to be accidentally re-extracted (and duplicated) by a later click. */
|
||||
function clearChosenFiles() {
|
||||
chosenFiles.value = []
|
||||
lowResolutionWarnings.value = {}
|
||||
}
|
||||
|
||||
function onFileInputChange(e) {
|
||||
|
|
@ -85,7 +147,7 @@ export function useFileUpload() {
|
|||
}
|
||||
|
||||
return {
|
||||
chosenFiles, isDragOver,
|
||||
chosenFiles, isDragOver, lowResolutionWarnings,
|
||||
setFiles, removeChosenFile, clearChosenFiles, onFileInputChange, onDragOver, onDragLeave, onDrop,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue