Skip to content

fix(server): more robust person thumbnail generation - #17974

Merged
mertalev merged 6 commits into
mainfrom
fix/server-person-thumbnail-crop
May 6, 2025
Merged

mertalev merged 6 commits into
mainfrom
fix/server-person-thumbnail-crop

Conversation

@mertalev

@mertalev mertalev commented Apr 29, 2025 •

Copy link
Copy Markdown
Member

Description

There are several edge cases that can lead person thumbnail generation to fail:

  1. The image dimensions stored in the DB are not always accurate to what the decoded dimensions will be. This can happen for RAW assets in particular, which can report the cropped or uncropped dimensions, etc.

  2. Metadata extraction may not have finished. When importing face metadata, it is possible for person thumbnail generation to be queued and run first, which can lead to failed jobs.

  3. The metadata stored in the DB can be outdated. There is no guarantee that the data read from the DB is fully up-to-date.

  4. If the use of embedded previews is enabled, the embedded preview would only be used for detecting faces and not for generating the thumbnail. This discrepancy can lead to failure because of different dimensions and metadata.

  5. Faces can spill outside the image dimensions as the model predicts "where the face would be" when the face is cut off.

This PR tightens the logic here by relying more on the metadata that sharp itself reports at the time of the job, as well as considering embedded previews. It moves this job to the media service as it relates more to other thumbnail jobs and helper methods than it does to person-related jobs. Lastly, it clamps the bounding box to be within the image dimensions.

Fixes #12440
Fixes #12530
Fixes #14993

@alextran1502

Copy link
Copy Markdown
Member

Do you have RAW assets to reproduce these edge cases?

@mertalev

mertalev commented May 5, 2025

Copy link
Copy Markdown
Member Author

Do you have RAW assets to reproduce these edge cases?

The way I tested it is by setting the min detection threshold to 0.02 (after adjusting the min value for this in the dto and web client) and set the min recognized faces to 1. The result will be that just about any image you upload will have hundreds of detected faces and a person + thumbnail will be generated for each.

I tested by uploading this image that has especially different dimensions between the reported ImageSize (5120x3584) and what actually gets decoded (4624x3080). Of the 214 created people, only a few with faces that are 0 pixels tall or wide failed. This only happens because the detection threshold is much too low.

@mertalev
mertalev marked this pull request as ready for review May 5, 2025 19:11
@mertalev
mertalev requested a review from danieldietzler as a code owner May 5, 2025 19:11
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label May 5, 2025
Comment thread server/src/services/media.service.ts
@mertalev
mertalev force-pushed the fix/server-person-thumbnail-crop branch from d9f12e9 to 8cf77d3 Compare May 6, 2025 18:09
@mertalev
mertalev merged commit 2a80251 into main May 6, 2025
@mertalev
mertalev deleted the fix/server-person-thumbnail-crop branch May 6, 2025 18:18
savely-krasovsky pushed a commit to savely-krasovsky/immich that referenced this pull request Jun 8, 2025
* more robust person thumbnail generation

* clamp bounding boxes

* update sql

* no need to process invalid images after decoding

* cursed knowledge

* new line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:bugfix documentation Improvements or additions to documentation 🗄️server

Projects

None yet

2 participants