mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
02b29046b3
* feat: add OCR functionality and related configurations * chore: update labeler configuration for machine learning files * feat(i18n): enhance OCR model descriptions and add orientation classification and unwarping features * chore: update Dockerfile to include ccache for improved build performance * feat(ocr): enhance OCR model configuration with orientation classification and unwarping options, update PaddleOCR integration, and improve response structure * refactor(ocr): remove OCR_CLEANUP job from enum and type definitions * refactor(ocr): remove obsolete OCR entity and migration files, and update asset job status and schema to accommodate new OCR table structure * refactor(ocr): update OCR schema and response structure to use individual coordinates instead of bounding box, and adjust related service and repository files * feat: enhance OCR configuration and functionality - Updated OCR settings to include minimum detection box score, minimum detection score, and minimum recognition score. - Refactored PaddleOCRecognizer to utilize new scoring parameters. - Introduced new database tables for asset OCR data and search functionality. - Modified related services and repositories to support the new OCR features. - Updated translations for improved clarity in settings UI. * sql changes * use rapidocr * change dto * update web * update lock * update api * store positions as normalized floats * match column order in db * update admin ui settings descriptions fix max resolution key set min threshold to 0.1 fix bind * apply config correctly, adjust defaults * unnecessary model type * unnecessary sources * fix(ocr): switch RapidOCR lang type from LangDet to LangRec * fix(ocr): expose lang_type (LangRec.CH) and font_path on OcrOptions for RapidOCR * fix(ocr): make OCR text search case- and accent-insensitive using ILIKE + unaccent * fix(ocr): add OCR search fields * fix: Add OCR database migration and update ML prediction logic. * trigrams are already case insensitive * add tests * format * update migrations * wrong uuid function * linting * maybe fix medium tests * formatting * fix weblate check * openapi * sql * minor fixes * maybe fix medium tests part 2 * passing medium tests * format web * readd sql * format dart * disabled in e2e * chore: translation ordering --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
178 lines
5.9 KiB
TypeScript
178 lines
5.9 KiB
TypeScript
import { AssetVisibility, ImmichWorker, JobName, JobStatus } from 'src/enum';
|
|
import { OcrService } from 'src/services/ocr.service';
|
|
import { assetStub } from 'test/fixtures/asset.stub';
|
|
import { systemConfigStub } from 'test/fixtures/system-config.stub';
|
|
import { makeStream, newTestService, ServiceMocks } from 'test/utils';
|
|
|
|
describe(OcrService.name, () => {
|
|
let sut: OcrService;
|
|
let mocks: ServiceMocks;
|
|
|
|
beforeEach(() => {
|
|
({ sut, mocks } = newTestService(OcrService));
|
|
|
|
mocks.config.getWorker.mockReturnValue(ImmichWorker.Microservices);
|
|
});
|
|
|
|
it('should work', () => {
|
|
expect(sut).toBeDefined();
|
|
});
|
|
|
|
describe('handleQueueOcr', () => {
|
|
it('should do nothing if machine learning is disabled', async () => {
|
|
mocks.systemMetadata.get.mockResolvedValue(systemConfigStub.machineLearningDisabled);
|
|
|
|
await sut.handleQueueOcr({ force: false });
|
|
|
|
expect(mocks.database.setDimensionSize).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should queue the assets without ocr', async () => {
|
|
mocks.assetJob.streamForOcrJob.mockReturnValue(makeStream([assetStub.image]));
|
|
|
|
await sut.handleQueueOcr({ force: false });
|
|
|
|
expect(mocks.job.queueAll).toHaveBeenCalledWith([{ name: JobName.Ocr, data: { id: assetStub.image.id } }]);
|
|
expect(mocks.assetJob.streamForOcrJob).toHaveBeenCalledWith(false);
|
|
});
|
|
|
|
it('should queue all the assets', async () => {
|
|
mocks.assetJob.streamForOcrJob.mockReturnValue(makeStream([assetStub.image]));
|
|
|
|
await sut.handleQueueOcr({ force: true });
|
|
|
|
expect(mocks.job.queueAll).toHaveBeenCalledWith([{ name: JobName.Ocr, data: { id: assetStub.image.id } }]);
|
|
expect(mocks.assetJob.streamForOcrJob).toHaveBeenCalledWith(true);
|
|
});
|
|
});
|
|
|
|
describe('handleOcr', () => {
|
|
it('should do nothing if machine learning is disabled', async () => {
|
|
mocks.systemMetadata.get.mockResolvedValue(systemConfigStub.machineLearningDisabled);
|
|
|
|
expect(await sut.handleOcr({ id: '123' })).toEqual(JobStatus.Skipped);
|
|
|
|
expect(mocks.asset.getByIds).not.toHaveBeenCalled();
|
|
expect(mocks.machineLearning.encodeImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should skip assets without a resize path', async () => {
|
|
mocks.assetJob.getForOcr.mockResolvedValue({ visibility: AssetVisibility.Timeline, previewFile: null });
|
|
|
|
expect(await sut.handleOcr({ id: assetStub.noResizePath.id })).toEqual(JobStatus.Failed);
|
|
|
|
expect(mocks.ocr.upsert).not.toHaveBeenCalled();
|
|
expect(mocks.machineLearning.ocr).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should save the returned objects', async () => {
|
|
mocks.machineLearning.ocr.mockResolvedValue({
|
|
box: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160],
|
|
boxScore: [0.9, 0.8],
|
|
text: ['One Two Three', 'Four Five'],
|
|
textScore: [0.95, 0.85],
|
|
});
|
|
mocks.assetJob.getForOcr.mockResolvedValue({
|
|
visibility: AssetVisibility.Timeline,
|
|
previewFile: assetStub.image.files[1].path,
|
|
});
|
|
|
|
expect(await sut.handleOcr({ id: assetStub.image.id })).toEqual(JobStatus.Success);
|
|
|
|
expect(mocks.machineLearning.ocr).toHaveBeenCalledWith(
|
|
'/uploads/user-id/thumbs/path.jpg',
|
|
expect.objectContaining({
|
|
modelName: 'PP-OCRv5_mobile',
|
|
minDetectionScore: 0.5,
|
|
minRecognitionScore: 0.8,
|
|
maxResolution: 736,
|
|
}),
|
|
);
|
|
expect(mocks.ocr.upsert).toHaveBeenCalledWith(assetStub.image.id, [
|
|
{
|
|
assetId: assetStub.image.id,
|
|
boxScore: 0.9,
|
|
text: 'One Two Three',
|
|
textScore: 0.95,
|
|
x1: 10,
|
|
y1: 20,
|
|
x2: 30,
|
|
y2: 40,
|
|
x3: 50,
|
|
y3: 60,
|
|
x4: 70,
|
|
y4: 80,
|
|
},
|
|
{
|
|
assetId: assetStub.image.id,
|
|
boxScore: 0.8,
|
|
text: 'Four Five',
|
|
textScore: 0.85,
|
|
x1: 90,
|
|
y1: 100,
|
|
x2: 110,
|
|
y2: 120,
|
|
x3: 130,
|
|
y3: 140,
|
|
x4: 150,
|
|
y4: 160,
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('should apply config settings', async () => {
|
|
mocks.systemMetadata.get.mockResolvedValue({
|
|
machineLearning: {
|
|
enabled: true,
|
|
ocr: {
|
|
modelName: 'PP-OCRv5_server',
|
|
enabled: true,
|
|
minDetectionScore: 0.8,
|
|
minRecognitionScore: 0.9,
|
|
maxResolution: 1500,
|
|
},
|
|
},
|
|
});
|
|
mocks.machineLearning.ocr.mockResolvedValue({ box: [], boxScore: [], text: [], textScore: [] });
|
|
mocks.assetJob.getForOcr.mockResolvedValue({
|
|
visibility: AssetVisibility.Timeline,
|
|
previewFile: assetStub.image.files[1].path,
|
|
});
|
|
|
|
expect(await sut.handleOcr({ id: assetStub.image.id })).toEqual(JobStatus.Success);
|
|
|
|
expect(mocks.machineLearning.ocr).toHaveBeenCalledWith(
|
|
'/uploads/user-id/thumbs/path.jpg',
|
|
expect.objectContaining({
|
|
modelName: 'PP-OCRv5_server',
|
|
minDetectionScore: 0.8,
|
|
minRecognitionScore: 0.9,
|
|
maxResolution: 1500,
|
|
}),
|
|
);
|
|
expect(mocks.ocr.upsert).toHaveBeenCalledWith(assetStub.image.id, []);
|
|
});
|
|
|
|
it('should skip invisible assets', async () => {
|
|
mocks.assetJob.getForOcr.mockResolvedValue({
|
|
visibility: AssetVisibility.Hidden,
|
|
previewFile: assetStub.image.files[1].path,
|
|
});
|
|
|
|
expect(await sut.handleOcr({ id: assetStub.livePhotoMotionAsset.id })).toEqual(JobStatus.Skipped);
|
|
|
|
expect(mocks.machineLearning.ocr).not.toHaveBeenCalled();
|
|
expect(mocks.ocr.upsert).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should fail if asset could not be found', async () => {
|
|
mocks.assetJob.getForOcr.mockResolvedValue(void 0);
|
|
|
|
expect(await sut.handleOcr({ id: assetStub.image.id })).toEqual(JobStatus.Failed);
|
|
|
|
expect(mocks.machineLearning.ocr).not.toHaveBeenCalled();
|
|
expect(mocks.ocr.upsert).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|