mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-29 23:12:38 +08:00
refactor(ocr): 添加OCR图像预处理功能并优化TesseractService
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
492f1e46ff
commit
f88eb8b08e
@ -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
11
src/main/utils/image.ts
Normal 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
26
src/main/utils/ocr.ts
Normal 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)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user