2023-12-05 16:43:15 +01:00
|
|
|
<script lang="ts">
|
2024-04-29 23:38:15 +02:00
|
|
|
import { timeBeforeShowLoadingSpinner } from '$lib/constants';
|
2024-02-14 14:09:49 +01:00
|
|
|
import { photoViewer } from '$lib/stores/assets.store';
|
|
|
|
import { getAssetThumbnailUrl, getPeopleThumbnailUrl } from '$lib/utils';
|
2024-04-29 23:38:15 +02:00
|
|
|
import { getPersonNameWithHiddenValue } from '$lib/utils/person';
|
|
|
|
import { AssetTypeEnum, ThumbnailFormat, type AssetFaceResponseDto, type PersonResponseDto } from '@immich/sdk';
|
2024-02-14 14:09:49 +01:00
|
|
|
import { mdiArrowLeftThin, mdiClose, mdiMagnify, mdiPlus } from '@mdi/js';
|
2023-12-05 16:43:15 +01:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
import { linear } from 'svelte/easing';
|
|
|
|
import { fly } from 'svelte/transition';
|
2024-02-14 14:09:49 +01:00
|
|
|
import ImageThumbnail from '../assets/thumbnail/image-thumbnail.svelte';
|
2023-12-05 16:43:15 +01:00
|
|
|
import LoadingSpinner from '../shared-components/loading-spinner.svelte';
|
2024-04-29 23:38:15 +02:00
|
|
|
import SearchPeople from '$lib/components/faces-page/people-search.svelte';
|
2024-05-04 20:29:50 +02:00
|
|
|
import CircleIconButton from '$lib/components/elements/buttons/circle-icon-button.svelte';
|
2023-12-05 16:43:15 +01:00
|
|
|
|
|
|
|
export let peopleWithFaces: AssetFaceResponseDto[];
|
|
|
|
export let allPeople: PersonResponseDto[];
|
2024-04-19 04:55:11 +02:00
|
|
|
export let editedPerson: PersonResponseDto;
|
2023-12-08 05:18:33 +01:00
|
|
|
export let assetType: AssetTypeEnum;
|
|
|
|
export let assetId: string;
|
2023-12-05 16:43:15 +01:00
|
|
|
|
|
|
|
// loading spinners
|
|
|
|
let isShowLoadingNewPerson = false;
|
|
|
|
let isShowLoadingSearch = false;
|
|
|
|
|
|
|
|
// search people
|
|
|
|
let searchedPeople: PersonResponseDto[] = [];
|
|
|
|
let searchFaces = false;
|
|
|
|
let searchName = '';
|
|
|
|
|
2024-04-29 23:38:15 +02:00
|
|
|
$: showPeople = searchName ? searchedPeople : allPeople.filter((person) => !person.isHidden);
|
|
|
|
|
2023-12-15 03:54:21 +01:00
|
|
|
const dispatch = createEventDispatcher<{
|
|
|
|
close: void;
|
|
|
|
createPerson: string | null;
|
|
|
|
reassign: PersonResponseDto;
|
|
|
|
}>();
|
2023-12-05 16:43:15 +01:00
|
|
|
const handleBackButton = () => {
|
|
|
|
dispatch('close');
|
|
|
|
};
|
|
|
|
const zoomImageToBase64 = async (face: AssetFaceResponseDto): Promise<string | null> => {
|
2023-12-08 05:18:33 +01:00
|
|
|
let image: HTMLImageElement | null = null;
|
|
|
|
if (assetType === AssetTypeEnum.Image) {
|
|
|
|
image = $photoViewer;
|
|
|
|
} else if (assetType === AssetTypeEnum.Video) {
|
2024-02-27 17:37:37 +01:00
|
|
|
const data = getAssetThumbnailUrl(assetId, ThumbnailFormat.Webp);
|
2023-12-08 05:18:33 +01:00
|
|
|
const img: HTMLImageElement = new Image();
|
|
|
|
img.src = data;
|
|
|
|
|
|
|
|
await new Promise<void>((resolve) => {
|
2024-02-02 04:18:00 +01:00
|
|
|
img.addEventListener('load', () => resolve());
|
|
|
|
img.addEventListener('error', () => resolve());
|
2023-12-08 05:18:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
image = img;
|
|
|
|
}
|
|
|
|
if (image === null) {
|
2023-12-05 16:43:15 +01:00
|
|
|
return null;
|
|
|
|
}
|
2024-02-02 04:18:00 +01:00
|
|
|
const {
|
|
|
|
boundingBoxX1: x1,
|
|
|
|
boundingBoxX2: x2,
|
|
|
|
boundingBoxY1: y1,
|
|
|
|
boundingBoxY2: y2,
|
|
|
|
imageWidth,
|
|
|
|
imageHeight,
|
|
|
|
} = face;
|
2023-12-05 16:43:15 +01:00
|
|
|
|
|
|
|
const coordinates = {
|
2024-02-02 04:18:00 +01:00
|
|
|
x1: (image.naturalWidth / imageWidth) * x1,
|
|
|
|
x2: (image.naturalWidth / imageWidth) * x2,
|
|
|
|
y1: (image.naturalHeight / imageHeight) * y1,
|
|
|
|
y2: (image.naturalHeight / imageHeight) * y2,
|
2023-12-05 16:43:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const faceWidth = coordinates.x2 - coordinates.x1;
|
|
|
|
const faceHeight = coordinates.y2 - coordinates.y1;
|
|
|
|
|
|
|
|
const faceImage = new Image();
|
2023-12-08 05:18:33 +01:00
|
|
|
faceImage.src = image.src;
|
2023-12-05 16:43:15 +01:00
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
2024-02-02 04:18:00 +01:00
|
|
|
faceImage.addEventListener('load', resolve);
|
|
|
|
faceImage.addEventListener('error', () => resolve(null));
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
canvas.width = faceWidth;
|
|
|
|
canvas.height = faceHeight;
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
if (context) {
|
|
|
|
context.drawImage(faceImage, coordinates.x1, coordinates.y1, faceWidth, faceHeight, 0, 0, faceWidth, faceHeight);
|
2023-12-05 16:43:15 +01:00
|
|
|
|
|
|
|
return canvas.toDataURL();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCreatePerson = async () => {
|
2024-01-28 01:54:31 +01:00
|
|
|
const timeout = setTimeout(() => (isShowLoadingNewPerson = true), timeBeforeShowLoadingSpinner);
|
2024-04-19 04:55:11 +02:00
|
|
|
const personToUpdate = peopleWithFaces.find((face) => face.person?.id === editedPerson.id);
|
2023-12-05 16:43:15 +01:00
|
|
|
|
|
|
|
const newFeaturePhoto = personToUpdate ? await zoomImageToBase64(personToUpdate) : null;
|
|
|
|
|
|
|
|
dispatch('createPerson', newFeaturePhoto);
|
|
|
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
isShowLoadingNewPerson = false;
|
|
|
|
dispatch('createPerson', newFeaturePhoto);
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<section
|
|
|
|
transition:fly={{ x: 360, duration: 100, easing: linear }}
|
2023-12-06 00:05:22 +01:00
|
|
|
class="absolute top-0 z-[2001] h-full w-[360px] overflow-x-hidden p-2 bg-immich-bg dark:bg-immich-dark-bg dark:text-immich-dark-fg"
|
2023-12-05 16:43:15 +01:00
|
|
|
>
|
|
|
|
<div class="flex place-items-center justify-between gap-2">
|
|
|
|
{#if !searchFaces}
|
|
|
|
<div class="flex items-center gap-2">
|
2024-05-04 20:29:50 +02:00
|
|
|
<CircleIconButton icon={mdiArrowLeftThin} title="Back" on:click={handleBackButton} />
|
2023-12-05 16:43:15 +01:00
|
|
|
<p class="flex text-lg text-immich-fg dark:text-immich-dark-fg">Select face</p>
|
|
|
|
</div>
|
|
|
|
<div class="flex justify-end gap-2">
|
2024-05-04 20:29:50 +02:00
|
|
|
<CircleIconButton
|
|
|
|
icon={mdiMagnify}
|
|
|
|
title="Search for existing person"
|
2023-12-05 16:43:15 +01:00
|
|
|
on:click={() => {
|
|
|
|
searchFaces = true;
|
|
|
|
}}
|
2024-05-04 20:29:50 +02:00
|
|
|
/>
|
2023-12-05 16:43:15 +01:00
|
|
|
{#if !isShowLoadingNewPerson}
|
2024-05-04 20:29:50 +02:00
|
|
|
<CircleIconButton icon={mdiPlus} title="Create new person" on:click={handleCreatePerson} />
|
2023-12-05 16:43:15 +01:00
|
|
|
{:else}
|
|
|
|
<div class="flex place-content-center place-items-center">
|
|
|
|
<LoadingSpinner />
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{:else}
|
2024-05-04 20:29:50 +02:00
|
|
|
<CircleIconButton icon={mdiArrowLeftThin} title="Back" on:click={handleBackButton} />
|
2023-12-05 16:43:15 +01:00
|
|
|
<div class="w-full flex">
|
2024-04-29 23:38:15 +02:00
|
|
|
<SearchPeople
|
|
|
|
type="input"
|
|
|
|
bind:searchName
|
|
|
|
bind:showLoadingSpinner={isShowLoadingSearch}
|
|
|
|
bind:searchedPeopleLocal={searchedPeople}
|
2023-12-05 16:43:15 +01:00
|
|
|
/>
|
|
|
|
{#if isShowLoadingSearch}
|
|
|
|
<div>
|
|
|
|
<LoadingSpinner />
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|
2024-05-04 20:29:50 +02:00
|
|
|
<CircleIconButton icon={mdiClose} title="Cancel search" on:click={() => (searchFaces = false)} />
|
2023-12-05 16:43:15 +01:00
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
<div class="px-4 py-4 text-sm">
|
|
|
|
<h2 class="mb-8 mt-4 uppercase">All people</h2>
|
|
|
|
<div class="immich-scrollbar mt-4 flex flex-wrap gap-2 overflow-y-auto">
|
2024-04-29 23:38:15 +02:00
|
|
|
{#each showPeople as person (person.id)}
|
|
|
|
{#if person.id !== editedPerson.id}
|
|
|
|
<div class="w-fit">
|
2024-05-27 09:06:15 +02:00
|
|
|
<button type="button" class="w-[90px]" on:click={() => dispatch('reassign', person)}>
|
2024-04-29 23:38:15 +02:00
|
|
|
<div class="relative">
|
|
|
|
<ImageThumbnail
|
|
|
|
curve
|
|
|
|
shadow
|
|
|
|
url={getPeopleThumbnailUrl(person.id)}
|
|
|
|
altText={getPersonNameWithHiddenValue(person.name, person.isHidden)}
|
|
|
|
title={getPersonNameWithHiddenValue(person.name, person.isHidden)}
|
|
|
|
widthStyle="90px"
|
|
|
|
heightStyle="90px"
|
|
|
|
thumbhash={null}
|
|
|
|
hidden={person.isHidden}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<p class="mt-1 truncate font-medium" title={getPersonNameWithHiddenValue(person.name, person.isHidden)}>
|
|
|
|
{person.name}
|
|
|
|
</p>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{/each}
|
2023-12-05 16:43:15 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</section>
|