mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-02 02:09:03 +08:00
* refactor(ocr): streamline OCR service registration and improve image preprocessing - Simplified the registration of the system OCR service by removing the conditional check for Linux. - Updated SystemOcrService to directly import necessary modules, enhancing clarity. - Refactored image preprocessing to use a static import of the 'sharp' library for better performance. * add patch for system-ocr * add patch * add patch again * add patch * delete setting * delete i18n * lint error * add isLinux * Revert "delete i18n" This reverts commit173e65bbd0. * Revert "delete setting" This reverts commitde39c76f83. * fix: add system check for error message --------- Co-authored-by: icarus <eurfelux@gmail.com>
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { isLinux, isWin } from '@main/constant'
|
|
import { loadOcrImage } from '@main/utils/ocr'
|
|
import { OcrAccuracy, recognize } from '@napi-rs/system-ocr'
|
|
import {
|
|
ImageFileMetadata,
|
|
isImageFileMetadata as isImageFileMetadata,
|
|
OcrResult,
|
|
OcrSystemConfig,
|
|
SupportedOcrFile
|
|
} from '@types'
|
|
|
|
import { OcrBaseService } from './OcrBaseService'
|
|
|
|
// const logger = loggerService.withContext('SystemOcrService')
|
|
export class SystemOcrService extends OcrBaseService {
|
|
constructor() {
|
|
super()
|
|
}
|
|
|
|
private async ocrImage(file: ImageFileMetadata, options?: OcrSystemConfig): Promise<OcrResult> {
|
|
if (isLinux) {
|
|
return { text: '' }
|
|
}
|
|
const buffer = await loadOcrImage(file)
|
|
const langs = isWin ? options?.langs : undefined
|
|
const result = await recognize(buffer, OcrAccuracy.Accurate, langs)
|
|
return { text: result.text }
|
|
}
|
|
|
|
public ocr = async (file: SupportedOcrFile, options?: OcrSystemConfig): Promise<OcrResult> => {
|
|
if (isImageFileMetadata(file)) {
|
|
return this.ocrImage(file, options)
|
|
} else {
|
|
throw new Error('Unsupported file type, currently only image files are supported')
|
|
}
|
|
}
|
|
}
|
|
|
|
export const systemOcrService = new SystemOcrService()
|