Skip to content

Commit daf4f01

Browse files
committed
feat: support black text white background
1 parent 50185fd commit daf4f01

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

app/composables/workers/imageProcessor.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readImg, Image, readCanvas } from 'image-js'
1+
import { Image, readCanvas } from 'image-js'
22

33
const UNIMERNET_MEAN = 0.7931
44
const UNIMERNET_STD = 0.1738
@@ -16,15 +16,28 @@ export async function preprocessImg(file: File) {
1616
// readCanvas actually also accepts an OffscreenCanvas, it's simply a matter of type annotation
1717
let image = readCanvas(canvas as unknown as HTMLCanvasElement)
1818
// console.log(image)
19+
image = image.grey()
20+
image = reverseColor(image)
1921
image = cropMargin(image)
2022
image = resize(image, 384, 384)
2123
const array = normalize(image)
2224
return { image, array }
2325
}
2426

25-
function cropMargin(input: Image): Image {
26-
const image = input.grey()
27-
// return grey
27+
// support black background white text, detect by heuristic
28+
function reverseColor(image: Image): Image {
29+
const histogram = image.histogram()
30+
const threshold = 200
31+
const black_pixels = histogram.slice(0, threshold).reduce((sum, v) => sum + v, 0)
32+
const white_pixels = histogram.slice(threshold).reduce((sum, v) => sum + v, 0)
33+
if (black_pixels >= white_pixels) {
34+
console.log('black text white background detected, reverse color')
35+
return image.invert()
36+
}
37+
return image
38+
}
39+
40+
function cropMargin(image: Image): Image {
2841
const data = image.getRawImage().data
2942

3043
let max = -Infinity, min = Infinity

i18n/locales/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"model_title": "Model",
99
"upload_title": "Upload",
1010
"uploader_label": "Paste, drop or click to upload your LaTeX image",
11-
"uploader_description": "Resolution no less than 384×384\nBlack text, white background, minimal margin\nExtreme height-width ratio may lead to illusion",
11+
"uploader_description": "Resolution no less than 384×384\nminimal margin\nExtreme height-width ratio may lead to illusion",
1212
"upload_button": "Click to upload",
1313
"upload_example": "Example image",
1414
"preview_title": "Preview",

i18n/locales/zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"model_title": "模型",
99
"upload_title": "上传",
1010
"uploader_label": "粘贴、拖拽或点击上传公式图片",
11-
"uploader_description": "分辨率不小于384×384\n白底黑字,白边尽可能少\n长宽比过于悬殊可能使模型产生幻觉",
11+
"uploader_description": "分辨率不小于384×384\n边缘尽可能少\n长宽比过于悬殊可能使模型产生幻觉",
1212
"upload_button": "点击上传",
1313
"upload_example": "示例图片",
1414
"preview_title": "预览",

0 commit comments

Comments
 (0)