2025-12-15 22:06:04 -05:00
|
|
|
import { CancellableTask } from '$lib/utils/cancellable-task';
|
2025-11-19 09:52:40 -06:00
|
|
|
import { getAssetOcr } from '@immich/sdk';
|
|
|
|
|
|
|
|
|
|
export type OcrBoundingBox = {
|
|
|
|
|
id: string;
|
|
|
|
|
assetId: string;
|
|
|
|
|
x1: number;
|
|
|
|
|
y1: number;
|
|
|
|
|
x2: number;
|
|
|
|
|
y2: number;
|
|
|
|
|
x3: number;
|
|
|
|
|
y3: number;
|
|
|
|
|
x4: number;
|
|
|
|
|
y4: number;
|
|
|
|
|
boxScore: number;
|
|
|
|
|
textScore: number;
|
|
|
|
|
text: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class OcrManager {
|
|
|
|
|
#data = $state<OcrBoundingBox[]>([]);
|
|
|
|
|
showOverlay = $state(false);
|
2025-11-25 00:57:46 +01:00
|
|
|
#hasOcrData = $derived(this.#data.length > 0);
|
2025-12-15 22:06:04 -05:00
|
|
|
#ocrLoader = new CancellableTask();
|
|
|
|
|
#cleared = false;
|
2025-11-19 09:52:40 -06:00
|
|
|
|
|
|
|
|
get data() {
|
|
|
|
|
return this.#data;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 00:57:46 +01:00
|
|
|
get hasOcrData() {
|
|
|
|
|
return this.#hasOcrData;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 09:52:40 -06:00
|
|
|
async getAssetOcr(id: string) {
|
2025-12-15 22:06:04 -05:00
|
|
|
if (this.#cleared) {
|
|
|
|
|
await this.#ocrLoader.reset();
|
|
|
|
|
this.#cleared = false;
|
|
|
|
|
}
|
|
|
|
|
await this.#ocrLoader.execute(async () => {
|
|
|
|
|
this.#data = await getAssetOcr({ id });
|
|
|
|
|
}, false);
|
2025-11-19 09:52:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
2025-12-15 22:06:04 -05:00
|
|
|
this.#cleared = true;
|
2025-11-19 09:52:40 -06:00
|
|
|
this.#data = [];
|
|
|
|
|
this.showOverlay = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggleOcrBoundingBox() {
|
|
|
|
|
this.showOverlay = !this.showOverlay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ocrManager = new OcrManager();
|