2023-02-25 15:12:03 +01:00
|
|
|
import { MACHINE_LEARNING_ENABLED } from '@app/common';
|
|
|
|
import { Inject, Injectable, Logger } from '@nestjs/common';
|
2023-03-20 16:55:28 +01:00
|
|
|
import { IAssetRepository, WithoutProperty } from '../asset';
|
|
|
|
import { IAssetJob, IBaseJob, IJobRepository, JobName } from '../job';
|
2023-02-25 15:12:03 +01:00
|
|
|
import { IMachineLearningRepository } from './machine-learning.interface';
|
|
|
|
import { ISmartInfoRepository } from './smart-info.repository';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SmartInfoService {
|
|
|
|
private logger = new Logger(SmartInfoService.name);
|
|
|
|
|
|
|
|
constructor(
|
2023-03-20 16:55:28 +01:00
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
2023-03-18 14:44:42 +01:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-02-25 15:12:03 +01:00
|
|
|
@Inject(ISmartInfoRepository) private repository: ISmartInfoRepository,
|
|
|
|
@Inject(IMachineLearningRepository) private machineLearning: IMachineLearningRepository,
|
|
|
|
) {}
|
|
|
|
|
2023-03-20 16:55:28 +01:00
|
|
|
async handleQueueObjectTagging({ force }: IBaseJob) {
|
|
|
|
try {
|
|
|
|
const assets = force
|
|
|
|
? await this.assetRepository.getAll()
|
|
|
|
: await this.assetRepository.getWithout(WithoutProperty.OBJECT_TAGS);
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.CLASSIFY_IMAGE, data: { asset } });
|
|
|
|
await this.jobRepository.queue({ name: JobName.DETECT_OBJECTS, data: { asset } });
|
|
|
|
}
|
|
|
|
} catch (error: any) {
|
|
|
|
this.logger.error(`Unable to queue object tagging`, error?.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDetectObjects(data: IAssetJob) {
|
2023-02-25 15:12:03 +01:00
|
|
|
const { asset } = data;
|
|
|
|
|
|
|
|
if (!MACHINE_LEARNING_ENABLED || !asset.resizePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-03-20 16:55:28 +01:00
|
|
|
const objects = await this.machineLearning.detectObjects({ thumbnailPath: asset.resizePath });
|
|
|
|
if (objects.length > 0) {
|
|
|
|
await this.repository.upsert({ assetId: asset.id, objects });
|
2023-03-18 14:44:42 +01:00
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ASSET, data: { ids: [asset.id] } });
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
} catch (error: any) {
|
2023-03-20 16:55:28 +01:00
|
|
|
this.logger.error(`Unable run object detection pipeline: ${asset.id}`, error?.stack);
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:55:28 +01:00
|
|
|
async handleClassifyImage(data: IAssetJob) {
|
2023-02-25 15:12:03 +01:00
|
|
|
const { asset } = data;
|
|
|
|
|
|
|
|
if (!MACHINE_LEARNING_ENABLED || !asset.resizePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-03-20 16:55:28 +01:00
|
|
|
const tags = await this.machineLearning.classifyImage({ thumbnailPath: asset.resizePath });
|
|
|
|
if (tags.length > 0) {
|
|
|
|
await this.repository.upsert({ assetId: asset.id, tags });
|
2023-03-18 14:44:42 +01:00
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ASSET, data: { ids: [asset.id] } });
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
} catch (error: any) {
|
2023-03-20 16:55:28 +01:00
|
|
|
this.logger.error(`Unable to run image tagging pipeline: ${asset.id}`, error?.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleQueueEncodeClip({ force }: IBaseJob) {
|
|
|
|
try {
|
|
|
|
const assets = force
|
|
|
|
? await this.assetRepository.getAll()
|
|
|
|
: await this.assetRepository.getWithout(WithoutProperty.CLIP_ENCODING);
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.ENCODE_CLIP, data: { asset } });
|
|
|
|
}
|
|
|
|
} catch (error: any) {
|
|
|
|
this.logger.error(`Unable to queue clip encoding`, error?.stack);
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-18 14:44:42 +01:00
|
|
|
|
|
|
|
async handleEncodeClip(data: IAssetJob) {
|
|
|
|
const { asset } = data;
|
|
|
|
|
|
|
|
if (!MACHINE_LEARNING_ENABLED || !asset.resizePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const clipEmbedding = await this.machineLearning.encodeImage({ thumbnailPath: asset.resizePath });
|
|
|
|
await this.repository.upsert({ assetId: asset.id, clipEmbedding: clipEmbedding });
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ASSET, data: { ids: [asset.id] } });
|
|
|
|
} catch (error: any) {
|
|
|
|
this.logger.error(`Unable run clip encoding pipeline: ${asset.id}`, error?.stack);
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|