mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 14:41:24 +08:00
feat(ocr): enhance OCR service for Linux compatibility and update image processing
- Added conditional registration of the system OCR service for non-Linux platforms. - Updated SystemOcrService to handle Linux by returning an empty text response. - Modified image preprocessing to dynamically require the 'sharp' library, improving compatibility and performance. - Included additional files in the electron-builder configuration for packaging.
This commit is contained in:
parent
5ab98c9d05
commit
80f49aecd7
@ -62,6 +62,7 @@ files:
|
|||||||
asarUnpack:
|
asarUnpack:
|
||||||
- resources/**
|
- resources/**
|
||||||
- '**/*.{metal,exp,lib}'
|
- '**/*.{metal,exp,lib}'
|
||||||
|
- 'node_modules/@img/sharp-libvips-*/**'
|
||||||
win:
|
win:
|
||||||
executableName: Cherry Studio
|
executableName: Cherry Studio
|
||||||
artifactName: ${productName}-${version}-${arch}-setup.${ext}
|
artifactName: ${productName}-${version}-${arch}-setup.${ext}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { loggerService } from '@logger'
|
import { loggerService } from '@logger'
|
||||||
|
import { isLinux } from '@main/constant'
|
||||||
import { BuiltinOcrProviderIds, OcrHandler, OcrProvider, OcrResult, SupportedOcrFile } from '@types'
|
import { BuiltinOcrProviderIds, OcrHandler, OcrProvider, OcrResult, SupportedOcrFile } from '@types'
|
||||||
|
|
||||||
import { systemOcrService } from './builtin/SystemOcrService'
|
|
||||||
import { tesseractService } from './builtin/TesseractService'
|
import { tesseractService } from './builtin/TesseractService'
|
||||||
|
|
||||||
const logger = loggerService.withContext('OcrService')
|
const logger = loggerService.withContext('OcrService')
|
||||||
@ -33,4 +33,8 @@ export const ocrService = new OcrService()
|
|||||||
|
|
||||||
// Register built-in providers
|
// Register built-in providers
|
||||||
ocrService.register(BuiltinOcrProviderIds.tesseract, tesseractService.ocr.bind(tesseractService))
|
ocrService.register(BuiltinOcrProviderIds.tesseract, tesseractService.ocr.bind(tesseractService))
|
||||||
ocrService.register(BuiltinOcrProviderIds.system, systemOcrService.ocr.bind(systemOcrService))
|
|
||||||
|
if (!isLinux) {
|
||||||
|
const { systemOcrService } = require('./builtin/SystemOcrService')
|
||||||
|
ocrService.register(BuiltinOcrProviderIds.system, systemOcrService.ocr.bind(systemOcrService))
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { isMac, isWin } from '@main/constant'
|
import { isLinux, isWin } from '@main/constant'
|
||||||
import { loadOcrImage } from '@main/utils/ocr'
|
import { loadOcrImage } from '@main/utils/ocr'
|
||||||
import { OcrAccuracy, recognize } from '@napi-rs/system-ocr'
|
|
||||||
import {
|
import {
|
||||||
ImageFileMetadata,
|
ImageFileMetadata,
|
||||||
isImageFileMetadata as isImageFileMetadata,
|
isImageFileMetadata as isImageFileMetadata,
|
||||||
@ -15,12 +14,14 @@ import { OcrBaseService } from './OcrBaseService'
|
|||||||
export class SystemOcrService extends OcrBaseService {
|
export class SystemOcrService extends OcrBaseService {
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
if (!isWin && !isMac) {
|
|
||||||
throw new Error('System OCR is only supported on Windows and macOS')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ocrImage(file: ImageFileMetadata, options?: OcrSystemConfig): Promise<OcrResult> {
|
private async ocrImage(file: ImageFileMetadata, options?: OcrSystemConfig): Promise<OcrResult> {
|
||||||
|
if (isLinux) {
|
||||||
|
return { text: '' }
|
||||||
|
}
|
||||||
|
|
||||||
|
const { OcrAccuracy, recognize } = require('@napi-rs/system-ocr')
|
||||||
const buffer = await loadOcrImage(file)
|
const buffer = await loadOcrImage(file)
|
||||||
const langs = isWin ? options?.langs : undefined
|
const langs = isWin ? options?.langs : undefined
|
||||||
const result = await recognize(buffer, OcrAccuracy.Accurate, langs)
|
const result = await recognize(buffer, OcrAccuracy.Accurate, langs)
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { ImageFileMetadata } from '@types'
|
import { ImageFileMetadata } from '@types'
|
||||||
import { readFile } from 'fs/promises'
|
import { readFile } from 'fs/promises'
|
||||||
import sharp from 'sharp'
|
|
||||||
|
|
||||||
const preprocessImage = async (buffer: Buffer): Promise<Buffer> => {
|
const preprocessImage = async (buffer: Buffer): Promise<Buffer> => {
|
||||||
|
const sharp = require('sharp')
|
||||||
return sharp(buffer)
|
return sharp(buffer)
|
||||||
.grayscale() // 转为灰度
|
.grayscale() // 转为灰度
|
||||||
.normalize()
|
.normalize()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user