diff --git a/machine-learning/.dockerignore b/machine-learning/.dockerignore index eba74f4cd2e2a..d5a4cf7d2bfdf 100644 --- a/machine-learning/.dockerignore +++ b/machine-learning/.dockerignore @@ -1 +1,3 @@ -venv/ \ No newline at end of file +venv/ +*.zip +*.onnx \ No newline at end of file diff --git a/machine-learning/.gitignore b/machine-learning/.gitignore index 14d9798b60aa5..e31c7773ee1bc 100644 --- a/machine-learning/.gitignore +++ b/machine-learning/.gitignore @@ -3,4 +3,170 @@ upload/ venv/ __pycache__/ -model-cache/ \ No newline at end of file +model-cache/ + + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ + + +*.onnx +*.zip \ No newline at end of file diff --git a/machine-learning/Dockerfile b/machine-learning/Dockerfile index 77fc960f33c1e..e9896807ba3a2 100644 --- a/machine-learning/Dockerfile +++ b/machine-learning/Dockerfile @@ -8,7 +8,8 @@ RUN python -m venv /opt/venv RUN /opt/venv/bin/pip install torch --index-url https://download.pytorch.org/whl/cpu RUN /opt/venv/bin/pip install transformers tqdm numpy scikit-learn scipy nltk sentencepiece fastapi Pillow uvicorn[standard] RUN /opt/venv/bin/pip install --no-deps sentence-transformers - +# Facial Recognition Stuff +RUN /opt/venv/bin/pip install insightface onnxruntime FROM python:3.10-slim diff --git a/machine-learning/src/main.py b/machine-learning/src/main.py index cd6debbfbf564..b09a43464f643 100644 --- a/machine-learning/src/main.py +++ b/machine-learning/src/main.py @@ -1,9 +1,13 @@ +import os +import numpy as np +import cv2 as cv +import uvicorn + +from insightface.app import FaceAnalysis from transformers import pipeline from sentence_transformers import SentenceTransformer, util from PIL import Image from fastapi import FastAPI -import uvicorn -import os from pydantic import BaseModel @@ -15,15 +19,6 @@ class ClipRequestBody(BaseModel): text: str -is_dev = os.getenv('NODE_ENV') == 'development' -server_port = os.getenv('MACHINE_LEARNING_PORT', 3003) -server_host = os.getenv('MACHINE_LEARNING_HOST', '0.0.0.0') - -app = FastAPI() - -""" -Model Initialization -""" classification_model = os.getenv( 'MACHINE_LEARNING_CLASSIFICATION_MODEL', 'microsoft/resnet-50') object_model = os.getenv('MACHINE_LEARNING_OBJECT_MODEL', 'hustvl/yolos-tiny') @@ -31,9 +26,15 @@ clip_image_model = os.getenv( 'MACHINE_LEARNING_CLIP_IMAGE_MODEL', 'clip-ViT-B-32') clip_text_model = os.getenv( 'MACHINE_LEARNING_CLIP_TEXT_MODEL', 'clip-ViT-B-32') +facial_recognition_model = os.getenv( + 'MACHINE_LEARNING_FACIAL_RECOGNITION_MODEL', 'buffalo_l') + +cache_folder = os.getenv('MACHINE_LEARNING_CACHE_FOLDER', '/cache') _model_cache = {} +app = FastAPI() + @app.get("/") async def root(): @@ -73,6 +74,36 @@ def clip_encode_text(payload: ClipRequestBody): return model.encode(text).tolist() +@app.post("/facial-recognition/detect-faces", status_code=200) +def facial_recognition(payload: MlRequestBody): + model = _get_model(facial_recognition_model, 'facial-recognition') + assetPath = payload.thumbnailPath + img = cv.imread(assetPath) + height, width, _ = img.shape + results = [] + faces = model.get(img) + for face in faces: + if face.det_score < 0.7: + continue + x1, y1, x2, y2 = face.bbox + # min face size as percent of original image + # if (x2 - x1) / width < 0.03 or (y2 - y1) / height < 0.05: + # continue + results.append({ + "imageWidth": width, + "imageHeight": height, + "boundingBox": { + "x1": round(x1), + "y1": round(y1), + "x2": round(x2), + "y2": round(y2), + }, + "score": face.det_score.item(), + "embedding": face.normed_embedding.tolist() + }) + return results + + def run_engine(engine, path): result = [] predictions = engine(path) @@ -93,12 +124,22 @@ def _get_model(model, task=None): key = '|'.join([model, str(task)]) if key not in _model_cache: if task: - _model_cache[key] = pipeline(model=model, task=task) + if task == 'facial-recognition': + face_model = FaceAnalysis( + name=model, root=cache_folder, allowed_modules=["detection", "recognition"]) + face_model.prepare(ctx_id=0, det_size=(640, 640)) + _model_cache[key] = face_model + else: + _model_cache[key] = pipeline(model=model, task=task) else: - _model_cache[key] = SentenceTransformer(model) + _model_cache[key] = SentenceTransformer( + model, cache_folder=cache_folder) return _model_cache[key] if __name__ == "__main__": - uvicorn.run("main:app", host=server_host, - port=int(server_port), reload=is_dev, workers=1) + host = os.getenv('MACHINE_LEARNING_HOST', '0.0.0.0') + port = int(os.getenv('MACHINE_LEARNING_PORT', 3003)) + is_dev = os.getenv('NODE_ENV') == 'development' + + uvicorn.run("main:app", host=host, port=port, reload=is_dev, workers=1) diff --git a/mobile/openapi/.openapi-generator/FILES b/mobile/openapi/.openapi-generator/FILES index 42a8c3106d00d..094bcbe40a157 100644 --- a/mobile/openapi/.openapi-generator/FILES +++ b/mobile/openapi/.openapi-generator/FILES @@ -61,6 +61,9 @@ doc/OAuthCallbackDto.md doc/OAuthConfigDto.md doc/OAuthConfigResponseDto.md doc/PartnerApi.md +doc/PersonApi.md +doc/PersonResponseDto.md +doc/PersonUpdateDto.md doc/QueueStatusDto.md doc/RemoveAssetsDto.md doc/SearchAlbumResponseDto.md @@ -113,6 +116,7 @@ lib/api/authentication_api.dart lib/api/job_api.dart lib/api/o_auth_api.dart lib/api/partner_api.dart +lib/api/person_api.dart lib/api/search_api.dart lib/api/server_info_api.dart lib/api/share_api.dart @@ -178,6 +182,8 @@ lib/model/map_marker_response_dto.dart lib/model/o_auth_callback_dto.dart lib/model/o_auth_config_dto.dart lib/model/o_auth_config_response_dto.dart +lib/model/person_response_dto.dart +lib/model/person_update_dto.dart lib/model/queue_status_dto.dart lib/model/remove_assets_dto.dart lib/model/search_album_response_dto.dart @@ -274,6 +280,9 @@ test/o_auth_callback_dto_test.dart test/o_auth_config_dto_test.dart test/o_auth_config_response_dto_test.dart test/partner_api_test.dart +test/person_api_test.dart +test/person_response_dto_test.dart +test/person_update_dto_test.dart test/queue_status_dto_test.dart test/remove_assets_dto_test.dart test/search_album_response_dto_test.dart diff --git a/mobile/openapi/README.md b/mobile/openapi/README.md index 25c6c7a97a370..1e87cce855818 100644 --- a/mobile/openapi/README.md +++ b/mobile/openapi/README.md @@ -132,6 +132,11 @@ Class | Method | HTTP request | Description *PartnerApi* | [**createPartner**](doc//PartnerApi.md#createpartner) | **POST** /partner/{id} | *PartnerApi* | [**getPartners**](doc//PartnerApi.md#getpartners) | **GET** /partner | *PartnerApi* | [**removePartner**](doc//PartnerApi.md#removepartner) | **DELETE** /partner/{id} | +*PersonApi* | [**getAllPeople**](doc//PersonApi.md#getallpeople) | **GET** /person | +*PersonApi* | [**getPerson**](doc//PersonApi.md#getperson) | **GET** /person/{id} | +*PersonApi* | [**getPersonAssets**](doc//PersonApi.md#getpersonassets) | **GET** /person/{id}/assets | +*PersonApi* | [**getPersonThumbnail**](doc//PersonApi.md#getpersonthumbnail) | **GET** /person/{id}/thumbnail | +*PersonApi* | [**updatePerson**](doc//PersonApi.md#updateperson) | **PUT** /person/{id} | *SearchApi* | [**getExploreData**](doc//SearchApi.md#getexploredata) | **GET** /search/explore | *SearchApi* | [**getSearchConfig**](doc//SearchApi.md#getsearchconfig) | **GET** /search/config | *SearchApi* | [**search**](doc//SearchApi.md#search) | **GET** /search | @@ -218,6 +223,8 @@ Class | Method | HTTP request | Description - [OAuthCallbackDto](doc//OAuthCallbackDto.md) - [OAuthConfigDto](doc//OAuthConfigDto.md) - [OAuthConfigResponseDto](doc//OAuthConfigResponseDto.md) + - [PersonResponseDto](doc//PersonResponseDto.md) + - [PersonUpdateDto](doc//PersonUpdateDto.md) - [QueueStatusDto](doc//QueueStatusDto.md) - [RemoveAssetsDto](doc//RemoveAssetsDto.md) - [SearchAlbumResponseDto](doc//SearchAlbumResponseDto.md) diff --git a/mobile/openapi/doc/AllJobStatusResponseDto.md b/mobile/openapi/doc/AllJobStatusResponseDto.md index e0e8e379585e8..cac4b0f44b83f 100644 --- a/mobile/openapi/doc/AllJobStatusResponseDto.md +++ b/mobile/openapi/doc/AllJobStatusResponseDto.md @@ -16,6 +16,7 @@ Name | Type | Description | Notes **storageTemplateMigrationQueue** | [**JobStatusDto**](JobStatusDto.md) | | **backgroundTaskQueue** | [**JobStatusDto**](JobStatusDto.md) | | **searchQueue** | [**JobStatusDto**](JobStatusDto.md) | | +**recognizeFacesQueue** | [**JobStatusDto**](JobStatusDto.md) | | [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/mobile/openapi/doc/AssetResponseDto.md b/mobile/openapi/doc/AssetResponseDto.md index ea9c3784351d0..2bb059ceb29b5 100644 --- a/mobile/openapi/doc/AssetResponseDto.md +++ b/mobile/openapi/doc/AssetResponseDto.md @@ -29,6 +29,7 @@ Name | Type | Description | Notes **smartInfo** | [**SmartInfoResponseDto**](SmartInfoResponseDto.md) | | [optional] **livePhotoVideoId** | **String** | | [optional] **tags** | [**List**](TagResponseDto.md) | | [optional] [default to const []] +**people** | [**List**](PersonResponseDto.md) | | [optional] [default to const []] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/mobile/openapi/doc/PersonApi.md b/mobile/openapi/doc/PersonApi.md new file mode 100644 index 0000000000000..dd1c0eb8e41bf --- /dev/null +++ b/mobile/openapi/doc/PersonApi.md @@ -0,0 +1,291 @@ +# openapi.api.PersonApi + +## Load the API package +```dart +import 'package:openapi/api.dart'; +``` + +All URIs are relative to */api* + +Method | HTTP request | Description +------------- | ------------- | ------------- +[**getAllPeople**](PersonApi.md#getallpeople) | **GET** /person | +[**getPerson**](PersonApi.md#getperson) | **GET** /person/{id} | +[**getPersonAssets**](PersonApi.md#getpersonassets) | **GET** /person/{id}/assets | +[**getPersonThumbnail**](PersonApi.md#getpersonthumbnail) | **GET** /person/{id}/thumbnail | +[**updatePerson**](PersonApi.md#updateperson) | **PUT** /person/{id} | + + +# **getAllPeople** +> List getAllPeople() + + + +### Example +```dart +import 'package:openapi/api.dart'; +// TODO Configure API key authorization: cookie +//defaultApiClient.getAuthentication('cookie').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('cookie').apiKeyPrefix = 'Bearer'; +// TODO Configure API key authorization: api_key +//defaultApiClient.getAuthentication('api_key').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('api_key').apiKeyPrefix = 'Bearer'; +// TODO Configure HTTP Bearer authorization: bearer +// Case 1. Use String Token +//defaultApiClient.getAuthentication('bearer').setAccessToken('YOUR_ACCESS_TOKEN'); +// Case 2. Use Function which generate token. +// String yourTokenGeneratorFunction() { ... } +//defaultApiClient.getAuthentication('bearer').setAccessToken(yourTokenGeneratorFunction); + +final api_instance = PersonApi(); + +try { + final result = api_instance.getAllPeople(); + print(result); +} catch (e) { + print('Exception when calling PersonApi->getAllPeople: $e\n'); +} +``` + +### Parameters +This endpoint does not need any parameter. + +### Return type + +[**List**](PersonResponseDto.md) + +### Authorization + +[cookie](../README.md#cookie), [api_key](../README.md#api_key), [bearer](../README.md#bearer) + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + +# **getPerson** +> PersonResponseDto getPerson(id) + + + +### Example +```dart +import 'package:openapi/api.dart'; +// TODO Configure API key authorization: cookie +//defaultApiClient.getAuthentication('cookie').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('cookie').apiKeyPrefix = 'Bearer'; +// TODO Configure API key authorization: api_key +//defaultApiClient.getAuthentication('api_key').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('api_key').apiKeyPrefix = 'Bearer'; +// TODO Configure HTTP Bearer authorization: bearer +// Case 1. Use String Token +//defaultApiClient.getAuthentication('bearer').setAccessToken('YOUR_ACCESS_TOKEN'); +// Case 2. Use Function which generate token. +// String yourTokenGeneratorFunction() { ... } +//defaultApiClient.getAuthentication('bearer').setAccessToken(yourTokenGeneratorFunction); + +final api_instance = PersonApi(); +final id = 38400000-8cf0-11bd-b23e-10b96e4ef00d; // String | + +try { + final result = api_instance.getPerson(id); + print(result); +} catch (e) { + print('Exception when calling PersonApi->getPerson: $e\n'); +} +``` + +### Parameters + +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- + **id** | **String**| | + +### Return type + +[**PersonResponseDto**](PersonResponseDto.md) + +### Authorization + +[cookie](../README.md#cookie), [api_key](../README.md#api_key), [bearer](../README.md#bearer) + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + +# **getPersonAssets** +> List getPersonAssets(id) + + + +### Example +```dart +import 'package:openapi/api.dart'; +// TODO Configure API key authorization: cookie +//defaultApiClient.getAuthentication('cookie').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('cookie').apiKeyPrefix = 'Bearer'; +// TODO Configure API key authorization: api_key +//defaultApiClient.getAuthentication('api_key').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('api_key').apiKeyPrefix = 'Bearer'; +// TODO Configure HTTP Bearer authorization: bearer +// Case 1. Use String Token +//defaultApiClient.getAuthentication('bearer').setAccessToken('YOUR_ACCESS_TOKEN'); +// Case 2. Use Function which generate token. +// String yourTokenGeneratorFunction() { ... } +//defaultApiClient.getAuthentication('bearer').setAccessToken(yourTokenGeneratorFunction); + +final api_instance = PersonApi(); +final id = 38400000-8cf0-11bd-b23e-10b96e4ef00d; // String | + +try { + final result = api_instance.getPersonAssets(id); + print(result); +} catch (e) { + print('Exception when calling PersonApi->getPersonAssets: $e\n'); +} +``` + +### Parameters + +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- + **id** | **String**| | + +### Return type + +[**List**](AssetResponseDto.md) + +### Authorization + +[cookie](../README.md#cookie), [api_key](../README.md#api_key), [bearer](../README.md#bearer) + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + +# **getPersonThumbnail** +> MultipartFile getPersonThumbnail(id) + + + +### Example +```dart +import 'package:openapi/api.dart'; +// TODO Configure API key authorization: cookie +//defaultApiClient.getAuthentication('cookie').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('cookie').apiKeyPrefix = 'Bearer'; +// TODO Configure API key authorization: api_key +//defaultApiClient.getAuthentication('api_key').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('api_key').apiKeyPrefix = 'Bearer'; +// TODO Configure HTTP Bearer authorization: bearer +// Case 1. Use String Token +//defaultApiClient.getAuthentication('bearer').setAccessToken('YOUR_ACCESS_TOKEN'); +// Case 2. Use Function which generate token. +// String yourTokenGeneratorFunction() { ... } +//defaultApiClient.getAuthentication('bearer').setAccessToken(yourTokenGeneratorFunction); + +final api_instance = PersonApi(); +final id = 38400000-8cf0-11bd-b23e-10b96e4ef00d; // String | + +try { + final result = api_instance.getPersonThumbnail(id); + print(result); +} catch (e) { + print('Exception when calling PersonApi->getPersonThumbnail: $e\n'); +} +``` + +### Parameters + +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- + **id** | **String**| | + +### Return type + +[**MultipartFile**](MultipartFile.md) + +### Authorization + +[cookie](../README.md#cookie), [api_key](../README.md#api_key), [bearer](../README.md#bearer) + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/octet-stream + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + +# **updatePerson** +> PersonResponseDto updatePerson(id, personUpdateDto) + + + +### Example +```dart +import 'package:openapi/api.dart'; +// TODO Configure API key authorization: cookie +//defaultApiClient.getAuthentication('cookie').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('cookie').apiKeyPrefix = 'Bearer'; +// TODO Configure API key authorization: api_key +//defaultApiClient.getAuthentication('api_key').apiKey = 'YOUR_API_KEY'; +// uncomment below to setup prefix (e.g. Bearer) for API key, if needed +//defaultApiClient.getAuthentication('api_key').apiKeyPrefix = 'Bearer'; +// TODO Configure HTTP Bearer authorization: bearer +// Case 1. Use String Token +//defaultApiClient.getAuthentication('bearer').setAccessToken('YOUR_ACCESS_TOKEN'); +// Case 2. Use Function which generate token. +// String yourTokenGeneratorFunction() { ... } +//defaultApiClient.getAuthentication('bearer').setAccessToken(yourTokenGeneratorFunction); + +final api_instance = PersonApi(); +final id = 38400000-8cf0-11bd-b23e-10b96e4ef00d; // String | +final personUpdateDto = PersonUpdateDto(); // PersonUpdateDto | + +try { + final result = api_instance.updatePerson(id, personUpdateDto); + print(result); +} catch (e) { + print('Exception when calling PersonApi->updatePerson: $e\n'); +} +``` + +### Parameters + +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- + **id** | **String**| | + **personUpdateDto** | [**PersonUpdateDto**](PersonUpdateDto.md)| | + +### Return type + +[**PersonResponseDto**](PersonResponseDto.md) + +### Authorization + +[cookie](../README.md#cookie), [api_key](../README.md#api_key), [bearer](../README.md#bearer) + +### HTTP request headers + + - **Content-Type**: application/json + - **Accept**: application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + diff --git a/mobile/openapi/doc/PersonResponseDto.md b/mobile/openapi/doc/PersonResponseDto.md new file mode 100644 index 0000000000000..05927762a94f7 --- /dev/null +++ b/mobile/openapi/doc/PersonResponseDto.md @@ -0,0 +1,17 @@ +# openapi.model.PersonResponseDto + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**id** | **String** | | +**name** | **String** | | +**thumbnailPath** | **String** | | + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/mobile/openapi/doc/PersonUpdateDto.md b/mobile/openapi/doc/PersonUpdateDto.md new file mode 100644 index 0000000000000..30451b8c5968c --- /dev/null +++ b/mobile/openapi/doc/PersonUpdateDto.md @@ -0,0 +1,15 @@ +# openapi.model.PersonUpdateDto + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**name** | **String** | | + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/mobile/openapi/lib/api.dart b/mobile/openapi/lib/api.dart index a7d0cb827cf9f..e22e9ac694e10 100644 --- a/mobile/openapi/lib/api.dart +++ b/mobile/openapi/lib/api.dart @@ -35,6 +35,7 @@ part 'api/authentication_api.dart'; part 'api/job_api.dart'; part 'api/o_auth_api.dart'; part 'api/partner_api.dart'; +part 'api/person_api.dart'; part 'api/search_api.dart'; part 'api/server_info_api.dart'; part 'api/share_api.dart'; @@ -93,6 +94,8 @@ part 'model/map_marker_response_dto.dart'; part 'model/o_auth_callback_dto.dart'; part 'model/o_auth_config_dto.dart'; part 'model/o_auth_config_response_dto.dart'; +part 'model/person_response_dto.dart'; +part 'model/person_update_dto.dart'; part 'model/queue_status_dto.dart'; part 'model/remove_assets_dto.dart'; part 'model/search_album_response_dto.dart'; diff --git a/mobile/openapi/lib/api/person_api.dart b/mobile/openapi/lib/api/person_api.dart new file mode 100644 index 0000000000000..37f8bf8a30735 --- /dev/null +++ b/mobile/openapi/lib/api/person_api.dart @@ -0,0 +1,261 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.12 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + + +class PersonApi { + PersonApi([ApiClient? apiClient]) : apiClient = apiClient ?? defaultApiClient; + + final ApiClient apiClient; + + /// Performs an HTTP 'GET /person' operation and returns the [Response]. + Future getAllPeopleWithHttpInfo() async { + // ignore: prefer_const_declarations + final path = r'/person'; + + // ignore: prefer_final_locals + Object? postBody; + + final queryParams = []; + final headerParams = {}; + final formParams = {}; + + const contentTypes = []; + + + return apiClient.invokeAPI( + path, + 'GET', + queryParams, + postBody, + headerParams, + formParams, + contentTypes.isEmpty ? null : contentTypes.first, + ); + } + + Future?> getAllPeople() async { + final response = await getAllPeopleWithHttpInfo(); + if (response.statusCode >= HttpStatus.badRequest) { + throw ApiException(response.statusCode, await _decodeBodyBytes(response)); + } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + final responseBody = await _decodeBodyBytes(response); + return (await apiClient.deserializeAsync(responseBody, 'List') as List) + .cast() + .toList(); + + } + return null; + } + + /// Performs an HTTP 'GET /person/{id}' operation and returns the [Response]. + /// Parameters: + /// + /// * [String] id (required): + Future getPersonWithHttpInfo(String id,) async { + // ignore: prefer_const_declarations + final path = r'/person/{id}' + .replaceAll('{id}', id); + + // ignore: prefer_final_locals + Object? postBody; + + final queryParams = []; + final headerParams = {}; + final formParams = {}; + + const contentTypes = []; + + + return apiClient.invokeAPI( + path, + 'GET', + queryParams, + postBody, + headerParams, + formParams, + contentTypes.isEmpty ? null : contentTypes.first, + ); + } + + /// Parameters: + /// + /// * [String] id (required): + Future getPerson(String id,) async { + final response = await getPersonWithHttpInfo(id,); + if (response.statusCode >= HttpStatus.badRequest) { + throw ApiException(response.statusCode, await _decodeBodyBytes(response)); + } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'PersonResponseDto',) as PersonResponseDto; + + } + return null; + } + + /// Performs an HTTP 'GET /person/{id}/assets' operation and returns the [Response]. + /// Parameters: + /// + /// * [String] id (required): + Future getPersonAssetsWithHttpInfo(String id,) async { + // ignore: prefer_const_declarations + final path = r'/person/{id}/assets' + .replaceAll('{id}', id); + + // ignore: prefer_final_locals + Object? postBody; + + final queryParams = []; + final headerParams = {}; + final formParams = {}; + + const contentTypes = []; + + + return apiClient.invokeAPI( + path, + 'GET', + queryParams, + postBody, + headerParams, + formParams, + contentTypes.isEmpty ? null : contentTypes.first, + ); + } + + /// Parameters: + /// + /// * [String] id (required): + Future?> getPersonAssets(String id,) async { + final response = await getPersonAssetsWithHttpInfo(id,); + if (response.statusCode >= HttpStatus.badRequest) { + throw ApiException(response.statusCode, await _decodeBodyBytes(response)); + } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + final responseBody = await _decodeBodyBytes(response); + return (await apiClient.deserializeAsync(responseBody, 'List') as List) + .cast() + .toList(); + + } + return null; + } + + /// Performs an HTTP 'GET /person/{id}/thumbnail' operation and returns the [Response]. + /// Parameters: + /// + /// * [String] id (required): + Future getPersonThumbnailWithHttpInfo(String id,) async { + // ignore: prefer_const_declarations + final path = r'/person/{id}/thumbnail' + .replaceAll('{id}', id); + + // ignore: prefer_final_locals + Object? postBody; + + final queryParams = []; + final headerParams = {}; + final formParams = {}; + + const contentTypes = []; + + + return apiClient.invokeAPI( + path, + 'GET', + queryParams, + postBody, + headerParams, + formParams, + contentTypes.isEmpty ? null : contentTypes.first, + ); + } + + /// Parameters: + /// + /// * [String] id (required): + Future getPersonThumbnail(String id,) async { + final response = await getPersonThumbnailWithHttpInfo(id,); + if (response.statusCode >= HttpStatus.badRequest) { + throw ApiException(response.statusCode, await _decodeBodyBytes(response)); + } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'MultipartFile',) as MultipartFile; + + } + return null; + } + + /// Performs an HTTP 'PUT /person/{id}' operation and returns the [Response]. + /// Parameters: + /// + /// * [String] id (required): + /// + /// * [PersonUpdateDto] personUpdateDto (required): + Future updatePersonWithHttpInfo(String id, PersonUpdateDto personUpdateDto,) async { + // ignore: prefer_const_declarations + final path = r'/person/{id}' + .replaceAll('{id}', id); + + // ignore: prefer_final_locals + Object? postBody = personUpdateDto; + + final queryParams = []; + final headerParams = {}; + final formParams = {}; + + const contentTypes = ['application/json']; + + + return apiClient.invokeAPI( + path, + 'PUT', + queryParams, + postBody, + headerParams, + formParams, + contentTypes.isEmpty ? null : contentTypes.first, + ); + } + + /// Parameters: + /// + /// * [String] id (required): + /// + /// * [PersonUpdateDto] personUpdateDto (required): + Future updatePerson(String id, PersonUpdateDto personUpdateDto,) async { + final response = await updatePersonWithHttpInfo(id, personUpdateDto,); + if (response.statusCode >= HttpStatus.badRequest) { + throw ApiException(response.statusCode, await _decodeBodyBytes(response)); + } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'PersonResponseDto',) as PersonResponseDto; + + } + return null; + } +} diff --git a/mobile/openapi/lib/api_client.dart b/mobile/openapi/lib/api_client.dart index dd8268f608f39..e159f6057899c 100644 --- a/mobile/openapi/lib/api_client.dart +++ b/mobile/openapi/lib/api_client.dart @@ -283,6 +283,10 @@ class ApiClient { return OAuthConfigDto.fromJson(value); case 'OAuthConfigResponseDto': return OAuthConfigResponseDto.fromJson(value); + case 'PersonResponseDto': + return PersonResponseDto.fromJson(value); + case 'PersonUpdateDto': + return PersonUpdateDto.fromJson(value); case 'QueueStatusDto': return QueueStatusDto.fromJson(value); case 'RemoveAssetsDto': diff --git a/mobile/openapi/lib/model/all_job_status_response_dto.dart b/mobile/openapi/lib/model/all_job_status_response_dto.dart index aced194303c0b..bcf1990fac0b9 100644 --- a/mobile/openapi/lib/model/all_job_status_response_dto.dart +++ b/mobile/openapi/lib/model/all_job_status_response_dto.dart @@ -21,6 +21,7 @@ class AllJobStatusResponseDto { required this.storageTemplateMigrationQueue, required this.backgroundTaskQueue, required this.searchQueue, + required this.recognizeFacesQueue, }); JobStatusDto thumbnailGenerationQueue; @@ -39,6 +40,8 @@ class AllJobStatusResponseDto { JobStatusDto searchQueue; + JobStatusDto recognizeFacesQueue; + @override bool operator ==(Object other) => identical(this, other) || other is AllJobStatusResponseDto && other.thumbnailGenerationQueue == thumbnailGenerationQueue && @@ -48,7 +51,8 @@ class AllJobStatusResponseDto { other.clipEncodingQueue == clipEncodingQueue && other.storageTemplateMigrationQueue == storageTemplateMigrationQueue && other.backgroundTaskQueue == backgroundTaskQueue && - other.searchQueue == searchQueue; + other.searchQueue == searchQueue && + other.recognizeFacesQueue == recognizeFacesQueue; @override int get hashCode => @@ -60,10 +64,11 @@ class AllJobStatusResponseDto { (clipEncodingQueue.hashCode) + (storageTemplateMigrationQueue.hashCode) + (backgroundTaskQueue.hashCode) + - (searchQueue.hashCode); + (searchQueue.hashCode) + + (recognizeFacesQueue.hashCode); @override - String toString() => 'AllJobStatusResponseDto[thumbnailGenerationQueue=$thumbnailGenerationQueue, metadataExtractionQueue=$metadataExtractionQueue, videoConversionQueue=$videoConversionQueue, objectTaggingQueue=$objectTaggingQueue, clipEncodingQueue=$clipEncodingQueue, storageTemplateMigrationQueue=$storageTemplateMigrationQueue, backgroundTaskQueue=$backgroundTaskQueue, searchQueue=$searchQueue]'; + String toString() => 'AllJobStatusResponseDto[thumbnailGenerationQueue=$thumbnailGenerationQueue, metadataExtractionQueue=$metadataExtractionQueue, videoConversionQueue=$videoConversionQueue, objectTaggingQueue=$objectTaggingQueue, clipEncodingQueue=$clipEncodingQueue, storageTemplateMigrationQueue=$storageTemplateMigrationQueue, backgroundTaskQueue=$backgroundTaskQueue, searchQueue=$searchQueue, recognizeFacesQueue=$recognizeFacesQueue]'; Map toJson() { final json = {}; @@ -75,6 +80,7 @@ class AllJobStatusResponseDto { json[r'storage-template-migration-queue'] = this.storageTemplateMigrationQueue; json[r'background-task-queue'] = this.backgroundTaskQueue; json[r'search-queue'] = this.searchQueue; + json[r'recognize-faces-queue'] = this.recognizeFacesQueue; return json; } @@ -105,6 +111,7 @@ class AllJobStatusResponseDto { storageTemplateMigrationQueue: JobStatusDto.fromJson(json[r'storage-template-migration-queue'])!, backgroundTaskQueue: JobStatusDto.fromJson(json[r'background-task-queue'])!, searchQueue: JobStatusDto.fromJson(json[r'search-queue'])!, + recognizeFacesQueue: JobStatusDto.fromJson(json[r'recognize-faces-queue'])!, ); } return null; @@ -160,6 +167,7 @@ class AllJobStatusResponseDto { 'storage-template-migration-queue', 'background-task-queue', 'search-queue', + 'recognize-faces-queue', }; } diff --git a/mobile/openapi/lib/model/asset_response_dto.dart b/mobile/openapi/lib/model/asset_response_dto.dart index e9f0185ce8e9e..a238e73497eda 100644 --- a/mobile/openapi/lib/model/asset_response_dto.dart +++ b/mobile/openapi/lib/model/asset_response_dto.dart @@ -34,6 +34,7 @@ class AssetResponseDto { this.smartInfo, this.livePhotoVideoId, this.tags = const [], + this.people = const [], }); AssetTypeEnum type; @@ -90,6 +91,8 @@ class AssetResponseDto { List tags; + List people; + @override bool operator ==(Object other) => identical(this, other) || other is AssetResponseDto && other.type == type && @@ -112,7 +115,8 @@ class AssetResponseDto { other.exifInfo == exifInfo && other.smartInfo == smartInfo && other.livePhotoVideoId == livePhotoVideoId && - other.tags == tags; + other.tags == tags && + other.people == people; @override int get hashCode => @@ -137,10 +141,11 @@ class AssetResponseDto { (exifInfo == null ? 0 : exifInfo!.hashCode) + (smartInfo == null ? 0 : smartInfo!.hashCode) + (livePhotoVideoId == null ? 0 : livePhotoVideoId!.hashCode) + - (tags.hashCode); + (tags.hashCode) + + (people.hashCode); @override - String toString() => 'AssetResponseDto[type=$type, id=$id, deviceAssetId=$deviceAssetId, ownerId=$ownerId, deviceId=$deviceId, originalPath=$originalPath, originalFileName=$originalFileName, resizePath=$resizePath, fileCreatedAt=$fileCreatedAt, fileModifiedAt=$fileModifiedAt, updatedAt=$updatedAt, isFavorite=$isFavorite, isArchived=$isArchived, mimeType=$mimeType, duration=$duration, webpPath=$webpPath, encodedVideoPath=$encodedVideoPath, exifInfo=$exifInfo, smartInfo=$smartInfo, livePhotoVideoId=$livePhotoVideoId, tags=$tags]'; + String toString() => 'AssetResponseDto[type=$type, id=$id, deviceAssetId=$deviceAssetId, ownerId=$ownerId, deviceId=$deviceId, originalPath=$originalPath, originalFileName=$originalFileName, resizePath=$resizePath, fileCreatedAt=$fileCreatedAt, fileModifiedAt=$fileModifiedAt, updatedAt=$updatedAt, isFavorite=$isFavorite, isArchived=$isArchived, mimeType=$mimeType, duration=$duration, webpPath=$webpPath, encodedVideoPath=$encodedVideoPath, exifInfo=$exifInfo, smartInfo=$smartInfo, livePhotoVideoId=$livePhotoVideoId, tags=$tags, people=$people]'; Map toJson() { final json = {}; @@ -193,6 +198,7 @@ class AssetResponseDto { // json[r'livePhotoVideoId'] = null; } json[r'tags'] = this.tags; + json[r'people'] = this.people; return json; } @@ -236,6 +242,7 @@ class AssetResponseDto { smartInfo: SmartInfoResponseDto.fromJson(json[r'smartInfo']), livePhotoVideoId: mapValueOfType(json, r'livePhotoVideoId'), tags: TagResponseDto.listFromJson(json[r'tags']), + people: PersonResponseDto.listFromJson(json[r'people']), ); } return null; diff --git a/mobile/openapi/lib/model/job_name.dart b/mobile/openapi/lib/model/job_name.dart index 9d9424515e24f..b76e44e3ff25f 100644 --- a/mobile/openapi/lib/model/job_name.dart +++ b/mobile/openapi/lib/model/job_name.dart @@ -27,6 +27,7 @@ class JobName { static const metadataExtractionQueue = JobName._(r'metadata-extraction-queue'); static const videoConversionQueue = JobName._(r'video-conversion-queue'); static const objectTaggingQueue = JobName._(r'object-tagging-queue'); + static const recognizeFacesQueue = JobName._(r'recognize-faces-queue'); static const clipEncodingQueue = JobName._(r'clip-encoding-queue'); static const backgroundTaskQueue = JobName._(r'background-task-queue'); static const storageTemplateMigrationQueue = JobName._(r'storage-template-migration-queue'); @@ -38,6 +39,7 @@ class JobName { metadataExtractionQueue, videoConversionQueue, objectTaggingQueue, + recognizeFacesQueue, clipEncodingQueue, backgroundTaskQueue, storageTemplateMigrationQueue, @@ -84,6 +86,7 @@ class JobNameTypeTransformer { case r'metadata-extraction-queue': return JobName.metadataExtractionQueue; case r'video-conversion-queue': return JobName.videoConversionQueue; case r'object-tagging-queue': return JobName.objectTaggingQueue; + case r'recognize-faces-queue': return JobName.recognizeFacesQueue; case r'clip-encoding-queue': return JobName.clipEncodingQueue; case r'background-task-queue': return JobName.backgroundTaskQueue; case r'storage-template-migration-queue': return JobName.storageTemplateMigrationQueue; diff --git a/mobile/openapi/lib/model/person_response_dto.dart b/mobile/openapi/lib/model/person_response_dto.dart new file mode 100644 index 0000000000000..8e23656f79a97 --- /dev/null +++ b/mobile/openapi/lib/model/person_response_dto.dart @@ -0,0 +1,125 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.12 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + +class PersonResponseDto { + /// Returns a new [PersonResponseDto] instance. + PersonResponseDto({ + required this.id, + required this.name, + required this.thumbnailPath, + }); + + String id; + + String name; + + String thumbnailPath; + + @override + bool operator ==(Object other) => identical(this, other) || other is PersonResponseDto && + other.id == id && + other.name == name && + other.thumbnailPath == thumbnailPath; + + @override + int get hashCode => + // ignore: unnecessary_parenthesis + (id.hashCode) + + (name.hashCode) + + (thumbnailPath.hashCode); + + @override + String toString() => 'PersonResponseDto[id=$id, name=$name, thumbnailPath=$thumbnailPath]'; + + Map toJson() { + final json = {}; + json[r'id'] = this.id; + json[r'name'] = this.name; + json[r'thumbnailPath'] = this.thumbnailPath; + return json; + } + + /// Returns a new [PersonResponseDto] instance and imports its values from + /// [value] if it's a [Map], null otherwise. + // ignore: prefer_constructors_over_static_methods + static PersonResponseDto? fromJson(dynamic value) { + if (value is Map) { + final json = value.cast(); + + // Ensure that the map contains the required keys. + // Note 1: the values aren't checked for validity beyond being non-null. + // Note 2: this code is stripped in release mode! + assert(() { + requiredKeys.forEach((key) { + assert(json.containsKey(key), 'Required key "PersonResponseDto[$key]" is missing from JSON.'); + assert(json[key] != null, 'Required key "PersonResponseDto[$key]" has a null value in JSON.'); + }); + return true; + }()); + + return PersonResponseDto( + id: mapValueOfType(json, r'id')!, + name: mapValueOfType(json, r'name')!, + thumbnailPath: mapValueOfType(json, r'thumbnailPath')!, + ); + } + return null; + } + + static List listFromJson(dynamic json, {bool growable = false,}) { + final result = []; + if (json is List && json.isNotEmpty) { + for (final row in json) { + final value = PersonResponseDto.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } + + static Map mapFromJson(dynamic json) { + final map = {}; + if (json is Map && json.isNotEmpty) { + json = json.cast(); // ignore: parameter_assignments + for (final entry in json.entries) { + final value = PersonResponseDto.fromJson(entry.value); + if (value != null) { + map[entry.key] = value; + } + } + } + return map; + } + + // maps a json object with a list of PersonResponseDto-objects as value to a dart map + static Map> mapListFromJson(dynamic json, {bool growable = false,}) { + final map = >{}; + if (json is Map && json.isNotEmpty) { + // ignore: parameter_assignments + json = json.cast(); + for (final entry in json.entries) { + map[entry.key] = PersonResponseDto.listFromJson(entry.value, growable: growable,); + } + } + return map; + } + + /// The list of required keys that must be present in a JSON. + static const requiredKeys = { + 'id', + 'name', + 'thumbnailPath', + }; +} + diff --git a/mobile/openapi/lib/model/person_update_dto.dart b/mobile/openapi/lib/model/person_update_dto.dart new file mode 100644 index 0000000000000..773cf6aedf202 --- /dev/null +++ b/mobile/openapi/lib/model/person_update_dto.dart @@ -0,0 +1,109 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.12 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + +class PersonUpdateDto { + /// Returns a new [PersonUpdateDto] instance. + PersonUpdateDto({ + required this.name, + }); + + String name; + + @override + bool operator ==(Object other) => identical(this, other) || other is PersonUpdateDto && + other.name == name; + + @override + int get hashCode => + // ignore: unnecessary_parenthesis + (name.hashCode); + + @override + String toString() => 'PersonUpdateDto[name=$name]'; + + Map toJson() { + final json = {}; + json[r'name'] = this.name; + return json; + } + + /// Returns a new [PersonUpdateDto] instance and imports its values from + /// [value] if it's a [Map], null otherwise. + // ignore: prefer_constructors_over_static_methods + static PersonUpdateDto? fromJson(dynamic value) { + if (value is Map) { + final json = value.cast(); + + // Ensure that the map contains the required keys. + // Note 1: the values aren't checked for validity beyond being non-null. + // Note 2: this code is stripped in release mode! + assert(() { + requiredKeys.forEach((key) { + assert(json.containsKey(key), 'Required key "PersonUpdateDto[$key]" is missing from JSON.'); + assert(json[key] != null, 'Required key "PersonUpdateDto[$key]" has a null value in JSON.'); + }); + return true; + }()); + + return PersonUpdateDto( + name: mapValueOfType(json, r'name')!, + ); + } + return null; + } + + static List listFromJson(dynamic json, {bool growable = false,}) { + final result = []; + if (json is List && json.isNotEmpty) { + for (final row in json) { + final value = PersonUpdateDto.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } + + static Map mapFromJson(dynamic json) { + final map = {}; + if (json is Map && json.isNotEmpty) { + json = json.cast(); // ignore: parameter_assignments + for (final entry in json.entries) { + final value = PersonUpdateDto.fromJson(entry.value); + if (value != null) { + map[entry.key] = value; + } + } + } + return map; + } + + // maps a json object with a list of PersonUpdateDto-objects as value to a dart map + static Map> mapListFromJson(dynamic json, {bool growable = false,}) { + final map = >{}; + if (json is Map && json.isNotEmpty) { + // ignore: parameter_assignments + json = json.cast(); + for (final entry in json.entries) { + map[entry.key] = PersonUpdateDto.listFromJson(entry.value, growable: growable,); + } + } + return map; + } + + /// The list of required keys that must be present in a JSON. + static const requiredKeys = { + 'name', + }; +} + diff --git a/mobile/openapi/test/all_job_status_response_dto_test.dart b/mobile/openapi/test/all_job_status_response_dto_test.dart index 319eb2b10cd9f..eff589c9a8ef5 100644 --- a/mobile/openapi/test/all_job_status_response_dto_test.dart +++ b/mobile/openapi/test/all_job_status_response_dto_test.dart @@ -56,6 +56,11 @@ void main() { // TODO }); + // JobStatusDto recognizeFacesQueue + test('to test the property `recognizeFacesQueue`', () async { + // TODO + }); + }); diff --git a/mobile/openapi/test/asset_response_dto_test.dart b/mobile/openapi/test/asset_response_dto_test.dart index 4a9be63cb8f88..692b41b95e27e 100644 --- a/mobile/openapi/test/asset_response_dto_test.dart +++ b/mobile/openapi/test/asset_response_dto_test.dart @@ -121,6 +121,11 @@ void main() { // TODO }); + // List people (default value: const []) + test('to test the property `people`', () async { + // TODO + }); + }); diff --git a/mobile/openapi/test/person_api_test.dart b/mobile/openapi/test/person_api_test.dart new file mode 100644 index 0000000000000..33e78b8a7cfc4 --- /dev/null +++ b/mobile/openapi/test/person_api_test.dart @@ -0,0 +1,46 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.12 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +import 'package:openapi/api.dart'; +import 'package:test/test.dart'; + + +/// tests for PersonApi +void main() { + // final instance = PersonApi(); + + group('tests for PersonApi', () { + //Future> getAllPeople() async + test('test getAllPeople', () async { + // TODO + }); + + //Future getPerson(String id) async + test('test getPerson', () async { + // TODO + }); + + //Future> getPersonAssets(String id) async + test('test getPersonAssets', () async { + // TODO + }); + + //Future getPersonThumbnail(String id) async + test('test getPersonThumbnail', () async { + // TODO + }); + + //Future updatePerson(String id, PersonUpdateDto personUpdateDto) async + test('test updatePerson', () async { + // TODO + }); + + }); +} diff --git a/mobile/openapi/test/person_response_dto_test.dart b/mobile/openapi/test/person_response_dto_test.dart new file mode 100644 index 0000000000000..9adcbe1546210 --- /dev/null +++ b/mobile/openapi/test/person_response_dto_test.dart @@ -0,0 +1,37 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.12 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +import 'package:openapi/api.dart'; +import 'package:test/test.dart'; + +// tests for PersonResponseDto +void main() { + // final instance = PersonResponseDto(); + + group('test PersonResponseDto', () { + // String id + test('to test the property `id`', () async { + // TODO + }); + + // String name + test('to test the property `name`', () async { + // TODO + }); + + // String thumbnailPath + test('to test the property `thumbnailPath`', () async { + // TODO + }); + + + }); + +} diff --git a/mobile/openapi/test/person_update_dto_test.dart b/mobile/openapi/test/person_update_dto_test.dart new file mode 100644 index 0000000000000..6e09d860283a1 --- /dev/null +++ b/mobile/openapi/test/person_update_dto_test.dart @@ -0,0 +1,27 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.12 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +import 'package:openapi/api.dart'; +import 'package:test/test.dart'; + +// tests for PersonUpdateDto +void main() { + // final instance = PersonUpdateDto(); + + group('test PersonUpdateDto', () { + // String name + test('to test the property `name`', () async { + // TODO + }); + + + }); + +} diff --git a/server/apps/immich/src/api-v1/asset/asset-repository.ts b/server/apps/immich/src/api-v1/asset/asset-repository.ts index f01d35119c81c..0a0d9feeee28d 100644 --- a/server/apps/immich/src/api-v1/asset/asset-repository.ts +++ b/server/apps/immich/src/api-v1/asset/asset-repository.ts @@ -210,7 +210,15 @@ export class AssetRepository implements IAssetRepository { where: { id: assetId, }, - relations: ['exifInfo', 'tags', 'sharedLinks', 'smartInfo'], + relations: { + exifInfo: true, + tags: true, + sharedLinks: true, + smartInfo: true, + faces: { + person: true, + }, + }, }); } @@ -239,7 +247,14 @@ export class AssetRepository implements IAssetRepository { } get(id: string): Promise { - return this.assetRepository.findOne({ where: { id } }); + return this.assetRepository.findOne({ + where: { id }, + relations: { + faces: { + person: true, + }, + }, + }); } async create( @@ -264,11 +279,6 @@ export class AssetRepository implements IAssetRepository { asset.isFavorite = dto.isFavorite ?? asset.isFavorite; asset.isArchived = dto.isArchived ?? asset.isArchived; - if (dto.tagIds) { - const tags = await this._tagRepository.getByIds(userId, dto.tagIds); - asset.tags = tags; - } - if (asset.exifInfo != null) { asset.exifInfo.description = dto.description || ''; await this.exifRepository.save(asset.exifInfo); @@ -280,7 +290,12 @@ export class AssetRepository implements IAssetRepository { asset.exifInfo = exifInfo; } - return await this.assetRepository.save(asset); + await this.assetRepository.update(asset.id, { + isFavorite: asset.isFavorite, + isArchived: asset.isArchived, + }); + + return asset; } /** diff --git a/server/apps/immich/src/api-v1/asset/asset.core.ts b/server/apps/immich/src/api-v1/asset/asset.core.ts index 972fafd44527e..f0a819f2f5478 100644 --- a/server/apps/immich/src/api-v1/asset/asset.core.ts +++ b/server/apps/immich/src/api-v1/asset/asset.core.ts @@ -38,6 +38,7 @@ export class AssetCore { tags: [], sharedLinks: [], originalFileName: parse(file.originalName).name, + faces: [], }); await this.jobRepository.queue({ name: JobName.ASSET_UPLOADED, data: { asset, fileName: file.originalName } }); diff --git a/server/apps/immich/src/api-v1/asset/asset.service.ts b/server/apps/immich/src/api-v1/asset/asset.service.ts index dcaae0d956db4..3d40b7984fd60 100644 --- a/server/apps/immich/src/api-v1/asset/asset.service.ts +++ b/server/apps/immich/src/api-v1/asset/asset.service.ts @@ -355,6 +355,14 @@ export class AssetService { } try { + if (asset.faces) { + await Promise.all( + asset.faces.map(({ assetId, personId }) => + this.jobRepository.queue({ name: JobName.SEARCH_REMOVE_FACE, data: { assetId, personId } }), + ), + ); + } + await this._assetRepository.remove(asset); await this.jobRepository.queue({ name: JobName.SEARCH_REMOVE_ASSET, data: { ids: [id] } }); diff --git a/server/apps/immich/src/app.cron-jobs.ts b/server/apps/immich/src/app.cron-jobs.ts index c05f3e908f44f..7473c90b5b4e7 100644 --- a/server/apps/immich/src/app.cron-jobs.ts +++ b/server/apps/immich/src/app.cron-jobs.ts @@ -1,13 +1,13 @@ -import { UserService } from '@app/domain'; +import { JobService } from '@app/domain'; import { Injectable } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; @Injectable() export class AppCronJobs { - constructor(private userService: UserService) {} + constructor(private jobService: JobService) {} - @Cron(CronExpression.EVERY_DAY_AT_11PM) - async onQueueUserDeleteCheck() { - await this.userService.handleQueueUserDelete(); + @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT) + async onNightlyJob() { + await this.jobService.handleNightlyJobs(); } } diff --git a/server/apps/immich/src/app.module.ts b/server/apps/immich/src/app.module.ts index 11b5f15257d91..7d78292aee750 100644 --- a/server/apps/immich/src/app.module.ts +++ b/server/apps/immich/src/app.module.ts @@ -10,6 +10,7 @@ import { AlbumController, APIKeyController, AuthController, + PersonController, JobController, OAuthController, PartnerController, @@ -44,6 +45,7 @@ import { AppCronJobs } from './app.cron-jobs'; SharedLinkController, SystemConfigController, UserController, + PersonController, ], providers: [ // diff --git a/server/apps/immich/src/controllers/index.ts b/server/apps/immich/src/controllers/index.ts index 9846b43a33c68..1ffb3b79cb8c3 100644 --- a/server/apps/immich/src/controllers/index.ts +++ b/server/apps/immich/src/controllers/index.ts @@ -4,6 +4,7 @@ export * from './auth.controller'; export * from './job.controller'; export * from './oauth.controller'; export * from './partner.controller'; +export * from './person.controller'; export * from './search.controller'; export * from './server-info.controller'; export * from './shared-link.controller'; diff --git a/server/apps/immich/src/controllers/person.controller.ts b/server/apps/immich/src/controllers/person.controller.ts new file mode 100644 index 0000000000000..a2a597ca4c64e --- /dev/null +++ b/server/apps/immich/src/controllers/person.controller.ts @@ -0,0 +1,57 @@ +import { + AssetResponseDto, + AuthUserDto, + ImmichReadStream, + PersonResponseDto, + PersonService, + PersonUpdateDto, +} from '@app/domain'; +import { Body, Controller, Get, Param, Put, StreamableFile } from '@nestjs/common'; +import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; +import { GetAuthUser } from '../decorators/auth-user.decorator'; + +import { Authenticated } from '../decorators/authenticated.decorator'; +import { UseValidation } from '../decorators/use-validation.decorator'; +import { UUIDParamDto } from './dto/uuid-param.dto'; + +function asStreamableFile({ stream, type, length }: ImmichReadStream) { + return new StreamableFile(stream, { type, length }); +} + +@ApiTags('Person') +@Controller('person') +@Authenticated() +@UseValidation() +export class PersonController { + constructor(private service: PersonService) {} + + @Get() + getAllPeople(@GetAuthUser() authUser: AuthUserDto): Promise { + return this.service.getAll(authUser); + } + + @Get(':id') + getPerson(@GetAuthUser() authUser: AuthUserDto, @Param() { id }: UUIDParamDto): Promise { + return this.service.getById(authUser, id); + } + + @Put(':id') + updatePerson( + @GetAuthUser() authUser: AuthUserDto, + @Param() { id }: UUIDParamDto, + @Body() dto: PersonUpdateDto, + ): Promise { + return this.service.update(authUser, id, dto); + } + + @Get(':id/thumbnail') + @ApiOkResponse({ content: { 'application/octet-stream': { schema: { type: 'string', format: 'binary' } } } }) + getPersonThumbnail(@GetAuthUser() authUser: AuthUserDto, @Param() { id }: UUIDParamDto) { + return this.service.getThumbnail(authUser, id).then(asStreamableFile); + } + + @Get(':id/assets') + getPersonAssets(@GetAuthUser() authUser: AuthUserDto, @Param() { id }: UUIDParamDto): Promise { + return this.service.getAssets(authUser, id); + } +} diff --git a/server/apps/microservices/src/microservices.module.ts b/server/apps/microservices/src/microservices.module.ts index 5db6916c501e7..76d7957964fa5 100644 --- a/server/apps/microservices/src/microservices.module.ts +++ b/server/apps/microservices/src/microservices.module.ts @@ -6,6 +6,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { BackgroundTaskProcessor, ClipEncodingProcessor, + FacialRecognitionProcessor, ObjectTaggingProcessor, SearchIndexProcessor, StorageTemplateMigrationProcessor, @@ -29,6 +30,7 @@ import { MetadataExtractionProcessor } from './processors/metadata-extraction.pr StorageTemplateMigrationProcessor, BackgroundTaskProcessor, SearchIndexProcessor, + FacialRecognitionProcessor, ], }) export class MicroservicesModule {} diff --git a/server/apps/microservices/src/processors.ts b/server/apps/microservices/src/processors.ts index a8c213361d287..22133718ba6de 100644 --- a/server/apps/microservices/src/processors.ts +++ b/server/apps/microservices/src/processors.ts @@ -1,13 +1,17 @@ import { AssetService, + FacialRecognitionService, + IAssetFaceJob, IAssetJob, IAssetUploadedJob, IBaseJob, IBulkEntityJob, IDeleteFilesJob, + IFaceThumbnailJob, IUserDeletionJob, JobName, MediaService, + PersonService, QueueName, SearchService, SmartInfoService, @@ -23,6 +27,7 @@ import { Job } from 'bull'; export class BackgroundTaskProcessor { constructor( private assetService: AssetService, + private personService: PersonService, private storageService: StorageService, private systemConfigService: SystemConfigService, private userService: UserService, @@ -43,10 +48,20 @@ export class BackgroundTaskProcessor { await this.systemConfigService.refreshConfig(); } + @Process(JobName.USER_DELETE_CHECK) + async onUserDeleteCheck() { + await this.userService.handleUserDeleteCheck(); + } + @Process(JobName.USER_DELETION) async onUserDelete(job: Job) { await this.userService.handleUserDelete(job.data); } + + @Process(JobName.PERSON_CLEANUP) + async onPersonCleanup() { + await this.personService.handlePersonCleanup(); + } } @Processor(QueueName.OBJECT_TAGGING) @@ -69,6 +84,26 @@ export class ObjectTaggingProcessor { } } +@Processor(QueueName.RECOGNIZE_FACES) +export class FacialRecognitionProcessor { + constructor(private facialRecognitionService: FacialRecognitionService) {} + + @Process({ name: JobName.QUEUE_RECOGNIZE_FACES, concurrency: 1 }) + async onQueueRecognizeFaces(job: Job) { + await this.facialRecognitionService.handleQueueRecognizeFaces(job.data); + } + + @Process({ name: JobName.RECOGNIZE_FACES, concurrency: 1 }) + async onRecognizeFaces(job: Job) { + await this.facialRecognitionService.handleRecognizeFaces(job.data); + } + + @Process({ name: JobName.GENERATE_FACE_THUMBNAIL, concurrency: 1 }) + async onGenerateFaceThumbnail(job: Job) { + await this.facialRecognitionService.handleGenerateFaceThumbnail(job.data); + } +} + @Processor(QueueName.CLIP_ENCODING) export class ClipEncodingProcessor { constructor(private smartInfoService: SmartInfoService) {} @@ -98,6 +133,11 @@ export class SearchIndexProcessor { await this.searchService.handleIndexAssets(); } + @Process(JobName.SEARCH_INDEX_FACES) + async onIndexFaces() { + await this.searchService.handleIndexFaces(); + } + @Process(JobName.SEARCH_INDEX_ALBUM) onIndexAlbum(job: Job) { this.searchService.handleIndexAlbum(job.data); @@ -108,6 +148,11 @@ export class SearchIndexProcessor { this.searchService.handleIndexAsset(job.data); } + @Process(JobName.SEARCH_INDEX_FACE) + async onIndexFace(job: Job) { + await this.searchService.handleIndexFace(job.data); + } + @Process(JobName.SEARCH_REMOVE_ALBUM) onRemoveAlbum(job: Job) { this.searchService.handleRemoveAlbum(job.data); @@ -117,6 +162,11 @@ export class SearchIndexProcessor { onRemoveAsset(job: Job) { this.searchService.handleRemoveAsset(job.data); } + + @Process(JobName.SEARCH_REMOVE_FACE) + onRemoveFace(job: Job) { + this.searchService.handleRemoveFace(job.data); + } } @Processor(QueueName.STORAGE_TEMPLATE_MIGRATION) diff --git a/server/immich-openapi-specs.json b/server/immich-openapi-specs.json index 20d2f686a4966..adacbf522f00d 100644 --- a/server/immich-openapi-specs.json +++ b/server/immich-openapi-specs.json @@ -1988,6 +1988,221 @@ ] } }, + "/person": { + "get": { + "operationId": "getAllPeople", + "parameters": [], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PersonResponseDto" + } + } + } + } + } + }, + "tags": [ + "Person" + ], + "security": [ + { + "bearer": [] + }, + { + "cookie": [] + }, + { + "api_key": [] + } + ] + } + }, + "/person/{id}": { + "get": { + "operationId": "getPerson", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "format": "uuid", + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PersonResponseDto" + } + } + } + } + }, + "tags": [ + "Person" + ], + "security": [ + { + "bearer": [] + }, + { + "cookie": [] + }, + { + "api_key": [] + } + ] + }, + "put": { + "operationId": "updatePerson", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "format": "uuid", + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PersonUpdateDto" + } + } + } + }, + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PersonResponseDto" + } + } + } + } + }, + "tags": [ + "Person" + ], + "security": [ + { + "bearer": [] + }, + { + "cookie": [] + }, + { + "api_key": [] + } + ] + } + }, + "/person/{id}/thumbnail": { + "get": { + "operationId": "getPersonThumbnail", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "format": "uuid", + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/octet-stream": { + "schema": { + "type": "string", + "format": "binary" + } + } + }, + "description": "" + } + }, + "tags": [ + "Person" + ], + "security": [ + { + "bearer": [] + }, + { + "cookie": [] + }, + { + "api_key": [] + } + ] + } + }, + "/person/{id}/assets": { + "get": { + "operationId": "getPersonAssets", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "format": "uuid", + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AssetResponseDto" + } + } + } + } + } + }, + "tags": [ + "Person" + ], + "security": [ + { + "bearer": [] + }, + { + "cookie": [] + }, + { + "api_key": [] + } + ] + } + }, "/asset/upload": { "post": { "operationId": "uploadFile", @@ -4130,6 +4345,25 @@ "userId" ] }, + "PersonResponseDto": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "thumbnailPath": { + "type": "string" + } + }, + "required": [ + "id", + "name", + "thumbnailPath" + ] + }, "AssetResponseDto": { "type": "object", "properties": { @@ -4203,6 +4437,12 @@ "items": { "$ref": "#/components/schemas/TagResponseDto" } + }, + "people": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PersonResponseDto" + } } }, "required": [ @@ -4618,6 +4858,9 @@ }, "search-queue": { "$ref": "#/components/schemas/JobStatusDto" + }, + "recognize-faces-queue": { + "$ref": "#/components/schemas/JobStatusDto" } }, "required": [ @@ -4628,7 +4871,8 @@ "clip-encoding-queue", "storage-template-migration-queue", "background-task-queue", - "search-queue" + "search-queue", + "recognize-faces-queue" ] }, "JobName": { @@ -4638,6 +4882,7 @@ "metadata-extraction-queue", "video-conversion-queue", "object-tagging-queue", + "recognize-faces-queue", "clip-encoding-queue", "background-task-queue", "storage-template-migration-queue", @@ -5375,6 +5620,17 @@ "profileImagePath" ] }, + "PersonUpdateDto": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ] + }, "CreateAssetDto": { "type": "object", "properties": { diff --git a/server/libs/domain/src/asset/asset.repository.ts b/server/libs/domain/src/asset/asset.repository.ts index 0df1a40ee86cb..ed3a66a29c8d2 100644 --- a/server/libs/domain/src/asset/asset.repository.ts +++ b/server/libs/domain/src/asset/asset.repository.ts @@ -18,6 +18,7 @@ export enum WithoutProperty { EXIF = 'exif', CLIP_ENCODING = 'clip-embedding', OBJECT_TAGS = 'object-tags', + FACES = 'faces', } export const IAssetRepository = 'IAssetRepository'; diff --git a/server/libs/domain/src/asset/response-dto/asset-response.dto.ts b/server/libs/domain/src/asset/response-dto/asset-response.dto.ts index e78c4ddf9c1c9..1cb9a58e35504 100644 --- a/server/libs/domain/src/asset/response-dto/asset-response.dto.ts +++ b/server/libs/domain/src/asset/response-dto/asset-response.dto.ts @@ -1,8 +1,9 @@ import { AssetEntity, AssetType } from '@app/infra/entities'; import { ApiProperty } from '@nestjs/swagger'; +import { mapFace, PersonResponseDto } from '../../person'; import { mapTag, TagResponseDto } from '../../tag'; import { ExifResponseDto, mapExif } from './exif-response.dto'; -import { SmartInfoResponseDto, mapSmartInfo } from './smart-info-response.dto'; +import { mapSmartInfo, SmartInfoResponseDto } from './smart-info-response.dto'; export class AssetResponseDto { id!: string; @@ -28,6 +29,7 @@ export class AssetResponseDto { smartInfo?: SmartInfoResponseDto; livePhotoVideoId?: string | null; tags?: TagResponseDto[]; + people?: PersonResponseDto[]; } export function mapAsset(entity: AssetEntity): AssetResponseDto { @@ -53,6 +55,7 @@ export function mapAsset(entity: AssetEntity): AssetResponseDto { smartInfo: entity.smartInfo ? mapSmartInfo(entity.smartInfo) : undefined, livePhotoVideoId: entity.livePhotoVideoId, tags: entity.tags?.map(mapTag), + people: entity.faces?.map(mapFace), }; } @@ -79,5 +82,6 @@ export function mapAssetWithoutExif(entity: AssetEntity): AssetResponseDto { smartInfo: entity.smartInfo ? mapSmartInfo(entity.smartInfo) : undefined, livePhotoVideoId: entity.livePhotoVideoId, tags: entity.tags?.map(mapTag), + people: entity.faces?.map(mapFace), }; } diff --git a/server/libs/domain/src/domain.module.ts b/server/libs/domain/src/domain.module.ts index b2c866f3acae9..d06103b0ee380 100644 --- a/server/libs/domain/src/domain.module.ts +++ b/server/libs/domain/src/domain.module.ts @@ -3,27 +3,31 @@ import { AlbumService } from './album'; import { APIKeyService } from './api-key'; import { AssetService } from './asset'; import { AuthService } from './auth'; +import { FacialRecognitionService } from './facial-recognition'; import { JobService } from './job'; import { MediaService } from './media'; import { OAuthService } from './oauth'; import { PartnerService } from './partner'; +import { PersonService } from './person'; import { SearchService } from './search'; import { ServerInfoService } from './server-info'; import { ShareService } from './share'; import { SmartInfoService } from './smart-info'; import { StorageService } from './storage'; import { StorageTemplateService } from './storage-template'; -import { UserService } from './user'; import { INITIAL_SYSTEM_CONFIG, SystemConfigService } from './system-config'; +import { UserService } from './user'; const providers: Provider[] = [ AlbumService, APIKeyService, AssetService, AuthService, + FacialRecognitionService, JobService, MediaService, OAuthService, + PersonService, PartnerService, SearchService, ServerInfoService, diff --git a/server/libs/domain/src/facial-recognition/face.repository.ts b/server/libs/domain/src/facial-recognition/face.repository.ts new file mode 100644 index 0000000000000..e45171854ec40 --- /dev/null +++ b/server/libs/domain/src/facial-recognition/face.repository.ts @@ -0,0 +1,14 @@ +import { AssetFaceEntity } from '@app/infra/entities'; + +export const IFaceRepository = 'IFaceRepository'; + +export interface AssetFaceId { + assetId: string; + personId: string; +} + +export interface IFaceRepository { + getAll(): Promise; + getByIds(ids: AssetFaceId[]): Promise; + create(entity: Partial): Promise; +} diff --git a/server/libs/domain/src/facial-recognition/facial-recognition.service.spec.ts b/server/libs/domain/src/facial-recognition/facial-recognition.service.spec.ts new file mode 100644 index 0000000000000..0c1f62d811916 --- /dev/null +++ b/server/libs/domain/src/facial-recognition/facial-recognition.service.spec.ts @@ -0,0 +1,320 @@ +import { + assetEntityStub, + faceStub, + newAssetRepositoryMock, + newFaceRepositoryMock, + newJobRepositoryMock, + newMachineLearningRepositoryMock, + newMediaRepositoryMock, + newPersonRepositoryMock, + newSearchRepositoryMock, + newStorageRepositoryMock, + personStub, +} from '../../test'; +import { IAssetRepository, WithoutProperty } from '../asset'; +import { IJobRepository, JobName } from '../job'; +import { IMediaRepository } from '../media'; +import { IPersonRepository } from '../person'; +import { ISearchRepository } from '../search'; +import { IMachineLearningRepository } from '../smart-info'; +import { IStorageRepository } from '../storage'; +import { IFaceRepository } from './face.repository'; +import { FacialRecognitionService } from './facial-recognition.services'; + +const croppedFace = Buffer.from('Cropped Face'); + +const face = { + start: { + assetId: 'asset-1', + personId: 'person-1', + boundingBox: { + x1: 5, + y1: 5, + x2: 505, + y2: 505, + }, + imageHeight: 1000, + imageWidth: 1000, + }, + middle: { + assetId: 'asset-1', + personId: 'person-1', + boundingBox: { + x1: 100, + y1: 100, + x2: 200, + y2: 200, + }, + imageHeight: 500, + imageWidth: 400, + embedding: [1, 2, 3, 4], + score: 0.2, + }, + end: { + assetId: 'asset-1', + personId: 'person-1', + boundingBox: { + x1: 300, + y1: 300, + x2: 495, + y2: 495, + }, + imageHeight: 500, + imageWidth: 500, + }, +}; + +const faceSearch = { + noMatch: { + total: 0, + count: 0, + page: 1, + items: [], + distances: [], + facets: [], + }, + oneMatch: { + total: 1, + count: 1, + page: 1, + items: [faceStub.face1], + distances: [0.1], + facets: [], + }, + oneRemoteMatch: { + total: 1, + count: 1, + page: 1, + items: [faceStub.face1], + distances: [0.8], + facets: [], + }, +}; + +describe(FacialRecognitionService.name, () => { + let sut: FacialRecognitionService; + let assetMock: jest.Mocked; + let faceMock: jest.Mocked; + let jobMock: jest.Mocked; + let machineLearningMock: jest.Mocked; + let mediaMock: jest.Mocked; + let personMock: jest.Mocked; + let searchMock: jest.Mocked; + let storageMock: jest.Mocked; + + beforeEach(async () => { + assetMock = newAssetRepositoryMock(); + faceMock = newFaceRepositoryMock(); + jobMock = newJobRepositoryMock(); + machineLearningMock = newMachineLearningRepositoryMock(); + mediaMock = newMediaRepositoryMock(); + personMock = newPersonRepositoryMock(); + searchMock = newSearchRepositoryMock(); + storageMock = newStorageRepositoryMock(); + + mediaMock.crop.mockResolvedValue(croppedFace); + + sut = new FacialRecognitionService( + assetMock, + faceMock, + jobMock, + machineLearningMock, + mediaMock, + personMock, + searchMock, + storageMock, + ); + }); + + it('should be defined', () => { + expect(sut).toBeDefined(); + }); + + describe('handleQueueRecognizeFaces', () => { + it('should queue missing assets', async () => { + assetMock.getWithout.mockResolvedValue([assetEntityStub.image]); + await sut.handleQueueRecognizeFaces({}); + + expect(assetMock.getWithout).toHaveBeenCalledWith(WithoutProperty.FACES); + expect(jobMock.queue).toHaveBeenCalledWith({ + name: JobName.RECOGNIZE_FACES, + data: { asset: assetEntityStub.image }, + }); + }); + + it('should queue all assets', async () => { + assetMock.getAll.mockResolvedValue([assetEntityStub.image]); + personMock.deleteAll.mockResolvedValue(5); + searchMock.deleteAllFaces.mockResolvedValue(100); + + await sut.handleQueueRecognizeFaces({ force: true }); + + expect(assetMock.getAll).toHaveBeenCalled(); + expect(jobMock.queue).toHaveBeenCalledWith({ + name: JobName.RECOGNIZE_FACES, + data: { asset: assetEntityStub.image }, + }); + }); + + it('should log an error', async () => { + assetMock.getWithout.mockRejectedValue(new Error('Database unavailable')); + await sut.handleQueueRecognizeFaces({}); + }); + }); + + describe('handleRecognizeFaces', () => { + it('should skip when no resize path', async () => { + await sut.handleRecognizeFaces({ asset: assetEntityStub.noResizePath }); + expect(machineLearningMock.detectFaces).not.toHaveBeenCalled(); + }); + + it('should handle no results', async () => { + machineLearningMock.detectFaces.mockResolvedValue([]); + await sut.handleRecognizeFaces({ asset: assetEntityStub.image }); + expect(machineLearningMock.detectFaces).toHaveBeenCalledWith({ + thumbnailPath: assetEntityStub.image.resizePath, + }); + expect(faceMock.create).not.toHaveBeenCalled(); + expect(jobMock.queue).not.toHaveBeenCalled(); + }); + + it('should match existing people', async () => { + machineLearningMock.detectFaces.mockResolvedValue([face.middle]); + searchMock.searchFaces.mockResolvedValue(faceSearch.oneMatch); + + await sut.handleRecognizeFaces({ asset: assetEntityStub.image }); + + expect(faceMock.create).toHaveBeenCalledWith({ + personId: 'person-1', + assetId: 'asset-id', + embedding: [1, 2, 3, 4], + }); + expect(jobMock.queue.mock.calls).toEqual([ + [{ name: JobName.SEARCH_INDEX_FACE, data: { personId: 'person-1', assetId: 'asset-id' } }], + [{ name: JobName.SEARCH_INDEX_ASSET, data: { ids: ['asset-id'] } }], + ]); + }); + + it('should create a new person', async () => { + machineLearningMock.detectFaces.mockResolvedValue([face.middle]); + searchMock.searchFaces.mockResolvedValue(faceSearch.oneRemoteMatch); + personMock.create.mockResolvedValue(personStub.noName); + + await sut.handleRecognizeFaces({ asset: assetEntityStub.image }); + + expect(personMock.create).toHaveBeenCalledWith({ ownerId: assetEntityStub.image.ownerId }); + expect(faceMock.create).toHaveBeenCalledWith({ + personId: 'person-1', + assetId: 'asset-id', + embedding: [1, 2, 3, 4], + }); + expect(jobMock.queue.mock.calls).toEqual([ + [ + { + name: JobName.GENERATE_FACE_THUMBNAIL, + data: { + assetId: 'asset-1', + personId: 'person-1', + boundingBox: { + x1: 100, + y1: 100, + x2: 200, + y2: 200, + }, + imageHeight: 500, + imageWidth: 400, + score: 0.2, + }, + }, + ], + [{ name: JobName.SEARCH_INDEX_FACE, data: { personId: 'person-1', assetId: 'asset-id' } }], + [{ name: JobName.SEARCH_INDEX_ASSET, data: { ids: ['asset-id'] } }], + ]); + }); + + it('should log an error', async () => { + machineLearningMock.detectFaces.mockRejectedValue(new Error('machine learning unavailable')); + await sut.handleRecognizeFaces({ asset: assetEntityStub.image }); + }); + }); + + describe('handleGenerateFaceThumbnail', () => { + it('should skip an asset not found', async () => { + assetMock.getByIds.mockResolvedValue([]); + + await sut.handleGenerateFaceThumbnail(face.middle); + + expect(mediaMock.crop).not.toHaveBeenCalled(); + }); + + it('should skip an asset without a thumbnail', async () => { + assetMock.getByIds.mockResolvedValue([assetEntityStub.noResizePath]); + + await sut.handleGenerateFaceThumbnail(face.middle); + + expect(mediaMock.crop).not.toHaveBeenCalled(); + }); + + it('should generate a thumbnail', async () => { + assetMock.getByIds.mockResolvedValue([assetEntityStub.image]); + + await sut.handleGenerateFaceThumbnail(face.middle); + + expect(assetMock.getByIds).toHaveBeenCalledWith(['asset-1']); + expect(storageMock.mkdirSync).toHaveBeenCalledWith('upload/thumbs/user-id'); + expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/user-id/thumbs/path.ext', { + left: 95, + top: 95, + width: 110, + height: 110, + }); + expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/user-id/person-1.jpeg', { + format: 'jpeg', + size: 250, + }); + expect(personMock.update).toHaveBeenCalledWith({ + id: 'person-1', + thumbnailPath: 'upload/thumbs/user-id/person-1.jpeg', + }); + }); + + it('should generate a thumbnail without going negative', async () => { + assetMock.getByIds.mockResolvedValue([assetEntityStub.image]); + + await sut.handleGenerateFaceThumbnail(face.start); + + expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/user-id/thumbs/path.ext', { + left: 0, + top: 0, + width: 510, + height: 510, + }); + expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/user-id/person-1.jpeg', { + format: 'jpeg', + size: 250, + }); + }); + + it('should generate a thumbnail without overflowing', async () => { + assetMock.getByIds.mockResolvedValue([assetEntityStub.image]); + + await sut.handleGenerateFaceThumbnail(face.end); + + expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/user-id/thumbs/path.ext', { + left: 297, + top: 297, + width: 202, + height: 202, + }); + expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/user-id/person-1.jpeg', { + format: 'jpeg', + size: 250, + }); + }); + + it('should log an error', async () => { + assetMock.getByIds.mockRejectedValue(new Error('Database unavailable')); + await sut.handleGenerateFaceThumbnail(face.middle); + }); + }); +}); diff --git a/server/libs/domain/src/facial-recognition/facial-recognition.services.ts b/server/libs/domain/src/facial-recognition/facial-recognition.services.ts new file mode 100644 index 0000000000000..39d6b0ffbfbd0 --- /dev/null +++ b/server/libs/domain/src/facial-recognition/facial-recognition.services.ts @@ -0,0 +1,144 @@ +import { Inject, Logger } from '@nestjs/common'; +import { join } from 'path'; +import { IAssetRepository, WithoutProperty } from '../asset'; +import { MACHINE_LEARNING_ENABLED } from '../domain.constant'; +import { IAssetJob, IBaseJob, IFaceThumbnailJob, IJobRepository, JobName } from '../job'; +import { CropOptions, FACE_THUMBNAIL_SIZE, IMediaRepository } from '../media'; +import { IPersonRepository } from '../person/person.repository'; +import { ISearchRepository } from '../search/search.repository'; +import { IMachineLearningRepository } from '../smart-info'; +import { IStorageRepository, StorageCore, StorageFolder } from '../storage'; +import { AssetFaceId, IFaceRepository } from './face.repository'; + +export class FacialRecognitionService { + private logger = new Logger(FacialRecognitionService.name); + private storageCore = new StorageCore(); + + constructor( + @Inject(IAssetRepository) private assetRepository: IAssetRepository, + @Inject(IFaceRepository) private faceRepository: IFaceRepository, + @Inject(IJobRepository) private jobRepository: IJobRepository, + @Inject(IMachineLearningRepository) private machineLearning: IMachineLearningRepository, + @Inject(IMediaRepository) private mediaRepository: IMediaRepository, + @Inject(IPersonRepository) private personRepository: IPersonRepository, + @Inject(ISearchRepository) private searchRepository: ISearchRepository, + @Inject(IStorageRepository) private storageRepository: IStorageRepository, + ) {} + + async handleQueueRecognizeFaces({ force }: IBaseJob) { + try { + const assets = force + ? await this.assetRepository.getAll() + : await this.assetRepository.getWithout(WithoutProperty.FACES); + + if (force) { + const people = await this.personRepository.deleteAll(); + const faces = await this.searchRepository.deleteAllFaces(); + this.logger.debug(`Deleted ${people} people and ${faces} faces`); + } + for (const asset of assets) { + await this.jobRepository.queue({ name: JobName.RECOGNIZE_FACES, data: { asset } }); + } + } catch (error: any) { + this.logger.error(`Unable to queue recognize faces`, error?.stack); + } + } + + async handleRecognizeFaces(data: IAssetJob) { + const { asset } = data; + + if (!MACHINE_LEARNING_ENABLED || !asset.resizePath) { + return; + } + + try { + const faces = await this.machineLearning.detectFaces({ thumbnailPath: asset.resizePath }); + + this.logger.debug(`${faces.length} faces detected in ${asset.resizePath}`); + this.logger.verbose(faces.map((face) => ({ ...face, embedding: `float[${face.embedding.length}]` }))); + + for (const { embedding, ...rest } of faces) { + const faceSearchResult = await this.searchRepository.searchFaces(embedding, { ownerId: asset.ownerId }); + + let personId: string | null = null; + + // try to find a matching face and link to the associated person + // The closer to 0, the better the match. Range is from 0 to 2 + if (faceSearchResult.total && faceSearchResult.distances[0] < 0.6) { + this.logger.verbose(`Match face with distance ${faceSearchResult.distances[0]}`); + personId = faceSearchResult.items[0].personId; + } + + if (!personId) { + this.logger.debug('No matches, creating a new person.'); + const person = await this.personRepository.create({ ownerId: asset.ownerId }); + personId = person.id; + await this.jobRepository.queue({ + name: JobName.GENERATE_FACE_THUMBNAIL, + data: { assetId: asset.id, personId, ...rest }, + }); + } + + const faceId: AssetFaceId = { assetId: asset.id, personId }; + + await this.faceRepository.create({ ...faceId, embedding }); + await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_FACE, data: faceId }); + await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ASSET, data: { ids: [asset.id] } }); + } + + // queue all faces for asset + } catch (error: any) { + this.logger.error(`Unable run facial recognition pipeline: ${asset.id}`, error?.stack); + } + } + + async handleGenerateFaceThumbnail(data: IFaceThumbnailJob) { + const { assetId, personId, boundingBox, imageWidth, imageHeight } = data; + + try { + const [asset] = await this.assetRepository.getByIds([assetId]); + if (!asset || !asset.resizePath) { + this.logger.warn(`Asset not found for facial cropping: ${assetId}`); + return null; + } + + this.logger.verbose(`Cropping face for person: ${personId}`); + + const outputFolder = this.storageCore.getFolderLocation(StorageFolder.THUMBNAILS, asset.ownerId); + const output = join(outputFolder, `${personId}.jpeg`); + this.storageRepository.mkdirSync(outputFolder); + + const { x1, y1, x2, y2 } = boundingBox; + + const halfWidth = (x2 - x1) / 2; + const halfHeight = (y2 - y1) / 2; + + const middleX = Math.round(x1 + halfWidth); + const middleY = Math.round(y1 + halfHeight); + + // zoom out 10% + const targetHalfSize = Math.floor(Math.max(halfWidth, halfHeight) * 1.1); + + // get the longest distance from the center of the image without overflowing + const newHalfSize = Math.min( + middleX - Math.max(0, middleX - targetHalfSize), + middleY - Math.max(0, middleY - targetHalfSize), + Math.min(imageWidth - 1, middleX + targetHalfSize) - middleX, + Math.min(imageHeight - 1, middleY + targetHalfSize) - middleY, + ); + + const cropOptions: CropOptions = { + left: middleX - newHalfSize, + top: middleY - newHalfSize, + width: newHalfSize * 2, + height: newHalfSize * 2, + }; + + const croppedOutput = await this.mediaRepository.crop(asset.resizePath, cropOptions); + await this.mediaRepository.resize(croppedOutput, output, { size: FACE_THUMBNAIL_SIZE, format: 'jpeg' }); + await this.personRepository.update({ id: personId, thumbnailPath: output }); + } catch (error: Error | any) { + this.logger.error(`Failed to crop face for asset: ${assetId}, person: ${personId} - ${error}`, error.stack); + } + } +} diff --git a/server/libs/domain/src/facial-recognition/index.ts b/server/libs/domain/src/facial-recognition/index.ts new file mode 100644 index 0000000000000..affe817a025ca --- /dev/null +++ b/server/libs/domain/src/facial-recognition/index.ts @@ -0,0 +1,2 @@ +export * from './facial-recognition.services'; +export * from './face.repository'; diff --git a/server/libs/domain/src/index.ts b/server/libs/domain/src/index.ts index b5e846fe978c0..7ce05c6ba3409 100644 --- a/server/libs/domain/src/index.ts +++ b/server/libs/domain/src/index.ts @@ -8,10 +8,12 @@ export * from './domain.config'; export * from './domain.constant'; export * from './domain.module'; export * from './domain.util'; +export * from './facial-recognition'; export * from './job'; export * from './media'; export * from './metadata'; export * from './oauth'; +export * from './person'; export * from './search'; export * from './server-info'; export * from './partner'; diff --git a/server/libs/domain/src/job/job.constants.ts b/server/libs/domain/src/job/job.constants.ts index ab272fff243a3..1370a4bbe1836 100644 --- a/server/libs/domain/src/job/job.constants.ts +++ b/server/libs/domain/src/job/job.constants.ts @@ -3,6 +3,7 @@ export enum QueueName { METADATA_EXTRACTION = 'metadata-extraction-queue', VIDEO_CONVERSION = 'video-conversion-queue', OBJECT_TAGGING = 'object-tagging-queue', + RECOGNIZE_FACES = 'recognize-faces-queue', CLIP_ENCODING = 'clip-encoding-queue', BACKGROUND_TASK = 'background-task-queue', STORAGE_TEMPLATE_MIGRATION = 'storage-template-migration-queue', @@ -48,16 +49,25 @@ export enum JobName { DETECT_OBJECTS = 'detect-objects', CLASSIFY_IMAGE = 'classify-image', + // facial recognition + QUEUE_RECOGNIZE_FACES = 'queue-recognize-faces', + RECOGNIZE_FACES = 'recognize-faces', + GENERATE_FACE_THUMBNAIL = 'generate-face-thumbnail', + PERSON_CLEANUP = 'person-cleanup', + // cleanup DELETE_FILES = 'delete-files', // search SEARCH_INDEX_ASSETS = 'search-index-assets', SEARCH_INDEX_ASSET = 'search-index-asset', + SEARCH_INDEX_FACE = 'search-index-face', + SEARCH_INDEX_FACES = 'search-index-faces', SEARCH_INDEX_ALBUMS = 'search-index-albums', SEARCH_INDEX_ALBUM = 'search-index-album', SEARCH_REMOVE_ALBUM = 'search-remove-album', SEARCH_REMOVE_ASSET = 'search-remove-asset', + SEARCH_REMOVE_FACE = 'search-remove-face', // clip QUEUE_ENCODE_CLIP = 'queue-clip-encode', diff --git a/server/libs/domain/src/job/job.interface.ts b/server/libs/domain/src/job/job.interface.ts index 912afb0c3f11e..369724bec8538 100644 --- a/server/libs/domain/src/job/job.interface.ts +++ b/server/libs/domain/src/job/job.interface.ts @@ -1,4 +1,5 @@ import { AlbumEntity, AssetEntity, UserEntity } from '@app/infra/entities'; +import { BoundingBox } from '../smart-info'; export interface IBaseJob { force?: boolean; @@ -12,6 +13,19 @@ export interface IAssetJob extends IBaseJob { asset: AssetEntity; } +export interface IAssetFaceJob extends IBaseJob { + assetId: string; + personId: string; +} + +export interface IFaceThumbnailJob extends IAssetFaceJob { + imageWidth: number; + imageHeight: number; + boundingBox: BoundingBox; + assetId: string; + personId: string; +} + export interface IBulkEntityJob extends IBaseJob { ids: string[]; } diff --git a/server/libs/domain/src/job/job.repository.ts b/server/libs/domain/src/job/job.repository.ts index f4a0509627dd2..c09ff3c129501 100644 --- a/server/libs/domain/src/job/job.repository.ts +++ b/server/libs/domain/src/job/job.repository.ts @@ -1,10 +1,12 @@ import { JobName, QueueName } from './job.constants'; import { + IAssetFaceJob, IAssetJob, IAssetUploadedJob, IBaseJob, IBulkEntityJob, IDeleteFilesJob, + IFaceThumbnailJob, IUserDeletionJob, } from './job.interface'; @@ -54,6 +56,11 @@ export type JobItem = | { name: JobName.DETECT_OBJECTS; data: IAssetJob } | { name: JobName.CLASSIFY_IMAGE; data: IAssetJob } + // Recognize Faces + | { name: JobName.QUEUE_RECOGNIZE_FACES; data: IBaseJob } + | { name: JobName.RECOGNIZE_FACES; data: IAssetJob } + | { name: JobName.GENERATE_FACE_THUMBNAIL; data: IFaceThumbnailJob } + // Clip Embedding | { name: JobName.QUEUE_ENCODE_CLIP; data: IBaseJob } | { name: JobName.ENCODE_CLIP; data: IAssetJob } @@ -61,13 +68,19 @@ export type JobItem = // Filesystem | { name: JobName.DELETE_FILES; data: IDeleteFilesJob } + // Asset Deletion + | { name: JobName.PERSON_CLEANUP } + // Search | { name: JobName.SEARCH_INDEX_ASSETS } | { name: JobName.SEARCH_INDEX_ASSET; data: IBulkEntityJob } + | { name: JobName.SEARCH_INDEX_FACES } + | { name: JobName.SEARCH_INDEX_FACE; data: IAssetFaceJob } | { name: JobName.SEARCH_INDEX_ALBUMS } | { name: JobName.SEARCH_INDEX_ALBUM; data: IBulkEntityJob } | { name: JobName.SEARCH_REMOVE_ASSET; data: IBulkEntityJob } - | { name: JobName.SEARCH_REMOVE_ALBUM; data: IBulkEntityJob }; + | { name: JobName.SEARCH_REMOVE_ALBUM; data: IBulkEntityJob } + | { name: JobName.SEARCH_REMOVE_FACE; data: IAssetFaceJob }; export const IJobRepository = 'IJobRepository'; diff --git a/server/libs/domain/src/job/job.service.spec.ts b/server/libs/domain/src/job/job.service.spec.ts index bb3f62dc0a681..5b95ecaa0828b 100644 --- a/server/libs/domain/src/job/job.service.spec.ts +++ b/server/libs/domain/src/job/job.service.spec.ts @@ -15,6 +15,17 @@ describe(JobService.name, () => { expect(sut).toBeDefined(); }); + describe('handleNightlyJobs', () => { + it('should run the scheduled jobs', async () => { + await sut.handleNightlyJobs(); + + expect(jobMock.queue.mock.calls).toEqual([ + [{ name: JobName.USER_DELETE_CHECK }], + [{ name: JobName.PERSON_CLEANUP }], + ]); + }); + }); + describe('getAllJobStatus', () => { it('should get all job statuses', async () => { jobMock.getJobCounts.mockResolvedValue({ @@ -54,6 +65,7 @@ describe(JobService.name, () => { 'storage-template-migration-queue': expectedJobStatus, 'thumbnail-generation-queue': expectedJobStatus, 'video-conversion-queue': expectedJobStatus, + 'recognize-faces-queue': expectedJobStatus, }); }); }); diff --git a/server/libs/domain/src/job/job.service.ts b/server/libs/domain/src/job/job.service.ts index fcf96968226ef..26824810aa7bb 100644 --- a/server/libs/domain/src/job/job.service.ts +++ b/server/libs/domain/src/job/job.service.ts @@ -11,6 +11,11 @@ export class JobService { constructor(@Inject(IJobRepository) private jobRepository: IJobRepository) {} + async handleNightlyJobs() { + await this.jobRepository.queue({ name: JobName.USER_DELETE_CHECK }); + await this.jobRepository.queue({ name: JobName.PERSON_CLEANUP }); + } + handleCommand(queueName: QueueName, dto: JobCommandDto): Promise { this.logger.debug(`Handling command: queue=${queueName},force=${dto.force}`); @@ -73,6 +78,9 @@ export class JobService { case QueueName.THUMBNAIL_GENERATION: return this.jobRepository.queue({ name: JobName.QUEUE_GENERATE_THUMBNAILS, data: { force } }); + case QueueName.RECOGNIZE_FACES: + return this.jobRepository.queue({ name: JobName.QUEUE_RECOGNIZE_FACES, data: { force } }); + default: throw new BadRequestException(`Invalid job name: ${name}`); } diff --git a/server/libs/domain/src/job/response-dto/all-job-status-response.dto.ts b/server/libs/domain/src/job/response-dto/all-job-status-response.dto.ts index 5004923115b9e..a988bbfcc28c7 100644 --- a/server/libs/domain/src/job/response-dto/all-job-status-response.dto.ts +++ b/server/libs/domain/src/job/response-dto/all-job-status-response.dto.ts @@ -53,4 +53,7 @@ export class AllJobStatusResponseDto implements Record @ApiProperty({ type: JobStatusDto }) [QueueName.SEARCH]!: JobStatusDto; + + @ApiProperty({ type: JobStatusDto }) + [QueueName.RECOGNIZE_FACES]!: JobStatusDto; } diff --git a/server/libs/domain/src/media/index.ts b/server/libs/domain/src/media/index.ts index 5ab3597d7031f..ad52a4afbfd69 100644 --- a/server/libs/domain/src/media/index.ts +++ b/server/libs/domain/src/media/index.ts @@ -1,2 +1,3 @@ +export * from './media.constant'; export * from './media.repository'; export * from './media.service'; diff --git a/server/libs/domain/src/media/media.constant.ts b/server/libs/domain/src/media/media.constant.ts new file mode 100644 index 0000000000000..97dd3f1d72c73 --- /dev/null +++ b/server/libs/domain/src/media/media.constant.ts @@ -0,0 +1,3 @@ +export const JPEG_THUMBNAIL_SIZE = 1440; +export const WEBP_THUMBNAIL_SIZE = 250; +export const FACE_THUMBNAIL_SIZE = 250; diff --git a/server/libs/domain/src/media/media.repository.ts b/server/libs/domain/src/media/media.repository.ts index ff2c2e0456cc4..bcb63ccb40091 100644 --- a/server/libs/domain/src/media/media.repository.ts +++ b/server/libs/domain/src/media/media.repository.ts @@ -31,10 +31,18 @@ export interface VideoInfo { audioStreams: AudioStreamInfo[]; } +export interface CropOptions { + top: number; + left: number; + width: number; + height: number; +} + export interface IMediaRepository { // image extractThumbnailFromExif(input: string, output: string): Promise; - resize(input: string, output: string, options: ResizeOptions): Promise; + resize(input: string | Buffer, output: string, options: ResizeOptions): Promise; + crop(input: string, options: CropOptions): Promise; // video extractVideoThumbnail(input: string, output: string, size: number): Promise; diff --git a/server/libs/domain/src/media/media.service.spec.ts b/server/libs/domain/src/media/media.service.spec.ts index 0697581770dcb..8ff0b3ce69487 100644 --- a/server/libs/domain/src/media/media.service.spec.ts +++ b/server/libs/domain/src/media/media.service.spec.ts @@ -34,6 +34,7 @@ describe(MediaService.name, () => { jobMock = newJobRepositoryMock(); mediaMock = newMediaRepositoryMock(); storageMock = newStorageRepositoryMock(); + sut = new MediaService(assetMock, communicationMock, jobMock, mediaMock, storageMock, configMock); }); diff --git a/server/libs/domain/src/media/media.service.ts b/server/libs/domain/src/media/media.service.ts index 81cae61fd4254..dbf7fc6b0a0b3 100644 --- a/server/libs/domain/src/media/media.service.ts +++ b/server/libs/domain/src/media/media.service.ts @@ -7,6 +7,7 @@ import { IAssetJob, IBaseJob, IJobRepository, JobName } from '../job'; import { IStorageRepository, StorageCore, StorageFolder } from '../storage'; import { ISystemConfigRepository, SystemConfigFFmpegDto } from '../system-config'; import { SystemConfigCore } from '../system-config/system-config.core'; +import { JPEG_THUMBNAIL_SIZE, WEBP_THUMBNAIL_SIZE } from './media.constant'; import { AudioStreamInfo, IMediaRepository, VideoStreamInfo } from './media.repository'; @Injectable() @@ -57,11 +58,10 @@ export class MediaService { this.storageRepository.mkdirSync(resizePath); const jpegThumbnailPath = join(resizePath, `${asset.id}.jpeg`); - const thumbnailDimension = 1440; if (asset.type == AssetType.IMAGE) { try { await this.mediaRepository.resize(asset.originalPath, jpegThumbnailPath, { - size: thumbnailDimension, + size: JPEG_THUMBNAIL_SIZE, format: 'jpeg', }); } catch (error) { @@ -74,7 +74,7 @@ export class MediaService { if (asset.type == AssetType.VIDEO) { this.logger.log('Start Generating Video Thumbnail'); - await this.mediaRepository.extractVideoThumbnail(asset.originalPath, jpegThumbnailPath, thumbnailDimension); + await this.mediaRepository.extractVideoThumbnail(asset.originalPath, jpegThumbnailPath, JPEG_THUMBNAIL_SIZE); this.logger.log(`Generating Video Thumbnail Success ${asset.id}`); } @@ -86,6 +86,7 @@ export class MediaService { await this.jobRepository.queue({ name: JobName.CLASSIFY_IMAGE, data: { asset } }); await this.jobRepository.queue({ name: JobName.DETECT_OBJECTS, data: { asset } }); await this.jobRepository.queue({ name: JobName.ENCODE_CLIP, data: { asset } }); + await this.jobRepository.queue({ name: JobName.RECOGNIZE_FACES, data: { asset } }); this.communicationRepository.send(CommunicationEvent.UPLOAD_SUCCESS, asset.ownerId, mapAsset(asset)); } catch (error: any) { @@ -103,7 +104,7 @@ export class MediaService { const webpPath = asset.resizePath.replace('jpeg', 'webp'); try { - await this.mediaRepository.resize(asset.resizePath, webpPath, { size: 250, format: 'webp' }); + await this.mediaRepository.resize(asset.resizePath, webpPath, { size: WEBP_THUMBNAIL_SIZE, format: 'webp' }); await this.assetRepository.save({ id: asset.id, webpPath: webpPath }); } catch (error: any) { this.logger.error(`Failed to generate webp thumbnail for asset: ${asset.id}`, error.stack); diff --git a/server/libs/domain/src/person/dto/index.ts b/server/libs/domain/src/person/dto/index.ts new file mode 100644 index 0000000000000..5668429a29f85 --- /dev/null +++ b/server/libs/domain/src/person/dto/index.ts @@ -0,0 +1 @@ +export * from './person-update.dto'; diff --git a/server/libs/domain/src/person/dto/person-update.dto.ts b/server/libs/domain/src/person/dto/person-update.dto.ts new file mode 100644 index 0000000000000..72312378ca022 --- /dev/null +++ b/server/libs/domain/src/person/dto/person-update.dto.ts @@ -0,0 +1,7 @@ +import { IsNotEmpty, IsString } from 'class-validator'; + +export class PersonUpdateDto { + @IsNotEmpty() + @IsString() + name!: string; +} diff --git a/server/libs/domain/src/person/index.ts b/server/libs/domain/src/person/index.ts new file mode 100644 index 0000000000000..02a38887b7a81 --- /dev/null +++ b/server/libs/domain/src/person/index.ts @@ -0,0 +1,4 @@ +export * from './dto'; +export * from './person.repository'; +export * from './person.service'; +export * from './response-dto'; diff --git a/server/libs/domain/src/person/person.repository.ts b/server/libs/domain/src/person/person.repository.ts new file mode 100644 index 0000000000000..a9ae9dcbeaf6a --- /dev/null +++ b/server/libs/domain/src/person/person.repository.ts @@ -0,0 +1,19 @@ +import { AssetEntity, PersonEntity } from '@app/infra/entities'; + +export const IPersonRepository = 'IPersonRepository'; + +export interface PersonSearchOptions { + minimumFaceCount: number; +} + +export interface IPersonRepository { + getAll(userId: string, options: PersonSearchOptions): Promise; + getAllWithoutFaces(): Promise; + getById(userId: string, personId: string): Promise; + getAssets(userId: string, id: string): Promise; + + create(entity: Partial): Promise; + update(entity: Partial): Promise; + delete(entity: PersonEntity): Promise; + deleteAll(): Promise; +} diff --git a/server/libs/domain/src/person/person.service.spec.ts b/server/libs/domain/src/person/person.service.spec.ts new file mode 100644 index 0000000000000..d0011283cdb6a --- /dev/null +++ b/server/libs/domain/src/person/person.service.spec.ts @@ -0,0 +1,135 @@ +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { IJobRepository, JobName } from '..'; +import { + assetEntityStub, + authStub, + newJobRepositoryMock, + newPersonRepositoryMock, + newStorageRepositoryMock, + personStub, +} from '../../test'; +import { IStorageRepository } from '../storage'; +import { IPersonRepository } from './person.repository'; +import { PersonService } from './person.service'; +import { PersonResponseDto } from './response-dto'; + +const responseDto: PersonResponseDto = { + id: 'person-1', + name: 'Person 1', + thumbnailPath: '/path/to/thumbnail', +}; + +describe(PersonService.name, () => { + let sut: PersonService; + let personMock: jest.Mocked; + let storageMock: jest.Mocked; + let jobMock: jest.Mocked; + + beforeEach(async () => { + personMock = newPersonRepositoryMock(); + storageMock = newStorageRepositoryMock(); + jobMock = newJobRepositoryMock(); + sut = new PersonService(personMock, storageMock, jobMock); + }); + + it('should be defined', () => { + expect(sut).toBeDefined(); + }); + + describe('getAll', () => { + it('should get all people with thumbnails', async () => { + personMock.getAll.mockResolvedValue([personStub.withName, personStub.noThumbnail]); + await expect(sut.getAll(authStub.admin)).resolves.toEqual([responseDto]); + expect(personMock.getAll).toHaveBeenCalledWith(authStub.admin.id, { minimumFaceCount: 1 }); + }); + }); + + describe('getById', () => { + it('should throw a bad request when person is not found', async () => { + personMock.getById.mockResolvedValue(null); + await expect(sut.getById(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException); + }); + + it('should get a person by id', async () => { + personMock.getById.mockResolvedValue(personStub.withName); + await expect(sut.getById(authStub.admin, 'person-1')).resolves.toEqual(responseDto); + expect(personMock.getById).toHaveBeenCalledWith(authStub.admin.id, 'person-1'); + }); + }); + + describe('getThumbnail', () => { + it('should throw an error when personId is invalid', async () => { + personMock.getById.mockResolvedValue(null); + await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(NotFoundException); + expect(storageMock.createReadStream).not.toHaveBeenCalled(); + }); + + it('should throw an error when person has no thumbnail', async () => { + personMock.getById.mockResolvedValue(personStub.noThumbnail); + await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(NotFoundException); + expect(storageMock.createReadStream).not.toHaveBeenCalled(); + }); + + it('should serve the thumbnail', async () => { + personMock.getById.mockResolvedValue(personStub.noName); + await sut.getThumbnail(authStub.admin, 'person-1'); + expect(storageMock.createReadStream).toHaveBeenCalledWith('/path/to/thumbnail', 'image/jpeg'); + }); + }); + + describe('getAssets', () => { + it("should return a person's assets", async () => { + personMock.getAssets.mockResolvedValue([assetEntityStub.image, assetEntityStub.video]); + await sut.getAssets(authStub.admin, 'person-1'); + expect(personMock.getAssets).toHaveBeenCalledWith('admin_id', 'person-1'); + }); + }); + + describe('update', () => { + it('should throw an error when personId is invalid', async () => { + personMock.getById.mockResolvedValue(null); + await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).rejects.toBeInstanceOf( + BadRequestException, + ); + expect(personMock.update).not.toHaveBeenCalled(); + }); + + it("should update a person's name", async () => { + personMock.getById.mockResolvedValue(personStub.noName); + personMock.update.mockResolvedValue(personStub.withName); + personMock.getAssets.mockResolvedValue([assetEntityStub.image]); + + await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).resolves.toEqual(responseDto); + + expect(personMock.getById).toHaveBeenCalledWith('admin_id', 'person-1'); + expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', name: 'Person 1' }); + expect(jobMock.queue).toHaveBeenCalledWith({ + name: JobName.SEARCH_INDEX_ASSET, + data: { ids: [assetEntityStub.image.id] }, + }); + }); + }); + + describe('handlePersonCleanup', () => { + it('should delete people without faces', async () => { + personMock.getAllWithoutFaces.mockResolvedValue([personStub.noName]); + + await sut.handlePersonCleanup(); + + expect(personMock.delete).toHaveBeenCalledWith(personStub.noName); + expect(jobMock.queue).toHaveBeenCalledWith({ + name: JobName.DELETE_FILES, + data: { files: ['/path/to/thumbnail'] }, + }); + }); + + it('should log an error', async () => { + personMock.getAllWithoutFaces.mockResolvedValue([personStub.noName]); + personMock.delete.mockRejectedValue(new Error('database unavailable')); + + await sut.handlePersonCleanup(); + + expect(jobMock.queue).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/server/libs/domain/src/person/person.service.ts b/server/libs/domain/src/person/person.service.ts new file mode 100644 index 0000000000000..80618e4524f5c --- /dev/null +++ b/server/libs/domain/src/person/person.service.ts @@ -0,0 +1,82 @@ +import { BadRequestException, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common'; +import { AssetResponseDto, mapAsset } from '../asset'; +import { AuthUserDto } from '../auth'; +import { IJobRepository, JobName } from '../job'; +import { ImmichReadStream, IStorageRepository } from '../storage'; +import { PersonUpdateDto } from './dto'; +import { IPersonRepository } from './person.repository'; +import { mapPerson, PersonResponseDto } from './response-dto'; + +@Injectable() +export class PersonService { + readonly logger = new Logger(PersonService.name); + + constructor( + @Inject(IPersonRepository) private repository: IPersonRepository, + @Inject(IStorageRepository) private storageRepository: IStorageRepository, + @Inject(IJobRepository) private jobRepository: IJobRepository, + ) {} + + async getAll(authUser: AuthUserDto): Promise { + const people = await this.repository.getAll(authUser.id, { minimumFaceCount: 1 }); + const named = people.filter((person) => !!person.name); + const unnamed = people.filter((person) => !person.name); + return ( + [...named, ...unnamed] + // with thumbnails + .filter((person) => !!person.thumbnailPath) + .map((person) => mapPerson(person)) + ); + } + + async getById(authUser: AuthUserDto, personId: string): Promise { + const person = await this.repository.getById(authUser.id, personId); + if (!person) { + throw new BadRequestException(); + } + + return mapPerson(person); + } + + async getThumbnail(authUser: AuthUserDto, personId: string): Promise { + const person = await this.repository.getById(authUser.id, personId); + if (!person || !person.thumbnailPath) { + throw new NotFoundException(); + } + + return this.storageRepository.createReadStream(person.thumbnailPath, 'image/jpeg'); + } + + async getAssets(authUser: AuthUserDto, personId: string): Promise { + const assets = await this.repository.getAssets(authUser.id, personId); + return assets.map(mapAsset); + } + + async update(authUser: AuthUserDto, personId: string, dto: PersonUpdateDto): Promise { + const exists = await this.repository.getById(authUser.id, personId); + if (!exists) { + throw new BadRequestException(); + } + + const person = await this.repository.update({ id: personId, name: dto.name }); + + const relatedAsset = await this.getAssets(authUser, personId); + const assetIds = relatedAsset.map((asset) => asset.id); + await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ASSET, data: { ids: assetIds } }); + + return mapPerson(person); + } + + async handlePersonCleanup(): Promise { + const people = await this.repository.getAllWithoutFaces(); + for (const person of people) { + this.logger.debug(`Person ${person.name || person.id} no longer has any faces, deleting.`); + try { + await this.repository.delete(person); + await this.jobRepository.queue({ name: JobName.DELETE_FILES, data: { files: [person.thumbnailPath] } }); + } catch (error: Error | any) { + this.logger.error(`Unable to delete person: ${error}`, error?.stack); + } + } + } +} diff --git a/server/libs/domain/src/person/response-dto/index.ts b/server/libs/domain/src/person/response-dto/index.ts new file mode 100644 index 0000000000000..0e48d9b0e6538 --- /dev/null +++ b/server/libs/domain/src/person/response-dto/index.ts @@ -0,0 +1 @@ +export * from './person-response.dto'; diff --git a/server/libs/domain/src/person/response-dto/person-response.dto.ts b/server/libs/domain/src/person/response-dto/person-response.dto.ts new file mode 100644 index 0000000000000..c5e4a221ce923 --- /dev/null +++ b/server/libs/domain/src/person/response-dto/person-response.dto.ts @@ -0,0 +1,19 @@ +import { AssetFaceEntity, PersonEntity } from '@app/infra/entities'; + +export class PersonResponseDto { + id!: string; + name!: string; + thumbnailPath!: string; +} + +export function mapPerson(person: PersonEntity): PersonResponseDto { + return { + id: person.id, + name: person.name, + thumbnailPath: person.thumbnailPath, + }; +} + +export function mapFace(face: AssetFaceEntity): PersonResponseDto { + return mapPerson(face.person); +} diff --git a/server/libs/domain/src/search/search.repository.ts b/server/libs/domain/src/search/search.repository.ts index 9878c825d022f..78885bff0066f 100644 --- a/server/libs/domain/src/search/search.repository.ts +++ b/server/libs/domain/src/search/search.repository.ts @@ -1,8 +1,9 @@ -import { AlbumEntity, AssetEntity, AssetType } from '@app/infra/entities'; +import { AlbumEntity, AssetEntity, AssetFaceEntity, AssetType } from '@app/infra/entities'; export enum SearchCollection { ASSETS = 'assets', ALBUMS = 'albums', + FACES = 'faces', } export enum SearchStrategy { @@ -10,6 +11,10 @@ export enum SearchStrategy { TEXT = 'TEXT', } +export interface SearchFaceFilter { + ownerId: string; +} + export interface SearchFilter { id?: string; userId: string; @@ -37,6 +42,8 @@ export interface SearchResult { page: number; /** items for page */ items: T[]; + /** score */ + distances: number[]; facets: SearchFacet[]; } @@ -56,6 +63,13 @@ export interface SearchExploreItem { }>; } +export type OwnedFaceEntity = Pick & { + /** computed as assetId|personId */ + id: string; + /** copied from asset.id */ + ownerId: string; +}; + export type SearchCollectionIndexStatus = Record; export const ISearchRepository = 'ISearchRepository'; @@ -66,13 +80,17 @@ export interface ISearchRepository { importAlbums(items: AlbumEntity[], done: boolean): Promise; importAssets(items: AssetEntity[], done: boolean): Promise; + importFaces(items: OwnedFaceEntity[], done: boolean): Promise; deleteAlbums(ids: string[]): Promise; deleteAssets(ids: string[]): Promise; + deleteFaces(ids: string[]): Promise; + deleteAllFaces(): Promise; searchAlbums(query: string, filters: SearchFilter): Promise>; searchAssets(query: string, filters: SearchFilter): Promise>; vectorSearch(query: number[], filters: SearchFilter): Promise>; + searchFaces(query: number[], filters: SearchFaceFilter): Promise>; explore(userId: string): Promise[]>; } diff --git a/server/libs/domain/src/search/search.service.spec.ts b/server/libs/domain/src/search/search.service.spec.ts index 72dc61449f4ef..306f33777d2c1 100644 --- a/server/libs/domain/src/search/search.service.spec.ts +++ b/server/libs/domain/src/search/search.service.spec.ts @@ -6,8 +6,10 @@ import { assetEntityStub, asyncTick, authStub, + faceStub, newAlbumRepositoryMock, newAssetRepositoryMock, + newFaceRepositoryMock, newJobRepositoryMock, newMachineLearningRepositoryMock, newSearchRepositoryMock, @@ -15,6 +17,7 @@ import { } from '../../test'; import { IAlbumRepository } from '../album/album.repository'; import { IAssetRepository } from '../asset/asset.repository'; +import { IFaceRepository } from '../facial-recognition'; import { JobName } from '../job'; import { IJobRepository } from '../job/job.repository'; import { IMachineLearningRepository } from '../smart-info'; @@ -28,20 +31,29 @@ describe(SearchService.name, () => { let sut: SearchService; let albumMock: jest.Mocked; let assetMock: jest.Mocked; + let faceMock: jest.Mocked; let jobMock: jest.Mocked; let machineMock: jest.Mocked; let searchMock: jest.Mocked; let configMock: jest.Mocked; + const makeSut = (value?: string) => { + if (value) { + configMock.get.mockReturnValue(value); + } + return new SearchService(albumMock, assetMock, faceMock, jobMock, machineMock, searchMock, configMock); + }; + beforeEach(() => { albumMock = newAlbumRepositoryMock(); assetMock = newAssetRepositoryMock(); + faceMock = newFaceRepositoryMock(); jobMock = newJobRepositoryMock(); machineMock = newMachineLearningRepositoryMock(); searchMock = newSearchRepositoryMock(); configMock = { get: jest.fn() } as unknown as jest.Mocked; - sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + sut = makeSut(); }); afterEach(() => { @@ -80,8 +92,7 @@ describe(SearchService.name, () => { }); it('should be disabled via an env variable', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); expect(sut.isEnabled()).toBe(false); }); @@ -93,8 +104,7 @@ describe(SearchService.name, () => { }); it('should return the config when search is disabled', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); expect(sut.getConfig()).toEqual({ enabled: false }); }); @@ -102,8 +112,7 @@ describe(SearchService.name, () => { describe(`bootstrap`, () => { it('should skip when search is disabled', async () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); await sut.bootstrap(); @@ -115,7 +124,7 @@ describe(SearchService.name, () => { }); it('should skip schema migration if not needed', async () => { - searchMock.checkMigrationStatus.mockResolvedValue({ assets: false, albums: false }); + searchMock.checkMigrationStatus.mockResolvedValue({ assets: false, albums: false, faces: false }); await sut.bootstrap(); expect(searchMock.setup).toHaveBeenCalled(); @@ -123,21 +132,21 @@ describe(SearchService.name, () => { }); it('should do schema migration if needed', async () => { - searchMock.checkMigrationStatus.mockResolvedValue({ assets: true, albums: true }); + searchMock.checkMigrationStatus.mockResolvedValue({ assets: true, albums: true, faces: true }); await sut.bootstrap(); expect(searchMock.setup).toHaveBeenCalled(); expect(jobMock.queue.mock.calls).toEqual([ [{ name: JobName.SEARCH_INDEX_ASSETS }], [{ name: JobName.SEARCH_INDEX_ALBUMS }], + [{ name: JobName.SEARCH_INDEX_FACES }], ]); }); }); describe('search', () => { it('should throw an error is search is disabled', async () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); await expect(sut.search(authStub.admin, {})).rejects.toBeInstanceOf(BadRequestException); @@ -157,6 +166,7 @@ describe(SearchService.name, () => { page: 1, items: [], facets: [], + distances: [], }, assets: { total: 0, @@ -164,6 +174,7 @@ describe(SearchService.name, () => { page: 1, items: [], facets: [], + distances: [], }, }); @@ -202,8 +213,7 @@ describe(SearchService.name, () => { }); it('should skip if search is disabled', async () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); await sut.handleIndexAssets(); @@ -214,8 +224,7 @@ describe(SearchService.name, () => { describe('handleIndexAsset', () => { it('should skip if search is disabled', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); sut.handleIndexAsset({ ids: [assetEntityStub.image.id] }); }); @@ -226,8 +235,7 @@ describe(SearchService.name, () => { describe('handleIndexAlbums', () => { it('should skip if search is disabled', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); sut.handleIndexAlbums(); }); @@ -251,8 +259,7 @@ describe(SearchService.name, () => { describe('handleIndexAlbum', () => { it('should skip if search is disabled', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); sut.handleIndexAlbum({ ids: [albumStub.empty.id] }); }); @@ -263,8 +270,7 @@ describe(SearchService.name, () => { describe('handleRemoveAlbum', () => { it('should skip if search is disabled', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); sut.handleRemoveAlbum({ ids: ['album1'] }); }); @@ -275,8 +281,7 @@ describe(SearchService.name, () => { describe('handleRemoveAsset', () => { it('should skip if search is disabled', () => { - configMock.get.mockReturnValue('false'); - const sut = new SearchService(albumMock, assetMock, jobMock, machineMock, searchMock, configMock); + const sut = makeSut('false'); sut.handleRemoveAsset({ ids: ['asset1'] }); }); @@ -285,6 +290,84 @@ describe(SearchService.name, () => { }); }); + describe('handleIndexFaces', () => { + it('should call done, even when there are no faces', async () => { + faceMock.getAll.mockResolvedValue([]); + + await sut.handleIndexFaces(); + + expect(searchMock.importFaces).toHaveBeenCalledWith([], true); + }); + + it('should index all the faces', async () => { + faceMock.getAll.mockResolvedValue([faceStub.face1]); + + await sut.handleIndexFaces(); + + expect(searchMock.importFaces.mock.calls).toEqual([ + [ + [ + { + id: 'asset-id|person-1', + ownerId: 'user-id', + assetId: 'asset-id', + personId: 'person-1', + embedding: [1, 2, 3, 4], + }, + ], + false, + ], + [[], true], + ]); + }); + + it('should log an error', async () => { + faceMock.getAll.mockResolvedValue([faceStub.face1]); + searchMock.importFaces.mockRejectedValue(new Error('import failed')); + + await sut.handleIndexFaces(); + + expect(searchMock.importFaces).toHaveBeenCalled(); + }); + + it('should skip if search is disabled', async () => { + const sut = makeSut('false'); + + await sut.handleIndexFaces(); + + expect(searchMock.importFaces).not.toHaveBeenCalled(); + }); + }); + + describe('handleIndexAsset', () => { + it('should skip if search is disabled', () => { + const sut = makeSut('false'); + sut.handleIndexFace({ assetId: 'asset-1', personId: 'person-1' }); + + expect(searchMock.importFaces).not.toHaveBeenCalled(); + expect(faceMock.getByIds).not.toHaveBeenCalled(); + }); + + it('should index the face', () => { + faceMock.getByIds.mockResolvedValue([faceStub.face1]); + + sut.handleIndexFace({ assetId: 'asset-1', personId: 'person-1' }); + + expect(faceMock.getByIds).toHaveBeenCalledWith([{ assetId: 'asset-1', personId: 'person-1' }]); + }); + }); + + describe('handleRemoveFace', () => { + it('should skip if search is disabled', () => { + const sut = makeSut('false'); + sut.handleRemoveFace({ assetId: 'asset-1', personId: 'person-1' }); + }); + + it('should remove the face', () => { + sut.handleRemoveFace({ assetId: 'asset-1', personId: 'person-1' }); + }); + }); + describe('flush', () => { it('should flush queued album updates', async () => { albumMock.getByIds.mockResolvedValue([albumStub.empty]); diff --git a/server/libs/domain/src/search/search.service.ts b/server/libs/domain/src/search/search.service.ts index f026a0695adc6..be1a9c46744e2 100644 --- a/server/libs/domain/src/search/search.service.ts +++ b/server/libs/domain/src/search/search.service.ts @@ -1,4 +1,4 @@ -import { AlbumEntity, AssetEntity } from '@app/infra/entities'; +import { AlbumEntity, AssetEntity, AssetFaceEntity } from '@app/infra/entities'; import { BadRequestException, Inject, Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { mapAlbum } from '../album'; @@ -7,12 +7,14 @@ import { mapAsset } from '../asset'; import { IAssetRepository } from '../asset/asset.repository'; import { AuthUserDto } from '../auth'; import { MACHINE_LEARNING_ENABLED } from '../domain.constant'; -import { IBulkEntityJob, IJobRepository, JobName } from '../job'; +import { AssetFaceId, IFaceRepository } from '../facial-recognition'; +import { IAssetFaceJob, IBulkEntityJob, IJobRepository, JobName } from '../job'; import { IMachineLearningRepository } from '../smart-info'; import { SearchDto } from './dto'; import { SearchConfigResponseDto, SearchResponseDto } from './response-dto'; import { ISearchRepository, + OwnedFaceEntity, SearchCollection, SearchExploreItem, SearchResult, @@ -40,9 +42,15 @@ export class SearchService { delete: new Set(), }; + private faceQueue: SyncQueue = { + upsert: new Set(), + delete: new Set(), + }; + constructor( @Inject(IAlbumRepository) private albumRepository: IAlbumRepository, @Inject(IAssetRepository) private assetRepository: IAssetRepository, + @Inject(IFaceRepository) private faceRepository: IFaceRepository, @Inject(IJobRepository) private jobRepository: IJobRepository, @Inject(IMachineLearningRepository) private machineLearning: IMachineLearningRepository, @Inject(ISearchRepository) private searchRepository: ISearchRepository, @@ -88,6 +96,10 @@ export class SearchService { this.logger.debug('Queueing job to re-index all albums'); await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ALBUMS }); } + if (migrationStatus[SearchCollection.FACES]) { + this.logger.debug('Queueing job to re-index all faces'); + await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_FACES }); + } } async getExploreData(authUser: AuthUserDto): Promise[]> { @@ -159,6 +171,29 @@ export class SearchService { } } + async handleIndexFaces() { + if (!this.enabled) { + return; + } + + try { + // TODO: do this in batches based on searchIndexVersion + const faces = this.patchFaces(await this.faceRepository.getAll()); + this.logger.log(`Indexing ${faces.length} faces`); + + const chunkSize = 1000; + for (let i = 0; i < faces.length; i += chunkSize) { + await this.searchRepository.importFaces(faces.slice(i, i + chunkSize), false); + } + + await this.searchRepository.importFaces([], true); + + this.logger.debug('Finished re-indexing all faces'); + } catch (error: any) { + this.logger.error(`Unable to index all faces`, error?.stack); + } + } + handleIndexAlbum({ ids }: IBulkEntityJob) { if (!this.enabled) { return; @@ -179,6 +214,15 @@ export class SearchService { } } + async handleIndexFace({ assetId, personId }: IAssetFaceJob) { + if (!this.enabled) { + return; + } + + // immediately push to typesense + await this.searchRepository.importFaces(await this.idsToFaces([{ assetId, personId }]), false); + } + handleRemoveAlbum({ ids }: IBulkEntityJob) { if (!this.enabled) { return; @@ -199,6 +243,14 @@ export class SearchService { } } + handleRemoveFace({ assetId, personId }: IAssetFaceJob) { + if (!this.enabled) { + return; + } + + this.faceQueue.delete.add(this.asKey({ assetId, personId })); + } + private async flush() { if (this.albumQueue.upsert.size > 0) { const ids = [...this.albumQueue.upsert.keys()]; @@ -229,6 +281,21 @@ export class SearchService { await this.searchRepository.deleteAssets(ids); this.assetQueue.delete.clear(); } + + if (this.faceQueue.upsert.size > 0) { + const ids = [...this.faceQueue.upsert.keys()].map((key) => this.asParts(key)); + const items = await this.idsToFaces(ids); + this.logger.debug(`Flushing ${items.length} face upserts`); + await this.searchRepository.importFaces(items, false); + this.faceQueue.upsert.clear(); + } + + if (this.faceQueue.delete.size > 0) { + const ids = [...this.faceQueue.delete.keys()]; + this.logger.debug(`Flushing ${ids.length} face deletes`); + await this.searchRepository.deleteFaces(ids); + this.faceQueue.delete.clear(); + } } private assertEnabled() { @@ -247,6 +314,10 @@ export class SearchService { return this.patchAssets(entities.filter((entity) => entity.isVisible)); } + private async idsToFaces(ids: AssetFaceId[]): Promise { + return this.patchFaces(await this.faceRepository.getByIds(ids)); + } + private patchAssets(assets: AssetEntity[]): AssetEntity[] { return assets; } @@ -254,4 +325,23 @@ export class SearchService { private patchAlbums(albums: AlbumEntity[]): AlbumEntity[] { return albums.map((entity) => ({ ...entity, assets: [] })); } + + private patchFaces(faces: AssetFaceEntity[]): OwnedFaceEntity[] { + return faces.map((face) => ({ + id: this.asKey(face), + ownerId: face.asset.ownerId, + assetId: face.assetId, + personId: face.personId, + embedding: face.embedding, + })); + } + + private asKey(face: AssetFaceId): string { + return `${face.assetId}|${face.personId}`; + } + + private asParts(key: string): AssetFaceId { + const [assetId, personId] = key.split('|'); + return { assetId, personId }; + } } diff --git a/server/libs/domain/src/smart-info/machine-learning.interface.ts b/server/libs/domain/src/smart-info/machine-learning.interface.ts index 9d22e0857229c..7fb06d29ede68 100644 --- a/server/libs/domain/src/smart-info/machine-learning.interface.ts +++ b/server/libs/domain/src/smart-info/machine-learning.interface.ts @@ -4,9 +4,25 @@ export interface MachineLearningInput { thumbnailPath: string; } +export interface BoundingBox { + x1: number; + y1: number; + x2: number; + y2: number; +} + +export interface DetectFaceResult { + imageWidth: number; + imageHeight: number; + boundingBox: BoundingBox; + score: number; + embedding: number[]; +} + export interface IMachineLearningRepository { classifyImage(input: MachineLearningInput): Promise; detectObjects(input: MachineLearningInput): Promise; encodeImage(input: MachineLearningInput): Promise; encodeText(input: string): Promise; + detectFaces(input: MachineLearningInput): Promise; } diff --git a/server/libs/domain/src/user/user.service.spec.ts b/server/libs/domain/src/user/user.service.spec.ts index 647c0593453c3..d9995229513f8 100644 --- a/server/libs/domain/src/user/user.service.spec.ts +++ b/server/libs/domain/src/user/user.service.spec.ts @@ -435,7 +435,7 @@ describe(UserService.name, () => { { deletedAt: makeDeletedAt(5) }, ] as UserEntity[]); - await sut.handleQueueUserDelete(); + await sut.handleUserDeleteCheck(); expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled(); expect(jobMock.queue).not.toHaveBeenCalled(); @@ -445,7 +445,7 @@ describe(UserService.name, () => { const user = { deletedAt: makeDeletedAt(10) }; userRepositoryMock.getDeletedUsers.mockResolvedValue([user] as UserEntity[]); - await sut.handleQueueUserDelete(); + await sut.handleUserDeleteCheck(); expect(userRepositoryMock.getDeletedUsers).toHaveBeenCalled(); expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.USER_DELETION, data: { user } }); diff --git a/server/libs/domain/src/user/user.service.ts b/server/libs/domain/src/user/user.service.ts index 305d5e283a9b7..2f7fc37762ee6 100644 --- a/server/libs/domain/src/user/user.service.ts +++ b/server/libs/domain/src/user/user.service.ts @@ -143,7 +143,7 @@ export class UserService { return { admin, password, provided: !!providedPassword }; } - async handleQueueUserDelete() { + async handleUserDeleteCheck() { const users = await this.userRepository.getDeletedUsers(); for (const user of users) { if (this.isReadyForDeletion(user)) { diff --git a/server/libs/domain/test/face.repository.mock.ts b/server/libs/domain/test/face.repository.mock.ts new file mode 100644 index 0000000000000..7e56de4475974 --- /dev/null +++ b/server/libs/domain/test/face.repository.mock.ts @@ -0,0 +1,9 @@ +import { IFaceRepository } from '../src'; + +export const newFaceRepositoryMock = (): jest.Mocked => { + return { + getAll: jest.fn(), + getByIds: jest.fn(), + create: jest.fn(), + }; +}; diff --git a/server/libs/domain/test/fixtures.ts b/server/libs/domain/test/fixtures.ts index 6eebc2e474a97..eb948550691df 100644 --- a/server/libs/domain/test/fixtures.ts +++ b/server/libs/domain/test/fixtures.ts @@ -3,6 +3,7 @@ import { APIKeyEntity, AssetEntity, AssetType, + PersonEntity, PartnerEntity, SharedLinkEntity, SharedLinkType, @@ -10,6 +11,7 @@ import { TranscodePreset, UserEntity, UserTokenEntity, + AssetFaceEntity, } from '@app/infra/entities'; import { AlbumResponseDto, @@ -142,6 +144,7 @@ export const assetEntityStub = { livePhotoVideoId: null, tags: [], sharedLinks: [], + faces: [], }), image: Object.freeze({ id: 'asset-id', @@ -168,6 +171,7 @@ export const assetEntityStub = { tags: [], sharedLinks: [], originalFileName: 'asset-id.ext', + faces: [], }), video: Object.freeze({ id: 'asset-id', @@ -194,6 +198,7 @@ export const assetEntityStub = { livePhotoVideoId: null, tags: [], sharedLinks: [], + faces: [], }), livePhotoMotionAsset: Object.freeze({ id: 'live-photo-motion-asset', @@ -372,6 +377,7 @@ const assetResponse: AssetResponseDto = { exifInfo: assetInfo, livePhotoVideoId: null, tags: [], + people: [], }; const albumResponse: AlbumResponseDto = { @@ -655,6 +661,7 @@ export const sharedLinkStub = { }, tags: [], sharedLinks: [], + faces: [], }, ], }, @@ -729,6 +736,7 @@ export const searchStub = { page: 1, items: [], facets: [], + distances: [], }), }; @@ -826,6 +834,39 @@ export const probeStub = { }), }; +export const personStub = { + noName: Object.freeze({ + id: 'person-1', + createdAt: new Date('2021-01-01'), + updatedAt: new Date('2021-01-01'), + ownerId: userEntityStub.admin.id, + owner: userEntityStub.admin, + name: '', + thumbnailPath: '/path/to/thumbnail', + faces: [], + }), + withName: Object.freeze({ + id: 'person-1', + createdAt: new Date('2021-01-01'), + updatedAt: new Date('2021-01-01'), + ownerId: userEntityStub.admin.id, + owner: userEntityStub.admin, + name: 'Person 1', + thumbnailPath: '/path/to/thumbnail', + faces: [], + }), + noThumbnail: Object.freeze({ + id: 'person-1', + createdAt: new Date('2021-01-01'), + updatedAt: new Date('2021-01-01'), + ownerId: userEntityStub.admin.id, + owner: userEntityStub.admin, + name: '', + thumbnailPath: '', + faces: [], + }), +}; + export const partnerStub = { adminToUser1: Object.freeze({ createdAt: new Date('2023-02-23T05:06:29.716Z'), @@ -844,3 +885,13 @@ export const partnerStub = { sharedWith: userEntityStub.admin, }), }; + +export const faceStub = { + face1: Object.freeze({ + assetId: assetEntityStub.image.id, + asset: assetEntityStub.image, + personId: personStub.withName.id, + person: personStub.withName, + embedding: [1, 2, 3, 4], + }), +}; diff --git a/server/libs/domain/test/index.ts b/server/libs/domain/test/index.ts index 842d291319399..546b7ced56d39 100644 --- a/server/libs/domain/test/index.ts +++ b/server/libs/domain/test/index.ts @@ -3,11 +3,13 @@ export * from './api-key.repository.mock'; export * from './asset.repository.mock'; export * from './communication.repository.mock'; export * from './crypto.repository.mock'; +export * from './face.repository.mock'; export * from './fixtures'; export * from './job.repository.mock'; export * from './machine-learning.repository.mock'; export * from './media.repository.mock'; export * from './partner.repository.mock'; +export * from './person.repository.mock'; export * from './search.repository.mock'; export * from './shared-link.repository.mock'; export * from './smart-info.repository.mock'; diff --git a/server/libs/domain/test/machine-learning.repository.mock.ts b/server/libs/domain/test/machine-learning.repository.mock.ts index b9205d44e8dbf..0988ce8dca4ae 100644 --- a/server/libs/domain/test/machine-learning.repository.mock.ts +++ b/server/libs/domain/test/machine-learning.repository.mock.ts @@ -6,5 +6,6 @@ export const newMachineLearningRepositoryMock = (): jest.Mocked => { extractThumbnailFromExif: jest.fn(), extractVideoThumbnail: jest.fn(), resize: jest.fn(), + crop: jest.fn(), probe: jest.fn(), transcode: jest.fn(), }; diff --git a/server/libs/domain/test/person.repository.mock.ts b/server/libs/domain/test/person.repository.mock.ts new file mode 100644 index 0000000000000..6a1b5e8c580ba --- /dev/null +++ b/server/libs/domain/test/person.repository.mock.ts @@ -0,0 +1,15 @@ +import { IPersonRepository } from '../src'; + +export const newPersonRepositoryMock = (): jest.Mocked => { + return { + getById: jest.fn(), + getAll: jest.fn(), + getAssets: jest.fn(), + getAllWithoutFaces: jest.fn(), + + create: jest.fn(), + update: jest.fn(), + deleteAll: jest.fn(), + delete: jest.fn(), + }; +}; diff --git a/server/libs/domain/test/search.repository.mock.ts b/server/libs/domain/test/search.repository.mock.ts index 5a4fcdf217903..b845e30f9038b 100644 --- a/server/libs/domain/test/search.repository.mock.ts +++ b/server/libs/domain/test/search.repository.mock.ts @@ -6,11 +6,15 @@ export const newSearchRepositoryMock = (): jest.Mocked => { checkMigrationStatus: jest.fn(), importAssets: jest.fn(), importAlbums: jest.fn(), + importFaces: jest.fn(), deleteAlbums: jest.fn(), deleteAssets: jest.fn(), + deleteFaces: jest.fn(), + deleteAllFaces: jest.fn(), searchAssets: jest.fn(), searchAlbums: jest.fn(), vectorSearch: jest.fn(), explore: jest.fn(), + searchFaces: jest.fn(), }; }; diff --git a/server/libs/infra/src/entities/asset-face.entity.ts b/server/libs/infra/src/entities/asset-face.entity.ts new file mode 100644 index 0000000000000..96aa17a2ca168 --- /dev/null +++ b/server/libs/infra/src/entities/asset-face.entity.ts @@ -0,0 +1,25 @@ +import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm'; +import { AssetEntity } from './asset.entity'; +import { PersonEntity } from './person.entity'; + +@Entity('asset_faces') +export class AssetFaceEntity { + @PrimaryColumn() + assetId!: string; + + @PrimaryColumn() + personId!: string; + + @Column({ + type: 'float4', + array: true, + nullable: true, + }) + embedding!: number[] | null; + + @ManyToOne(() => AssetEntity, (asset) => asset.faces, { onDelete: 'CASCADE', onUpdate: 'CASCADE' }) + asset!: AssetEntity; + + @ManyToOne(() => PersonEntity, (person) => person.faces, { onDelete: 'CASCADE', onUpdate: 'CASCADE' }) + person!: PersonEntity; +} diff --git a/server/libs/infra/src/entities/asset.entity.ts b/server/libs/infra/src/entities/asset.entity.ts index d370f06478c4b..cba5518ea9080 100644 --- a/server/libs/infra/src/entities/asset.entity.ts +++ b/server/libs/infra/src/entities/asset.entity.ts @@ -7,12 +7,14 @@ import { JoinTable, ManyToMany, ManyToOne, + OneToMany, OneToOne, PrimaryGeneratedColumn, Unique, UpdateDateColumn, } from 'typeorm'; import { AlbumEntity } from './album.entity'; +import { AssetFaceEntity } from './asset-face.entity'; import { ExifEntity } from './exif.entity'; import { SharedLinkEntity } from './shared-link.entity'; import { SmartInfoEntity } from './smart-info.entity'; @@ -109,6 +111,9 @@ export class AssetEntity { @ManyToMany(() => AlbumEntity, (album) => album.assets, { onDelete: 'CASCADE', onUpdate: 'CASCADE' }) albums?: AlbumEntity[]; + + @OneToMany(() => AssetFaceEntity, (assetFace) => assetFace.asset) + faces!: AssetFaceEntity[]; } export enum AssetType { diff --git a/server/libs/infra/src/entities/index.ts b/server/libs/infra/src/entities/index.ts index 8e4a73b147347..f166b5927f7b8 100644 --- a/server/libs/infra/src/entities/index.ts +++ b/server/libs/infra/src/entities/index.ts @@ -1,18 +1,22 @@ import { AlbumEntity } from './album.entity'; import { APIKeyEntity } from './api-key.entity'; +import { AssetFaceEntity } from './asset-face.entity'; import { AssetEntity } from './asset.entity'; import { PartnerEntity } from './partner.entity'; +import { PersonEntity } from './person.entity'; import { SharedLinkEntity } from './shared-link.entity'; import { SmartInfoEntity } from './smart-info.entity'; import { SystemConfigEntity } from './system-config.entity'; -import { UserEntity } from './user.entity'; import { UserTokenEntity } from './user-token.entity'; +import { UserEntity } from './user.entity'; export * from './album.entity'; export * from './api-key.entity'; +export * from './asset-face.entity'; export * from './asset.entity'; export * from './exif.entity'; export * from './partner.entity'; +export * from './person.entity'; export * from './shared-link.entity'; export * from './smart-info.entity'; export * from './system-config.entity'; @@ -24,7 +28,9 @@ export const databaseEntities = [ AlbumEntity, APIKeyEntity, AssetEntity, + AssetFaceEntity, PartnerEntity, + PersonEntity, SharedLinkEntity, SmartInfoEntity, SystemConfigEntity, diff --git a/server/libs/infra/src/entities/person.entity.ts b/server/libs/infra/src/entities/person.entity.ts new file mode 100644 index 0000000000000..40ad593e93552 --- /dev/null +++ b/server/libs/infra/src/entities/person.entity.ts @@ -0,0 +1,38 @@ +import { + Column, + CreateDateColumn, + Entity, + ManyToOne, + OneToMany, + PrimaryGeneratedColumn, + UpdateDateColumn, +} from 'typeorm'; +import { AssetFaceEntity } from './asset-face.entity'; +import { UserEntity } from './user.entity'; + +@Entity('person') +export class PersonEntity { + @PrimaryGeneratedColumn('uuid') + id!: string; + + @CreateDateColumn({ type: 'timestamptz' }) + createdAt!: Date; + + @UpdateDateColumn({ type: 'timestamptz' }) + updatedAt!: Date; + + @Column() + ownerId!: string; + + @ManyToOne(() => UserEntity, { onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: false }) + owner!: UserEntity; + + @Column({ default: '' }) + name!: string; + + @Column({ default: '' }) + thumbnailPath!: string; + + @OneToMany(() => AssetFaceEntity, (assetFace) => assetFace.person) + faces!: AssetFaceEntity[]; +} diff --git a/server/libs/infra/src/infra.module.ts b/server/libs/infra/src/infra.module.ts index 51cc1bef146c2..34cd5f7c8812c 100644 --- a/server/libs/infra/src/infra.module.ts +++ b/server/libs/infra/src/infra.module.ts @@ -3,6 +3,7 @@ import { IAssetRepository, ICommunicationRepository, ICryptoRepository, + IFaceRepository, IGeocodingRepository, IJobRepository, IKeyRepository, @@ -10,6 +11,7 @@ import { IMediaRepository, immichAppConfig, IPartnerRepository, + IPersonRepository, ISearchRepository, ISharedLinkRepository, ISmartInfoRepository, @@ -32,12 +34,14 @@ import { AssetRepository, CommunicationRepository, CryptoRepository, + FaceRepository, FilesystemProvider, GeocodingRepository, JobRepository, MachineLearningRepository, MediaRepository, PartnerRepository, + PersonRepository, SharedLinkRepository, SmartInfoRepository, SystemConfigRepository, @@ -51,12 +55,14 @@ const providers: Provider[] = [ { provide: IAssetRepository, useClass: AssetRepository }, { provide: ICommunicationRepository, useClass: CommunicationRepository }, { provide: ICryptoRepository, useClass: CryptoRepository }, + { provide: IFaceRepository, useClass: FaceRepository }, { provide: IGeocodingRepository, useClass: GeocodingRepository }, { provide: IJobRepository, useClass: JobRepository }, { provide: IKeyRepository, useClass: APIKeyRepository }, { provide: IMachineLearningRepository, useClass: MachineLearningRepository }, { provide: IMediaRepository, useClass: MediaRepository }, { provide: IPartnerRepository, useClass: PartnerRepository }, + { provide: IPersonRepository, useClass: PersonRepository }, { provide: ISearchRepository, useClass: TypesenseRepository }, { provide: ISharedLinkRepository, useClass: SharedLinkRepository }, { provide: ISmartInfoRepository, useClass: SmartInfoRepository }, diff --git a/server/libs/infra/src/migrations/1684255168091-AddFacialTables.ts b/server/libs/infra/src/migrations/1684255168091-AddFacialTables.ts new file mode 100644 index 0000000000000..1f2426bb0cc5d --- /dev/null +++ b/server/libs/infra/src/migrations/1684255168091-AddFacialTables.ts @@ -0,0 +1,22 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddFacialTables1684255168091 implements MigrationInterface { + name = 'AddFacialTables1684255168091' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "person" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "ownerId" uuid NOT NULL, "name" character varying NOT NULL DEFAULT '', "thumbnailPath" character varying NOT NULL DEFAULT '', CONSTRAINT "PK_5fdaf670315c4b7e70cce85daa3" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TABLE "asset_faces" ("assetId" uuid NOT NULL, "personId" uuid NOT NULL, "embedding" real array, CONSTRAINT "PK_bf339a24070dac7e71304ec530a" PRIMARY KEY ("assetId", "personId"))`); + await queryRunner.query(`ALTER TABLE "person" ADD CONSTRAINT "FK_5527cc99f530a547093f9e577b6" FOREIGN KEY ("ownerId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE`); + await queryRunner.query(`ALTER TABLE "asset_faces" ADD CONSTRAINT "FK_02a43fd0b3c50fb6d7f0cb7282c" FOREIGN KEY ("assetId") REFERENCES "assets"("id") ON DELETE CASCADE ON UPDATE CASCADE`); + await queryRunner.query(`ALTER TABLE "asset_faces" ADD CONSTRAINT "FK_95ad7106dd7b484275443f580f9" FOREIGN KEY ("personId") REFERENCES "person"("id") ON DELETE CASCADE ON UPDATE CASCADE`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "asset_faces" DROP CONSTRAINT "FK_95ad7106dd7b484275443f580f9"`); + await queryRunner.query(`ALTER TABLE "asset_faces" DROP CONSTRAINT "FK_02a43fd0b3c50fb6d7f0cb7282c"`); + await queryRunner.query(`ALTER TABLE "person" DROP CONSTRAINT "FK_5527cc99f530a547093f9e577b6"`); + await queryRunner.query(`DROP TABLE "asset_faces"`); + await queryRunner.query(`DROP TABLE "person"`); + } + +} diff --git a/server/libs/infra/src/repositories/asset.repository.ts b/server/libs/infra/src/repositories/asset.repository.ts index 4861b9c4e2b29..dda9572f562a5 100644 --- a/server/libs/infra/src/repositories/asset.repository.ts +++ b/server/libs/infra/src/repositories/asset.repository.ts @@ -15,6 +15,9 @@ export class AssetRepository implements IAssetRepository { exifInfo: true, smartInfo: true, tags: true, + faces: { + person: true, + }, }, }); } @@ -35,6 +38,9 @@ export class AssetRepository implements IAssetRepository { exifInfo: true, smartInfo: true, tags: true, + faces: { + person: true, + }, }, }); } @@ -48,6 +54,7 @@ export class AssetRepository implements IAssetRepository { owner: true, smartInfo: true, tags: true, + faces: true, }, }); } @@ -129,6 +136,20 @@ export class AssetRepository implements IAssetRepository { }; break; + case WithoutProperty.FACES: + relations = { + faces: true, + }; + where = { + resizePath: IsNull(), + isVisible: true, + faces: { + assetId: IsNull(), + personId: IsNull(), + }, + }; + break; + default: throw new Error(`Invalid getWithout property: ${property}`); } diff --git a/server/libs/infra/src/repositories/face.repository.ts b/server/libs/infra/src/repositories/face.repository.ts new file mode 100644 index 0000000000000..34b8bf800dbba --- /dev/null +++ b/server/libs/infra/src/repositories/face.repository.ts @@ -0,0 +1,22 @@ +import { AssetFaceId, IFaceRepository } from '@app/domain'; +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { AssetFaceEntity } from '../entities/asset-face.entity'; + +@Injectable() +export class FaceRepository implements IFaceRepository { + constructor(@InjectRepository(AssetFaceEntity) private repository: Repository) {} + + getAll(): Promise { + return this.repository.find({ relations: { asset: true } }); + } + + getByIds(ids: AssetFaceId[]): Promise { + return this.repository.find({ where: ids, relations: { asset: true } }); + } + + create(entity: Partial): Promise { + return this.repository.save(entity); + } +} diff --git a/server/libs/infra/src/repositories/index.ts b/server/libs/infra/src/repositories/index.ts index 28b208c31efb1..75347863e1cd9 100644 --- a/server/libs/infra/src/repositories/index.ts +++ b/server/libs/infra/src/repositories/index.ts @@ -3,12 +3,14 @@ export * from './api-key.repository'; export * from './asset.repository'; export * from './communication.repository'; export * from './crypto.repository'; +export * from './face.repository'; export * from './filesystem.provider'; export * from './geocoding.repository'; export * from './job.repository'; export * from './machine-learning.repository'; export * from './media.repository'; export * from './partner.repository'; +export * from './person.repository'; export * from './shared-link.repository'; export * from './smart-info.repository'; export * from './system-config.repository'; diff --git a/server/libs/infra/src/repositories/job.repository.ts b/server/libs/infra/src/repositories/job.repository.ts index f129ade23b9c7..557ab07802dde 100644 --- a/server/libs/infra/src/repositories/job.repository.ts +++ b/server/libs/infra/src/repositories/job.repository.ts @@ -20,6 +20,7 @@ export class JobRepository implements IJobRepository { [QueueName.THUMBNAIL_GENERATION]: this.generateThumbnail, [QueueName.METADATA_EXTRACTION]: this.metadataExtraction, [QueueName.OBJECT_TAGGING]: this.objectTagging, + [QueueName.RECOGNIZE_FACES]: this.recognizeFaces, [QueueName.CLIP_ENCODING]: this.clipEmbedding, [QueueName.VIDEO_CONVERSION]: this.videoTranscode, [QueueName.BACKGROUND_TASK]: this.backgroundTask, @@ -31,6 +32,7 @@ export class JobRepository implements IJobRepository { @InjectQueue(QueueName.OBJECT_TAGGING) private objectTagging: Queue, @InjectQueue(QueueName.CLIP_ENCODING) private clipEmbedding: Queue, @InjectQueue(QueueName.METADATA_EXTRACTION) private metadataExtraction: Queue, + @InjectQueue(QueueName.RECOGNIZE_FACES) private recognizeFaces: Queue, @InjectQueue(QueueName.STORAGE_TEMPLATE_MIGRATION) private storageTemplateMigration: Queue, @InjectQueue(QueueName.THUMBNAIL_GENERATION) private generateThumbnail: Queue, @InjectQueue(QueueName.VIDEO_CONVERSION) private videoTranscode: Queue, @@ -91,6 +93,19 @@ export class JobRepository implements IJobRepository { await this.metadataExtraction.add(item.name, item.data); break; + case JobName.QUEUE_RECOGNIZE_FACES: + case JobName.RECOGNIZE_FACES: + await this.recognizeFaces.add(item.name, item.data); + break; + + case JobName.GENERATE_FACE_THUMBNAIL: + await this.recognizeFaces.add(item.name, item.data, { priority: 1 }); + break; + + case JobName.PERSON_CLEANUP: + await this.backgroundTask.add(item.name); + break; + case JobName.QUEUE_GENERATE_THUMBNAILS: case JobName.GENERATE_JPEG_THUMBNAIL: case JobName.GENERATE_WEBP_THUMBNAIL: @@ -120,13 +135,19 @@ export class JobRepository implements IJobRepository { case JobName.SEARCH_INDEX_ASSETS: case JobName.SEARCH_INDEX_ALBUMS: + case JobName.SEARCH_INDEX_FACES: await this.searchIndex.add(item.name, {}); break; case JobName.SEARCH_INDEX_ASSET: case JobName.SEARCH_INDEX_ALBUM: + case JobName.SEARCH_INDEX_FACE: + await this.searchIndex.add(item.name, item.data); + break; + case JobName.SEARCH_REMOVE_ALBUM: case JobName.SEARCH_REMOVE_ASSET: + case JobName.SEARCH_REMOVE_FACE: await this.searchIndex.add(item.name, item.data); break; diff --git a/server/libs/infra/src/repositories/machine-learning.repository.ts b/server/libs/infra/src/repositories/machine-learning.repository.ts index 7fe90db99d4c5..4931b82c43229 100644 --- a/server/libs/infra/src/repositories/machine-learning.repository.ts +++ b/server/libs/infra/src/repositories/machine-learning.repository.ts @@ -1,4 +1,4 @@ -import { IMachineLearningRepository, MachineLearningInput, MACHINE_LEARNING_URL } from '@app/domain'; +import { DetectFaceResult, IMachineLearningRepository, MachineLearningInput, MACHINE_LEARNING_URL } from '@app/domain'; import { Injectable } from '@nestjs/common'; import axios from 'axios'; @@ -10,6 +10,10 @@ export class MachineLearningRepository implements IMachineLearningRepository { return client.post('/image-classifier/tag-image', input).then((res) => res.data); } + detectFaces(input: MachineLearningInput): Promise { + return client.post('/facial-recognition/detect-faces', input).then((res) => res.data); + } + detectObjects(input: MachineLearningInput): Promise { return client.post('/object-detection/detect-object', input).then((res) => res.data); } diff --git a/server/libs/infra/src/repositories/media.repository.ts b/server/libs/infra/src/repositories/media.repository.ts index 94d5f70d3fdea..ef7ca0f9428ca 100644 --- a/server/libs/infra/src/repositories/media.repository.ts +++ b/server/libs/infra/src/repositories/media.repository.ts @@ -1,4 +1,4 @@ -import { IMediaRepository, ResizeOptions, VideoInfo } from '@app/domain'; +import { CropOptions, IMediaRepository, ResizeOptions, VideoInfo } from '@app/domain'; import { exiftool } from 'exiftool-vendored'; import ffmpeg, { FfprobeData } from 'fluent-ffmpeg'; import sharp from 'sharp'; @@ -7,11 +7,22 @@ import { promisify } from 'util'; const probe = promisify(ffmpeg.ffprobe); export class MediaRepository implements IMediaRepository { + crop(input: string, options: CropOptions): Promise { + return sharp(input, { failOnError: false }) + .extract({ + left: options.left, + top: options.top, + width: options.width, + height: options.height, + }) + .toBuffer(); + } + extractThumbnailFromExif(input: string, output: string): Promise { return exiftool.extractThumbnail(input, output); } - async resize(input: string, output: string, options: ResizeOptions): Promise { + async resize(input: string | Buffer, output: string, options: ResizeOptions): Promise { switch (options.format) { case 'webp': await sharp(input, { failOnError: false }) diff --git a/server/libs/infra/src/repositories/person.repository.ts b/server/libs/infra/src/repositories/person.repository.ts new file mode 100644 index 0000000000000..96100fb52499d --- /dev/null +++ b/server/libs/infra/src/repositories/person.repository.ts @@ -0,0 +1,78 @@ +import { IPersonRepository, PersonSearchOptions } from '@app/domain'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { AssetEntity, AssetFaceEntity, PersonEntity } from '../entities'; + +export class PersonRepository implements IPersonRepository { + constructor( + @InjectRepository(AssetEntity) private assetRepository: Repository, + @InjectRepository(PersonEntity) private personRepository: Repository, + @InjectRepository(AssetFaceEntity) private assetFaceRepository: Repository, + ) {} + + delete(entity: PersonEntity): Promise { + return this.personRepository.remove(entity); + } + + async deleteAll(): Promise { + const people = await this.personRepository.find(); + await this.personRepository.remove(people); + return people.length; + } + + getAll(userId: string, options?: PersonSearchOptions): Promise { + return this.personRepository + .createQueryBuilder('person') + .leftJoin('person.faces', 'face') + .where('person.ownerId = :userId', { userId }) + .orderBy('COUNT(face.assetId)', 'DESC') + .having('COUNT(face.assetId) >= :faces', { faces: options?.minimumFaceCount || 1 }) + .groupBy('person.id') + .getMany(); + } + + getAllWithoutFaces(): Promise { + return this.personRepository + .createQueryBuilder('person') + .leftJoin('person.faces', 'face') + .having('COUNT(face.assetId) = 0') + .groupBy('person.id') + .getMany(); + } + + getById(ownerId: string, personId: string): Promise { + return this.personRepository.findOne({ where: { id: personId, ownerId } }); + } + + getAssets(ownerId: string, personId: string): Promise { + return this.assetRepository.find({ + where: { + ownerId, + faces: { + personId, + }, + isVisible: true, + }, + relations: { + faces: { + person: true, + }, + exifInfo: true, + }, + order: { + createdAt: 'ASC', + }, + // TODO: remove after either (1) pagination or (2) time bucket is implemented for this query + take: 3000, + }); + } + + create(entity: Partial): Promise { + return this.personRepository.save(entity); + } + + async update(entity: Partial): Promise { + const { id } = await this.personRepository.save(entity); + return this.personRepository.findOneByOrFail({ id }); + } +} diff --git a/server/libs/infra/src/repositories/typesense.repository.ts b/server/libs/infra/src/repositories/typesense.repository.ts index c56cef053d7ae..b7995ad81fb4f 100644 --- a/server/libs/infra/src/repositories/typesense.repository.ts +++ b/server/libs/infra/src/repositories/typesense.repository.ts @@ -1,8 +1,10 @@ import { ISearchRepository, + OwnedFaceEntity, SearchCollection, SearchCollectionIndexStatus, SearchExploreItem, + SearchFaceFilter, SearchFilter, SearchResult, } from '@app/domain'; @@ -12,9 +14,9 @@ import { catchError, filter, firstValueFrom, from, map, mergeMap, of, toArray } import { Client } from 'typesense'; import { CollectionCreateSchema } from 'typesense/lib/Typesense/Collections'; import { DocumentSchema, SearchResponse } from 'typesense/lib/Typesense/Documents'; -import { AlbumEntity, AssetEntity } from '../entities'; +import { AlbumEntity, AssetEntity, AssetFaceEntity } from '../entities'; import { typesenseConfig } from '../infra.config'; -import { albumSchema, assetSchema } from '../typesense-schemas'; +import { albumSchema, assetSchema, faceSchema } from '../typesense-schemas'; function removeNil>(item: T): T { _.forOwn(item, (value, key) => { @@ -26,14 +28,21 @@ function removeNil>(item: T): T { return item; } +interface MultiSearchError { + code: number; + error: string; +} + interface CustomAssetEntity extends AssetEntity { geo?: [number, number]; motion?: boolean; + people?: string[]; } const schemaMap: Record = { [SearchCollection.ASSETS]: assetSchema, [SearchCollection.ALBUMS]: albumSchema, + [SearchCollection.FACES]: faceSchema, }; const schemas = Object.entries(schemaMap) as [SearchCollection, CollectionCreateSchema][]; @@ -61,7 +70,7 @@ export class TypesenseRepository implements ISearchRepository { async setup(): Promise { const collections = await this.client.collections().retrieve(); for (const collection of collections) { - this.logger.debug(`${collection.name} => ${collection.num_documents}`); + this.logger.debug(`${collection.name} collection has ${collection.num_documents} documents`); // await this.client.collections(collection.name).delete(); } @@ -84,6 +93,7 @@ export class TypesenseRepository implements ISearchRepository { const migrationMap: SearchCollectionIndexStatus = { [SearchCollection.ASSETS]: false, [SearchCollection.ALBUMS]: false, + [SearchCollection.FACES]: false, }; // check if alias is using the current schema @@ -110,9 +120,13 @@ export class TypesenseRepository implements ISearchRepository { await this.import(SearchCollection.ASSETS, items, done); } + async importFaces(items: OwnedFaceEntity[], done: boolean): Promise { + await this.import(SearchCollection.FACES, items, done); + } + private async import( collection: SearchCollection, - items: AlbumEntity[] | AssetEntity[], + items: AlbumEntity[] | AssetEntity[] | OwnedFaceEntity[], done: boolean, ): Promise { try { @@ -198,6 +212,15 @@ export class TypesenseRepository implements ISearchRepository { await this.delete(SearchCollection.ASSETS, ids); } + async deleteFaces(ids: string[]): Promise { + await this.delete(SearchCollection.FACES, ids); + } + + async deleteAllFaces(): Promise { + const records = await this.client.collections(faceSchema.name).documents().delete({ filter_by: 'ownerId:!=null' }); + return records.num_deleted; + } + async delete(collection: SearchCollection, ids: string[]): Promise { await this.client .collections(schemaMap[collection].name) @@ -232,6 +255,7 @@ export class TypesenseRepository implements ISearchRepository { 'exifInfo.description', 'smartInfo.tags', 'smartInfo.objects', + 'people', ].join(','), per_page: 250, facet_by: this.getFacetFieldNames(SearchCollection.ASSETS), @@ -242,6 +266,22 @@ export class TypesenseRepository implements ISearchRepository { return this.asResponse(results, filters.debug); } + async searchFaces(input: number[], filters: SearchFaceFilter): Promise> { + const { results } = await this.client.multiSearch.perform({ + searches: [ + { + collection: faceSchema.name, + q: '*', + vector_query: `embedding:([${input.join(',')}], k:5)`, + per_page: 250, + filter_by: this.buildFilterBy('ownerId', filters.ownerId, true), + } as any, + ], + }); + + return this.asResponse(results[0] as SearchResponse); + } + async vectorSearch(input: number[], filters: SearchFilter): Promise> { const { results } = await this.client.multiSearch.perform({ searches: [ @@ -259,12 +299,23 @@ export class TypesenseRepository implements ISearchRepository { return this.asResponse(results[0] as SearchResponse, filters.debug); } - private asResponse(results: SearchResponse, debug?: boolean): SearchResult { + private asResponse( + resultsOrError: SearchResponse | MultiSearchError, + debug?: boolean, + ): SearchResult { + const { error, code } = resultsOrError as MultiSearchError; + if (error) { + throw new Error(`Typesense multi-search error: ${code} - ${error}`); + } + + const results = resultsOrError as SearchResponse; + return { page: results.page, total: results.found, count: results.out_of, items: (results.hits || []).map((hit) => hit.document), + distances: (results.hits || []).map((hit: any) => hit.vector_distance), facets: (results.facet_counts || []).map((facet) => ({ counts: facet.counts.map((item) => ({ count: item.count, value: item.value })), fieldName: facet.field_name as string, @@ -306,12 +357,17 @@ export class TypesenseRepository implements ISearchRepository { } } - private patch(collection: SearchCollection, items: AssetEntity[] | AlbumEntity[]) { - return items.map((item) => - collection === SearchCollection.ASSETS - ? this.patchAsset(item as AssetEntity) - : this.patchAlbum(item as AlbumEntity), - ); + private patch(collection: SearchCollection, items: AssetEntity[] | AlbumEntity[] | OwnedFaceEntity[]) { + return items.map((item) => { + switch (collection) { + case SearchCollection.ASSETS: + return this.patchAsset(item as AssetEntity); + case SearchCollection.ALBUMS: + return this.patchAlbum(item as AlbumEntity); + case SearchCollection.FACES: + return this.patchFace(item as OwnedFaceEntity); + } + }); } private patchAlbum(album: AlbumEntity): AlbumEntity { @@ -327,9 +383,17 @@ export class TypesenseRepository implements ISearchRepository { custom = { ...custom, geo: [lat, lng] }; } + const people = asset.faces?.map((face) => face.person.name).filter((name) => name) || []; + if (people.length) { + custom = { ...custom, people }; + } return removeNil({ ...custom, motion: !!asset.livePhotoVideoId }); } + private patchFace(face: OwnedFaceEntity): OwnedFaceEntity { + return removeNil(face); + } + private getFacetFieldNames(collection: SearchCollection) { return (schemaMap[collection].fields || []) .filter((field) => field.facet) diff --git a/server/libs/infra/src/typesense-schemas/asset.schema.ts b/server/libs/infra/src/typesense-schemas/asset.schema.ts index 0b778fbeae780..5d31e67b2b4ab 100644 --- a/server/libs/infra/src/typesense-schemas/asset.schema.ts +++ b/server/libs/infra/src/typesense-schemas/asset.schema.ts @@ -1,6 +1,6 @@ import { CollectionCreateSchema } from 'typesense/lib/Typesense/Collections'; -export const assetSchemaVersion = 5; +export const assetSchemaVersion = 7; export const assetSchema: CollectionCreateSchema = { name: `assets-v${assetSchemaVersion}`, fields: [ @@ -32,6 +32,7 @@ export const assetSchema: CollectionCreateSchema = { // computed { name: 'geo', type: 'geopoint', facet: false, optional: true }, { name: 'motion', type: 'bool', facet: true }, + { name: 'people', type: 'string[]', facet: true, optional: true }, ], token_separators: ['.'], enable_nested_fields: true, diff --git a/server/libs/infra/src/typesense-schemas/face.schema.ts b/server/libs/infra/src/typesense-schemas/face.schema.ts new file mode 100644 index 0000000000000..c978057ec9b1c --- /dev/null +++ b/server/libs/infra/src/typesense-schemas/face.schema.ts @@ -0,0 +1,12 @@ +import { CollectionCreateSchema } from 'typesense/lib/Typesense/Collections'; + +export const faceSchemaVersion = 1; +export const faceSchema: CollectionCreateSchema = { + name: `faces-v${faceSchemaVersion}`, + fields: [ + { name: 'ownerId', type: 'string', facet: false }, + { name: 'assetId', type: 'string', facet: false }, + { name: 'personId', type: 'string', facet: false }, + { name: 'embedding', type: 'float[]', facet: false, num_dim: 512 }, + ], +}; diff --git a/server/libs/infra/src/typesense-schemas/index.ts b/server/libs/infra/src/typesense-schemas/index.ts index d002910221093..a2ab8619475a5 100644 --- a/server/libs/infra/src/typesense-schemas/index.ts +++ b/server/libs/infra/src/typesense-schemas/index.ts @@ -1,2 +1,3 @@ export * from './album.schema'; export * from './asset.schema'; +export * from './face.schema'; diff --git a/web/src/api/api.ts b/web/src/api/api.ts index 26499db26fb42..79397a9003bc2 100644 --- a/web/src/api/api.ts +++ b/web/src/api/api.ts @@ -8,6 +8,7 @@ import { ConfigurationParameters, JobApi, OAuthApi, + PersonApi, PartnerApi, SearchApi, ServerInfoApi, @@ -31,6 +32,7 @@ export class ImmichApi { public searchApi: SearchApi; public serverInfoApi: ServerInfoApi; public shareApi: ShareApi; + public personApi: PersonApi; public systemConfigApi: SystemConfigApi; public userApi: UserApi; @@ -49,6 +51,7 @@ export class ImmichApi { this.searchApi = new SearchApi(this.config); this.serverInfoApi = new ServerInfoApi(this.config); this.shareApi = new ShareApi(this.config); + this.personApi = new PersonApi(this.config); this.systemConfigApi = new SystemConfigApi(this.config); this.userApi = new UserApi(this.config); } @@ -98,6 +101,11 @@ export class ImmichApi { const path = `/user/profile-image/${userId}`; return this.createUrl(path); } + + public getPeopleThumbnailUrl(personId: string) { + const path = `/person/${personId}/thumbnail`; + return this.createUrl(path); + } } export const api = new ImmichApi({ basePath: '/api' }); diff --git a/web/src/api/open-api/api.ts b/web/src/api/open-api/api.ts index 602bdac3891ed..c2475e8d2361c 100644 --- a/web/src/api/open-api/api.ts +++ b/web/src/api/open-api/api.ts @@ -339,6 +339,12 @@ export interface AllJobStatusResponseDto { * @memberof AllJobStatusResponseDto */ 'search-queue': JobStatusDto; + /** + * + * @type {JobStatusDto} + * @memberof AllJobStatusResponseDto + */ + 'recognize-faces-queue': JobStatusDto; } /** * @@ -566,6 +572,12 @@ export interface AssetResponseDto { * @memberof AssetResponseDto */ 'tags'?: Array; + /** + * + * @type {Array} + * @memberof AssetResponseDto + */ + 'people'?: Array; } @@ -1329,6 +1341,7 @@ export const JobName = { MetadataExtractionQueue: 'metadata-extraction-queue', VideoConversionQueue: 'video-conversion-queue', ObjectTaggingQueue: 'object-tagging-queue', + RecognizeFacesQueue: 'recognize-faces-queue', ClipEncodingQueue: 'clip-encoding-queue', BackgroundTaskQueue: 'background-task-queue', StorageTemplateMigrationQueue: 'storage-template-migration-queue', @@ -1546,6 +1559,44 @@ export interface OAuthConfigResponseDto { */ 'autoLaunch'?: boolean; } +/** + * + * @export + * @interface PersonResponseDto + */ +export interface PersonResponseDto { + /** + * + * @type {string} + * @memberof PersonResponseDto + */ + 'id': string; + /** + * + * @type {string} + * @memberof PersonResponseDto + */ + 'name': string; + /** + * + * @type {string} + * @memberof PersonResponseDto + */ + 'thumbnailPath': string; +} +/** + * + * @export + * @interface PersonUpdateDto + */ +export interface PersonUpdateDto { + /** + * + * @type {string} + * @memberof PersonUpdateDto + */ + 'name': string; +} /** * * @export @@ -7460,6 +7511,406 @@ export class PartnerApi extends BaseAPI { } +/** + * PersonApi - axios parameter creator + * @export + */ +export const PersonApiAxiosParamCreator = function (configuration?: Configuration) { + return { + /** + * + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getAllPeople: async (options: AxiosRequestConfig = {}): Promise => { + const localVarPath = `/person`; + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + // authentication cookie required + + // authentication api_key required + await setApiKeyToObject(localVarHeaderParameter, "x-api-key", configuration) + + // authentication bearer required + // http bearer authentication required + await setBearerAuthToObject(localVarHeaderParameter, configuration) + + + + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPerson: async (id: string, options: AxiosRequestConfig = {}): Promise => { + // verify required parameter 'id' is not null or undefined + assertParamExists('getPerson', 'id', id) + const localVarPath = `/person/{id}` + .replace(`{${"id"}}`, encodeURIComponent(String(id))); + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + // authentication cookie required + + // authentication api_key required + await setApiKeyToObject(localVarHeaderParameter, "x-api-key", configuration) + + // authentication bearer required + // http bearer authentication required + await setBearerAuthToObject(localVarHeaderParameter, configuration) + + + + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPersonAssets: async (id: string, options: AxiosRequestConfig = {}): Promise => { + // verify required parameter 'id' is not null or undefined + assertParamExists('getPersonAssets', 'id', id) + const localVarPath = `/person/{id}/assets` + .replace(`{${"id"}}`, encodeURIComponent(String(id))); + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + // authentication cookie required + + // authentication api_key required + await setApiKeyToObject(localVarHeaderParameter, "x-api-key", configuration) + + // authentication bearer required + // http bearer authentication required + await setBearerAuthToObject(localVarHeaderParameter, configuration) + + + + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPersonThumbnail: async (id: string, options: AxiosRequestConfig = {}): Promise => { + // verify required parameter 'id' is not null or undefined + assertParamExists('getPersonThumbnail', 'id', id) + const localVarPath = `/person/{id}/thumbnail` + .replace(`{${"id"}}`, encodeURIComponent(String(id))); + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + // authentication cookie required + + // authentication api_key required + await setApiKeyToObject(localVarHeaderParameter, "x-api-key", configuration) + + // authentication bearer required + // http bearer authentication required + await setBearerAuthToObject(localVarHeaderParameter, configuration) + + + + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, + /** + * + * @param {string} id + * @param {PersonUpdateDto} personUpdateDto + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + updatePerson: async (id: string, personUpdateDto: PersonUpdateDto, options: AxiosRequestConfig = {}): Promise => { + // verify required parameter 'id' is not null or undefined + assertParamExists('updatePerson', 'id', id) + // verify required parameter 'personUpdateDto' is not null or undefined + assertParamExists('updatePerson', 'personUpdateDto', personUpdateDto) + const localVarPath = `/person/{id}` + .replace(`{${"id"}}`, encodeURIComponent(String(id))); + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'PUT', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + // authentication cookie required + + // authentication api_key required + await setApiKeyToObject(localVarHeaderParameter, "x-api-key", configuration) + + // authentication bearer required + // http bearer authentication required + await setBearerAuthToObject(localVarHeaderParameter, configuration) + + + + localVarHeaderParameter['Content-Type'] = 'application/json'; + + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + localVarRequestOptions.data = serializeDataIfNeeded(personUpdateDto, localVarRequestOptions, configuration) + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, + } +}; + +/** + * PersonApi - functional programming interface + * @export + */ +export const PersonApiFp = function(configuration?: Configuration) { + const localVarAxiosParamCreator = PersonApiAxiosParamCreator(configuration) + return { + /** + * + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async getAllPeople(options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise>> { + const localVarAxiosArgs = await localVarAxiosParamCreator.getAllPeople(options); + return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async getPerson(id: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.getPerson(id, options); + return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async getPersonAssets(id: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise>> { + const localVarAxiosArgs = await localVarAxiosParamCreator.getPersonAssets(id, options); + return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async getPersonThumbnail(id: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.getPersonThumbnail(id, options); + return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); + }, + /** + * + * @param {string} id + * @param {PersonUpdateDto} personUpdateDto + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async updatePerson(id: string, personUpdateDto: PersonUpdateDto, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.updatePerson(id, personUpdateDto, options); + return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); + }, + } +}; + +/** + * PersonApi - factory interface + * @export + */ +export const PersonApiFactory = function (configuration?: Configuration, basePath?: string, axios?: AxiosInstance) { + const localVarFp = PersonApiFp(configuration) + return { + /** + * + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getAllPeople(options?: any): AxiosPromise> { + return localVarFp.getAllPeople(options).then((request) => request(axios, basePath)); + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPerson(id: string, options?: any): AxiosPromise { + return localVarFp.getPerson(id, options).then((request) => request(axios, basePath)); + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPersonAssets(id: string, options?: any): AxiosPromise> { + return localVarFp.getPersonAssets(id, options).then((request) => request(axios, basePath)); + }, + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPersonThumbnail(id: string, options?: any): AxiosPromise { + return localVarFp.getPersonThumbnail(id, options).then((request) => request(axios, basePath)); + }, + /** + * + * @param {string} id + * @param {PersonUpdateDto} personUpdateDto + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + updatePerson(id: string, personUpdateDto: PersonUpdateDto, options?: any): AxiosPromise { + return localVarFp.updatePerson(id, personUpdateDto, options).then((request) => request(axios, basePath)); + }, + }; +}; + +/** + * PersonApi - object-oriented interface + * @export + * @class PersonApi + * @extends {BaseAPI} + */ +export class PersonApi extends BaseAPI { + /** + * + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof PersonApi + */ + public getAllPeople(options?: AxiosRequestConfig) { + return PersonApiFp(this.configuration).getAllPeople(options).then((request) => request(this.axios, this.basePath)); + } + + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof PersonApi + */ + public getPerson(id: string, options?: AxiosRequestConfig) { + return PersonApiFp(this.configuration).getPerson(id, options).then((request) => request(this.axios, this.basePath)); + } + + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof PersonApi + */ + public getPersonAssets(id: string, options?: AxiosRequestConfig) { + return PersonApiFp(this.configuration).getPersonAssets(id, options).then((request) => request(this.axios, this.basePath)); + } + + /** + * + * @param {string} id + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof PersonApi + */ + public getPersonThumbnail(id: string, options?: AxiosRequestConfig) { + return PersonApiFp(this.configuration).getPersonThumbnail(id, options).then((request) => request(this.axios, this.basePath)); + } + + /** + * + * @param {string} id + * @param {PersonUpdateDto} personUpdateDto + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof PersonApi + */ + public updatePerson(id: string, personUpdateDto: PersonUpdateDto, options?: AxiosRequestConfig) { + return PersonApiFp(this.configuration).updatePerson(id, personUpdateDto, options).then((request) => request(this.axios, this.basePath)); + } +} + + /** * SearchApi - axios parameter creator * @export diff --git a/web/src/lib/components/admin-page/jobs/jobs-panel.svelte b/web/src/lib/components/admin-page/jobs/jobs-panel.svelte index b103d4deb004d..70db761442762 100644 --- a/web/src/lib/components/admin-page/jobs/jobs-panel.svelte +++ b/web/src/lib/components/admin-page/jobs/jobs-panel.svelte @@ -36,6 +36,10 @@ title: 'Encode Clip', subtitle: 'Run machine learning to generate clip embeddings' }, + [JobName.RecognizeFacesQueue]: { + title: 'Recognize Faces', + subtitle: 'Run machine learning to recognize faces' + }, [JobName.VideoConversionQueue]: { title: 'Transcode Videos', subtitle: 'Transcode videos not in the desired format' diff --git a/web/src/lib/components/album-page/album-viewer.svelte b/web/src/lib/components/album-page/album-viewer.svelte index bd832f11f09dc..702ed4acf3c17 100644 --- a/web/src/lib/components/album-page/album-viewer.svelte +++ b/web/src/lib/components/album-page/album-viewer.svelte @@ -120,10 +120,6 @@ } }); - const clearMultiSelectAssetAssetHandler = () => { - multiSelectAsset = new Set(); - }; - // Update Album Name $: { if (!isEditingTitle && currentAlbumName != album.albumName && isOwned) { @@ -340,7 +336,7 @@ {#if isMultiSelectionMode} (multiSelectAsset = new Set())} > {#if isOwned} diff --git a/web/src/lib/components/asset-viewer/asset-viewer.svelte b/web/src/lib/components/asset-viewer/asset-viewer.svelte index 74c12f4c73de6..4c827fca255de 100644 --- a/web/src/lib/components/asset-viewer/asset-viewer.svelte +++ b/web/src/lib/components/asset-viewer/asset-viewer.svelte @@ -91,6 +91,11 @@ } }; + const handleCloseViewer = () => { + isShowDetail = false; + closeViewer(); + }; + const closeViewer = () => { dispatch('close'); }; @@ -398,6 +403,7 @@ {asset} albums={appearsInAlbums} on:close={() => (isShowDetail = false)} + on:close-viewer={handleCloseViewer} on:description-focus-in={disableKeyDownEvent} on:description-focus-out={enableKeyDownEvent} /> diff --git a/web/src/lib/components/asset-viewer/detail-panel.svelte b/web/src/lib/components/asset-viewer/detail-panel.svelte index 96616d774ede8..80e520761446f 100644 --- a/web/src/lib/components/asset-viewer/detail-panel.svelte +++ b/web/src/lib/components/asset-viewer/detail-panel.svelte @@ -1,16 +1,17 @@ @@ -13,7 +17,11 @@ src={url} alt={altText} class="object-cover transition-opacity duration-300" + class:rounded-lg={curve} + class:shadow-lg={shadow} + class:rounded-full={circle} class:opacity-0={loading} draggable="false" - on:load|once={() => (loading = false)} + use:imageLoad + on:image-load|once={() => (loading = false)} /> diff --git a/web/src/lib/components/faces-page/edit-name-input.svelte b/web/src/lib/components/faces-page/edit-name-input.svelte new file mode 100644 index 0000000000000..9eca6402b3d99 --- /dev/null +++ b/web/src/lib/components/faces-page/edit-name-input.svelte @@ -0,0 +1,42 @@ + + +
+ +
+ + + +
+
diff --git a/web/src/lib/constants.ts b/web/src/lib/constants.ts index 41563b72ada7c..4c77563811dd8 100644 --- a/web/src/lib/constants.ts +++ b/web/src/lib/constants.ts @@ -10,6 +10,7 @@ export enum AppRoute { ALBUMS = '/albums', ARCHIVE = '/archive', FAVORITES = '/favorites', + PEOPLE = '/people', PHOTOS = '/photos', EXPLORE = '/explore', SHARING = '/sharing', diff --git a/web/src/routes/(user)/explore/+page.server.ts b/web/src/routes/(user)/explore/+page.server.ts index 9eaf78ca1992a..8d4d074742f82 100644 --- a/web/src/routes/(user)/explore/+page.server.ts +++ b/web/src/routes/(user)/explore/+page.server.ts @@ -8,10 +8,11 @@ export const load = (async ({ locals, parent }) => { } const { data: items } = await locals.api.searchApi.getExploreData(); - + const { data: people } = await locals.api.personApi.getAllPeople(); return { user, items, + people, meta: { title: 'Explore' } diff --git a/web/src/routes/(user)/explore/+page.svelte b/web/src/routes/(user)/explore/+page.svelte index d8fc3adf2c0c4..11b515cc3c2c3 100644 --- a/web/src/routes/(user)/explore/+page.svelte +++ b/web/src/routes/(user)/explore/+page.svelte @@ -1,12 +1,13 @@ -
+
+ {#if people.length > 0} +
+
+

People

+ {#if data.people.length > MAX_ITEMS} + View All + {/if} +
+
+ {#each people as person (person.id)} + + +

{person.name}

+
+ {/each} +
+
+ {/if} + {#if places.length > 0}
diff --git a/web/src/routes/(user)/people/+page.server.ts b/web/src/routes/(user)/people/+page.server.ts new file mode 100644 index 0000000000000..8fabca2c4e0e0 --- /dev/null +++ b/web/src/routes/(user)/people/+page.server.ts @@ -0,0 +1,19 @@ +import { redirect } from '@sveltejs/kit'; +import type { PageServerLoad } from './$types'; + +export const load = (async ({ locals, parent }) => { + const { user } = await parent(); + if (!user) { + throw redirect(302, '/auth/login'); + } + + const { data: people } = await locals.api.personApi.getAllPeople(); + + return { + user, + people, + meta: { + title: 'People' + } + }; +}) satisfies PageServerLoad; diff --git a/web/src/routes/(user)/people/+page.svelte b/web/src/routes/(user)/people/+page.svelte new file mode 100644 index 0000000000000..1f83d9d46ecb0 --- /dev/null +++ b/web/src/routes/(user)/people/+page.svelte @@ -0,0 +1,48 @@ + + + + {#if data.people.length > 0} +
+
+ {#each data.people as person (person.id)} + + {/each} +
+
+ {:else} +
+
+ +

No people

+
+
+ {/if} +
diff --git a/web/src/routes/(user)/people/[personId]/+page.server.ts b/web/src/routes/(user)/people/[personId]/+page.server.ts new file mode 100644 index 0000000000000..b12f7a166beba --- /dev/null +++ b/web/src/routes/(user)/people/[personId]/+page.server.ts @@ -0,0 +1,21 @@ +import { redirect } from '@sveltejs/kit'; +import type { PageServerLoad } from './$types'; + +export const load = (async ({ locals, parent, params }) => { + const { user } = await parent(); + if (!user) { + throw redirect(302, '/auth/login'); + } + + const { data: person } = await locals.api.personApi.getPerson(params.personId); + const { data: assets } = await locals.api.personApi.getPersonAssets(params.personId); + + return { + user, + assets, + person, + meta: { + title: person.name || 'Person' + } + }; +}) satisfies PageServerLoad; diff --git a/web/src/routes/(user)/people/[personId]/+page.svelte b/web/src/routes/(user)/people/[personId]/+page.svelte new file mode 100644 index 0000000000000..7c22215396324 --- /dev/null +++ b/web/src/routes/(user)/people/[personId]/+page.svelte @@ -0,0 +1,113 @@ + + +{#if isMultiSelectionMode} + (multiSelectAsset = new Set())} + > + + + + + + + + + + +{:else} + goto(AppRoute.EXPLORE)} + /> +{/if} + + +
+ {#if isEditName} + handleNameChange(event.detail)} + on:blur={() => (isEditName = false)} + /> + {:else} + + + {/if} +
+ + +
+
+
+ +
+
+
diff --git a/web/src/routes/(user)/search/+page.svelte b/web/src/routes/(user)/search/+page.svelte index 4c50243d0bd56..96c9361e756f9 100644 --- a/web/src/routes/(user)/search/+page.svelte +++ b/web/src/routes/(user)/search/+page.svelte @@ -91,7 +91,7 @@
{:else}