Skip to content

Commit

Permalink
feat(web): add setting for minimum face count for face detection (imm…
Browse files Browse the repository at this point in the history
…ich-app#4128)

* feat: add setting for minimum face count for face detection

Adds the minimum face count setting to the web interface to
circumvent detection of strangers and random background people
if desired.

* fix: codestyle, remove max for face count
  • Loading branch information
GenericGuy authored Sep 18, 2023
1 parent 40b802a commit 94cbbf3
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 5 deletions.
6 changes: 6 additions & 0 deletions cli/src/api/open-api/api.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/docs/install/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ The default configuration looks like this:
"enabled": true,
"modelName": "buffalo_l",
"minScore": 0.7,
"maxDistance": 0.6
"maxDistance": 0.6,
"minFaces": 1
}
},
"oauth": {
Expand Down
1 change: 1 addition & 0 deletions mobile/openapi/doc/RecognitionConfig.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion mobile/openapi/lib/model/recognition_config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions mobile/openapi/test/recognition_config_test.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions server/immich-openapi-specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -6471,6 +6471,9 @@
"maxDistance": {
"type": "integer"
},
"minFaces": {
"type": "integer"
},
"minScore": {
"type": "integer"
},
Expand All @@ -6484,6 +6487,7 @@
"required": [
"minScore",
"maxDistance",
"minFaces",
"enabled",
"modelName"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ describe(FacialRecognitionService.name, () => {
enabled: true,
maxDistance: 0.6,
minScore: 0.7,
minFaces: 1,
modelName: 'buffalo_l',
},
);
Expand Down
6 changes: 5 additions & 1 deletion server/src/domain/person/person.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
newJobRepositoryMock,
newPersonRepositoryMock,
newStorageRepositoryMock,
newSystemConfigRepositoryMock,
personStub,
} from '@test';
import { BulkIdErrorReason } from '../asset';
import { IJobRepository, JobName } from '../job';
import { IStorageRepository } from '../storage';
import { ISystemConfigRepository } from '../system-config';
import { PersonResponseDto } from './person.dto';
import { IPersonRepository } from './person.repository';
import { PersonService } from './person.service';
Expand All @@ -26,14 +28,16 @@ const responseDto: PersonResponseDto = {
describe(PersonService.name, () => {
let sut: PersonService;
let personMock: jest.Mocked<IPersonRepository>;
let configMock: jest.Mocked<ISystemConfigRepository>;
let storageMock: jest.Mocked<IStorageRepository>;
let jobMock: jest.Mocked<IJobRepository>;

beforeEach(async () => {
personMock = newPersonRepositoryMock();
storageMock = newStorageRepositoryMock();
configMock = newSystemConfigRepositoryMock();
jobMock = newJobRepositoryMock();
sut = new PersonService(personMock, storageMock, jobMock);
sut = new PersonService(personMock, configMock, storageMock, jobMock);
});

it('should be defined', () => {
Expand Down
10 changes: 8 additions & 2 deletions server/src/domain/person/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AuthUserDto } from '../auth';
import { mimeTypes } from '../domain.constant';
import { IJobRepository, JobName } from '../job';
import { IStorageRepository, ImmichReadStream } from '../storage';
import { ISystemConfigRepository, SystemConfigCore } from '../system-config';
import {
MergePersonDto,
PeopleResponseDto,
Expand All @@ -17,17 +18,22 @@ import { IPersonRepository, UpdateFacesData } from './person.repository';

@Injectable()
export class PersonService {
private configCore: SystemConfigCore;
readonly logger = new Logger(PersonService.name);

constructor(
@Inject(IPersonRepository) private repository: IPersonRepository,
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(IJobRepository) private jobRepository: IJobRepository,
) {}
) {
this.configCore = new SystemConfigCore(configRepository);
}

async getAll(authUser: AuthUserDto, dto: PersonSearchDto): Promise<PeopleResponseDto> {
const { machineLearning } = await this.configCore.getConfig();
const people = await this.repository.getAllForUser(authUser.id, {
minimumFaceCount: 1,
minimumFaceCount: machineLearning.facialRecognition.minFaces,
withHidden: dto.withHidden || false,
});
const persons: PersonResponseDto[] = people
Expand Down
6 changes: 6 additions & 0 deletions server/src/domain/smart-info/dto/model-config.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ export class RecognitionConfig extends ModelConfig {
@Type(() => Number)
@ApiProperty({ type: 'integer' })
maxDistance!: number;

@IsNumber()
@Min(1)
@Type(() => Number)
@ApiProperty({ type: 'integer' })
minFaces!: number;
}
1 change: 1 addition & 0 deletions server/src/domain/system-config/system-config.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const defaults = Object.freeze<SystemConfig>({
modelName: 'buffalo_l',
minScore: 0.7,
maxDistance: 0.6,
minFaces: 1,
},
},
map: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const updatedConfig = Object.freeze<SystemConfig>({
modelName: 'buffalo_l',
minScore: 0.7,
maxDistance: 0.6,
minFaces: 1,
},
},
map: {
Expand Down
2 changes: 2 additions & 0 deletions server/src/infra/entities/system-config.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export enum SystemConfigKey {
MACHINE_LEARNING_FACIAL_RECOGNITION_MODEL_NAME = 'machineLearning.facialRecognition.modelName',
MACHINE_LEARNING_FACIAL_RECOGNITION_MIN_SCORE = 'machineLearning.facialRecognition.minScore',
MACHINE_LEARNING_FACIAL_RECOGNITION_MAX_DISTANCE = 'machineLearning.facialRecognition.maxDistance',
MACHINE_LEARNING_FACIAL_RECOGNITION_MIN_FACES = 'machineLearning.facialRecognition.minFaces',

MAP_ENABLED = 'map.enabled',
MAP_TILE_URL = 'map.tileUrl',
Expand Down Expand Up @@ -164,6 +165,7 @@ export interface SystemConfig {
enabled: boolean;
modelName: string;
minScore: number;
minFaces: number;
maxDistance: number;
};
};
Expand Down
6 changes: 6 additions & 0 deletions web/src/api/open-api/api.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@
isEdited={machineLearningConfig.facialRecognition.maxDistance !==
savedConfig.facialRecognition.maxDistance}
/>

<SettingInputField
inputType={SettingInputFieldType.NUMBER}
label="MIN FACES DETECTED"
desc="The minimum number of faces of a person that must be detected for them to appear in the People tab. Setting this to a value greater than 1 can prevent strangers or blurry faces that are not the main subject of the image from being displayed."
bind:value={machineLearningConfig.facialRecognition.minFaces}
step="1"
min="1"
disabled={disabled || !machineLearningConfig.enabled || !machineLearningConfig.facialRecognition.enabled}
isEdited={machineLearningConfig.facialRecognition.minFaces !== savedConfig.facialRecognition.minFaces}
/>
</div>
</SettingAccordion>

Expand Down

0 comments on commit 94cbbf3

Please sign in to comment.