refactor(ocr): 添加OCR图像预处理功能并优化TesseractService

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
icarus 2025-08-24 20:25:12 +08:00
parent 492f1e46ff
commit f88eb8b08e
3 changed files with 39 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import { loggerService } from '@logger'
import { getIpCountry } from '@main/utils/ipService'
import { loadOcrImage } from '@main/utils/ocr'
import { MB } from '@shared/config/constant'
import { ImageFileMetadata, isImageFile, OcrResult, SupportedOcrFile } from '@types'
import { app } from 'electron'
@ -39,7 +40,7 @@ export class TesseractService {
if (stat.size > MB_SIZE_THRESHOLD * MB) {
throw new Error(`This image is too large (max ${MB_SIZE_THRESHOLD}MB)`)
}
const buffer = await fs.promises.readFile(file.path)
const buffer = await loadOcrImage(file)
const result = await worker.recognize(buffer)
return { text: result.data.text }
}

11
src/main/utils/image.ts Normal file
View File

@ -0,0 +1,11 @@
import sharp from 'sharp'
/**
*
* @param image Buffer
* @returns Promise<Buffer> Buffer
* @throws {Error}
*/
export const greyScale = (image: Buffer): Promise<Buffer> => {
return sharp(image).greyscale().toBuffer()
}

26
src/main/utils/ocr.ts Normal file
View File

@ -0,0 +1,26 @@
import { ImageFileMetadata } from '@types'
import { readFile } from 'fs/promises'
import { greyScale } from './image'
const preprocessImage = (buffer: Buffer) => {
return greyScale(buffer)
}
/**
* OCR图像
* @param file -
* @returns Buffer
*
* :
* 1.
* 2.
* 3.
*/
export const loadOcrImage = async (file: ImageFileMetadata): Promise<Buffer> => {
// 读取原始图像
const buffer = await readFile(file.path)
// 统一预处理流程
return preprocessImage(buffer)
}