Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): optimize partial facial recognition #6634

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/src/domain/job/job.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe(JobService.name, () => {
{ name: JobName.QUEUE_GENERATE_THUMBNAILS, data: { force: false } },
{ name: JobName.CLEAN_OLD_AUDIT_LOGS },
{ name: JobName.USER_SYNC_USAGE },
{ name: JobName.QUEUE_FACIAL_RECOGNITION, data: { force: false } },
]);
});
});
Expand Down Expand Up @@ -318,7 +319,7 @@ describe(JobService.name, () => {
},
{
item: { name: JobName.FACE_DETECTION, data: { id: 'asset-1' } },
jobs: [JobName.QUEUE_FACIAL_RECOGNITION],
jobs: [],
},
{
item: { name: JobName.FACIAL_RECOGNITION, data: { id: 'asset-1' } },
Expand Down
6 changes: 1 addition & 5 deletions server/src/domain/job/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class JobService {
{ name: JobName.QUEUE_GENERATE_THUMBNAILS, data: { force: false } },
{ name: JobName.CLEAN_OLD_AUDIT_LOGS },
{ name: JobName.USER_SYNC_USAGE },
{ name: JobName.QUEUE_FACIAL_RECOGNITION, data: { force: false } },
]);
}

Expand Down Expand Up @@ -255,11 +256,6 @@ export class JobService {
}
break;
}

case JobName.FACE_DETECTION: {
await this.jobRepository.queue({ name: JobName.QUEUE_FACIAL_RECOGNITION, data: item.data });
break;
}
}
}
}
29 changes: 23 additions & 6 deletions server/src/domain/person/person.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,23 @@ describe(PersonService.name, () => {

describe('handleQueueRecognizeFaces', () => {
it('should return if machine learning is disabled', async () => {
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);

await expect(sut.handleQueueRecognizeFaces({})).resolves.toBe(true);
expect(jobMock.queue).not.toHaveBeenCalled();
expect(jobMock.queueAll).not.toHaveBeenCalled();
expect(configMock.load).toHaveBeenCalled();
});

it('should return if recognition jobs are already queued', async () => {
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 1, paused: 0, completed: 0, failed: 0, delayed: 0 });

await expect(sut.handleQueueRecognizeFaces({})).resolves.toBe(true);
expect(jobMock.queueAll).not.toHaveBeenCalled();
});

it('should queue missing assets', async () => {
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
personMock.getAllFaces.mockResolvedValue({
items: [faceStub.face1],
hasNextPage: false,
Expand All @@ -632,6 +641,7 @@ describe(PersonService.name, () => {
});

it('should queue all assets', async () => {
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
personMock.getAll.mockResolvedValue({
items: [],
hasNextPage: false,
Expand All @@ -653,6 +663,7 @@ describe(PersonService.name, () => {
});

it('should delete existing people and faces if forced', async () => {
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
personMock.getAll.mockResolvedValue({
items: [faceStub.face1.person],
hasNextPage: false,
Expand Down Expand Up @@ -738,13 +749,12 @@ describe(PersonService.name, () => {
expect(assetMock.upsertJobStatus.mock.calls[0][0].facesRecognizedAt?.getTime()).toBeGreaterThan(start);
});

it('should create a face with no person', async () => {
it('should create a face with no person and queue recognition job', async () => {
personMock.createFace.mockResolvedValue(faceStub.face1.id);
machineLearningMock.detectFaces.mockResolvedValue([detectFaceMock]);
smartInfoMock.searchFaces.mockResolvedValue([{ face: faceStub.face1, distance: 0.7 }]);
assetMock.getByIds.mockResolvedValue([assetStub.image]);
await sut.handleDetectFaces({ id: assetStub.image.id });

expect(personMock.createFace).toHaveBeenCalledWith({
const face = {
assetId: 'asset-id',
embedding: [1, 2, 3, 4],
boundingBoxX1: 100,
Expand All @@ -753,7 +763,14 @@ describe(PersonService.name, () => {
boundingBoxY2: 200,
imageHeight: 500,
imageWidth: 400,
});
};

await sut.handleDetectFaces({ id: assetStub.image.id });

expect(personMock.createFace).toHaveBeenCalledWith(face);
expect(jobMock.queueAll).toHaveBeenCalledWith([
{ name: JobName.FACIAL_RECOGNITION, data: { id: faceStub.face1.id } },
]);
expect(personMock.reassignFace).not.toHaveBeenCalled();
expect(personMock.reassignFaces).not.toHaveBeenCalled();
});
Expand Down
37 changes: 25 additions & 12 deletions server/src/domain/person/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,26 @@ export class PersonService {
this.logger.debug(`${faces.length} faces detected in ${asset.resizePath}`);
this.logger.verbose(faces.map((face) => ({ ...face, embedding: `vector(${face.embedding.length})` })));

for (const face of faces) {
const mappedFace = {
assetId: asset.id,
embedding: face.embedding,
imageHeight: face.imageHeight,
imageWidth: face.imageWidth,
boundingBoxX1: face.boundingBox.x1,
boundingBoxX2: face.boundingBox.x2,
boundingBoxY1: face.boundingBox.y1,
boundingBoxY2: face.boundingBox.y2,
};
if (faces.length) {
await this.jobRepository.queue({ name: JobName.QUEUE_FACIAL_RECOGNITION, data: { force: false } });

const createFaces = faces.map((face) => {
const mappedFace = {
assetId: asset.id,
embedding: face.embedding,
imageHeight: face.imageHeight,
imageWidth: face.imageWidth,
boundingBoxX1: face.boundingBox.x1,
boundingBoxX2: face.boundingBox.x2,
boundingBoxY1: face.boundingBox.y1,
boundingBoxY2: face.boundingBox.y2,
};

return this.repository.createFace(mappedFace);
mertalev marked this conversation as resolved.
Show resolved Hide resolved
});

await this.repository.createFace(mappedFace);
const faceIds = await Promise.all(createFaces);
await this.jobRepository.queueAll(faceIds.map((id) => ({ name: JobName.FACIAL_RECOGNITION, data: { id } })));
}

await this.assetRepository.upsertJobStatus({
Expand All @@ -362,9 +369,15 @@ export class PersonService {
}

await this.jobRepository.waitForQueueCompletion(QueueName.THUMBNAIL_GENERATION, QueueName.FACE_DETECTION);
const { waiting } = await this.jobRepository.getJobCounts(QueueName.FACIAL_RECOGNITION);

if (force) {
await this.deleteAllPeople();
} else if (waiting) {
this.logger.debug(
`Skipping facial recognition queueing because ${waiting} job${waiting > 1 ? 's are' : ' is'} already queued`,
);
return true;
}

const facePagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
Expand Down
2 changes: 1 addition & 1 deletion server/src/domain/repositories/person.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface IPersonRepository {
getAssets(personId: string): Promise<AssetEntity[]>;

create(entity: Partial<PersonEntity>): Promise<PersonEntity>;
createFace(entity: Partial<AssetFaceEntity>): Promise<void>;
createFace(entity: Partial<AssetFaceEntity>): Promise<string>;
delete(entities: PersonEntity[]): Promise<void>;
deleteAll(): Promise<void>;
deleteAllFaces(): Promise<void>;
Expand Down
5 changes: 3 additions & 2 deletions server/src/infra/repositories/person.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ export class PersonRepository implements IPersonRepository {
return this.personRepository.save(entity);
}

async createFace(entity: AssetFaceEntity): Promise<void> {
async createFace(entity: AssetFaceEntity): Promise<string> {
if (!entity.embedding) {
throw new Error('Embedding is required to create a face');
}
await this.assetFaceRepository.insert({ ...entity, embedding: () => asVector(entity.embedding, true) });
const res = await this.assetFaceRepository.insert({ ...entity, embedding: () => asVector(entity.embedding, true) });
return res.identifiers[0].id;
}

async update(entity: Partial<PersonEntity>): Promise<PersonEntity> {
Expand Down
Loading