Skip to content

Commit

Permalink
fix metadata face handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mertalev committed Oct 3, 2024
1 parent 96923f3 commit 15d1b5b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
34 changes: 15 additions & 19 deletions server/src/repositories/person.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,30 +303,26 @@ export class PersonRepository implements IPersonRepository {
faceIdsToRemove: string[],
embeddingsToAdd?: FaceSearchEntity[],
): Promise<void> {
const faceDeleteQuery = this.assetFaceRepository
.createQueryBuilder()
.delete()
.where('id = any(:faceIdsToRemove)', { faceIdsToRemove });
const query = this.faceSearchRepository.createQueryBuilder().select('1');
if (facesToAdd.length > 0) {
const insertCte = this.assetFaceRepository.createQueryBuilder().insert().values(facesToAdd);
query.addCommonTableExpression(insertCte, 'added');
}

if (facesToAdd.length === 0) {
await faceDeleteQuery.execute();
return;
if (faceIdsToRemove.length > 0) {
const deleteCte = this.assetFaceRepository
.createQueryBuilder()
.delete()
.where('id = any(:faceIdsToRemove)', { faceIdsToRemove });
query.addCommonTableExpression(deleteCte, 'deleted');
}

const faceInsertQuery = this.assetFaceRepository.createQueryBuilder().insert().values(facesToAdd);
if (!embeddingsToAdd || embeddingsToAdd.length === 0) {
await faceInsertQuery.addCommonTableExpression(faceDeleteQuery, 'deleted').execute();
return;
if (embeddingsToAdd?.length) {
const embeddingCte = this.faceSearchRepository.createQueryBuilder().insert().values(embeddingsToAdd).orIgnore();
query.addCommonTableExpression(embeddingCte, 'embeddings');
}

await this.faceSearchRepository
.createQueryBuilder()
.addCommonTableExpression(faceDeleteQuery, 'deleted')
.addCommonTableExpression(faceInsertQuery, 'added')
.insert()
.values(embeddingsToAdd)
.orIgnore()
.execute();
await query.execute();
}

async update(person: Partial<PersonEntity>): Promise<PersonEntity> {
Expand Down
11 changes: 6 additions & 5 deletions server/src/services/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export class PersonService extends BaseService {
}
const faceIdsToRemove = [...facesToRemove.values()].map((face) => face.id);

if (facesToAdd.length > 0 || faceIdsToRemove.length > 0) {
if (facesToAdd.length > 0 || faceIdsToRemove.length > 0 || embeddingsToAdd.length > 0) {
await this.personRepository.refreshFaces(facesToAdd, faceIdsToRemove, embeddingsToAdd);
}

Expand All @@ -367,10 +367,11 @@ export class PersonService extends BaseService {
]);
}

await this.assetRepository.upsertJobStatus({
assetId: asset.id,
facesRecognizedAt: new Date(),
});
if (embeddingsToAdd.length > 0) {
this.logger.log(`Added ${embeddingsToAdd.length} face embeddings for asset ${id}`);
}

await this.assetRepository.upsertJobStatus({ assetId: asset.id, facesRecognizedAt: new Date() });

return JobStatus.SUCCESS;
}
Expand Down

0 comments on commit 15d1b5b

Please sign in to comment.