Skip to content

Commit

Permalink
feat(faces-from-metadata): Import face regions from metadata
Browse files Browse the repository at this point in the history
Implements #1692. OpenAPI spec changes to accomodate metadata face import configs. New settings to enable the feature.

Signed-off-by: BugFest <bugfest.dev@pm.me>
  • Loading branch information
bugfest committed Jan 17, 2024
1 parent c317fea commit 6932625
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsBoolean } from 'class-validator';

export class SystemConfigMetadataDto {
@IsBoolean()
importFaces!: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts">
import ConfirmDialogue from '$lib/components/shared-components/confirm-dialogue.svelte';
</script>

<ConfirmDialogue title="Enable Import Faces from Metadata" on:cancel on:confirm>
<svelte:fragment slot="prompt">
<div class="flex flex-col gap-4 text-left">
<p>
Importing faces from metadata is currently incompatible with machine learning face recognition.
</p>
<p>
Are you sure you want to enable importing faces from metadata?
</p>
</div>
</svelte:fragment>
</ConfirmDialogue>
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<script lang="ts">
import {
notificationController,
NotificationType,
} from '$lib/components/shared-components/notification/notification';
import { handleError } from '$lib/utils/handle-error';
import { api, SystemConfigMetadataDto } from '@api';
import { isEqual } from 'lodash-es';
import { fade } from 'svelte/transition';
import ConfirmEnableImportFaces from '../confirm-enable-import-faces.svelte';
import SettingButtonsRow from '../setting-buttons-row.svelte';
import SettingSwitch from '../setting-switch.svelte';
import SettingAccordion from '../setting-accordion.svelte';
import type { ResetOptions } from '$lib/utils/dipatch';
export let metadataConfig: SystemConfigMetadataDto; // this is the config that is being edited
export let disabled = false;
let savedConfig: SystemConfigMetadataDto;
let defaultConfig: SystemConfigMetadataDto;
const handleReset = (detail: ResetOptions) => {
if (detail.default) {
resetToDefault();
} else {
reset();
}
};
async function getConfigs() {
[savedConfig, defaultConfig] = await Promise.all([
api.systemConfigApi.getConfig().then((res) => res.data.metadata),
api.systemConfigApi.getConfigDefaults().then((res) => res.data.metadata),
]);
}
let isConfirmOpen = false;
let handleConfirm: (value: boolean) => void;
const openConfirmModal = () => {
return new Promise((resolve) => {
handleConfirm = (value: boolean) => {
isConfirmOpen = false;
resolve(value);
};
isConfirmOpen = true;
});
};
async function saveSetting() {
try {
const { data: current } = await api.systemConfigApi.getConfig();
if (current.machineLearning.facialRecognition.enabled && !current.metadata?.importFaces && metadataConfig.importFaces) {
const confirmed = await openConfirmModal();
if (!confirmed) {
return;
}
}
const { data: updated } = await api.systemConfigApi.updateConfig({
systemConfigDto: {
...current,
metadata: metadataConfig,
},
});
metadataConfig = { ...updated.metadata };
savedConfig = { ...updated.metadata };
notificationController.show({ message: 'Settings saved', type: NotificationType.Info });
} catch (error) {
handleError(error, 'Unable to save settings');
}
}
async function reset() {
const { data: resetConfig } = await api.systemConfigApi.getConfig();
metadataConfig = { ...resetConfig.metadata };
savedConfig = { ...resetConfig.metadata };
notificationController.show({
message: 'Reset settings to the recent saved settings',
type: NotificationType.Info,
});
}
async function resetToDefault() {
const { data: configs } = await api.systemConfigApi.getConfigDefaults();
metadataConfig = { ...configs.metadata };
defaultConfig = { ...configs.metadata };
notificationController.show({
message: 'Reset password settings to default',
type: NotificationType.Info,
});
}
</script>

{#if isConfirmOpen}
<ConfirmEnableImportFaces on:cancel={() => handleConfirm(false)} on:confirm={() => handleConfirm(true)} />
{/if}

<div>
{#await getConfigs() then}
<div in:fade={{ duration: 500 }}>
<SettingAccordion title="Import faces" subtitle="Settings for importing face regions from metadata" isOpen>
<form autocomplete="off" on:submit|preventDefault>
<div class="ml-4 mt-4 flex flex-col gap-4">
<div class="ml-4">
<SettingSwitch
title="ENABLED"
{disabled}
subtitle="Import faces from image metadata"
bind:checked={metadataConfig.importFaces}
/>

<SettingButtonsRow
on:reset={({ detail }) => handleReset(detail)}
on:save={saveSetting}
showResetToDefault={!isEqual(savedConfig, defaultConfig)}
{disabled}
/>
</div>
</div>
</form>
</SettingAccordion>
</div>
{/await}
</div>

0 comments on commit 6932625

Please sign in to comment.