Bug 429021

Summary: Wrong image rotation when extracting face thumbs
Product: [Applications] digikam Reporter: Roberto <betokella>
Component: Faces-RecognitionAssignee: Digikam Developers <digikam-bugs-null>
Status: RESOLVED NOT A BUG    
Severity: minor CC: caulier.gilles, metzpinguin
Priority: NOR    
Version: 7.1.0   
Target Milestone: ---   
Platform: Microsoft Windows   
OS: Microsoft Windows   
Latest Commit: Version Fixed In: 7.2.0
Sentry Crash Report:

Description Roberto 2020-11-12 16:16:00 UTC
SUMMARY
I wanted to extract the faces thumbnails with the SQL code below, parsed and piped to magic for crop and resize based on image tag properties. With the thumbs in external JPG files, I noticed about 10% of them were wrong, not showing the face, but another part of the image. Both images with faces detected automatically and manually suffered with the issue, even thou auto recognized images were about 90% of the total images with the issue. The total images I looked into is about 3300. 

STEPS TO REPRODUCE
1. Rotate the affected images left/right
2. The image thumb shows erratic behavior, rotating in random direction (same as image, opposite, or completely upside down)
3. The thumb blinks and then rotates correctly
4. The image always rotates correctly. The boxes around faces are always correct

OBSERVED RESULT
Wrong face thumbs extracted from some images. Some image data sown below.

EXPECTED RESULT

SOFTWARE/OS VERSIONS
Windows: 10.0.19041
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
SQL code used to extract the face thumbs:
ATTACH 'E:\digiKam\digikam4.db' as ddk;
ATTACH 'E:\digiKam\thumbnails-digikam.db' as dtn;
SELECT tar.specificPath||ta.relativePath,ti.name,titp.imageid,titp.value 
FROM ddk.Albums AS ta, ddk.AlbumRoots AS tar, ddk.ImageTagProperties AS titp, ddk.Images AS ti 
WHERE ta.id=ti.album and tar.id=ta.albumRoot and ti.id=titp.imageid and titp.property='tagRegion' and titp.tagid IN (
	SELECT id FROM ddk.Tags WHERE name='XXX'
) ORDER BY ti.name
;
DETACH DATABASE ddk;
DETACH DATABASE dtn;

Sample image data, before and after the fix:
digiKam.db
	Tags with person name -> id=89
	Images name="2007-11-02 13h26 663" -> id=26236
	ImageTagProperties imageid=26236 and tagid=89 -> property=<rect x="1181" y="1038" width="192" height="251"/>

After fix (rotate back and forth 90 degrees)
	property=<rect x="1181" y="1038" width="192" height="251"/>
Comment 1 caulier.gilles 2020-11-12 17:43:50 UTC
It still reproducible using current 7.2.0-betaƩ installer for Windows ?

https://files.kde.org/digikam/

Gilles Caulier
Comment 2 Maik Qualmann 2020-11-12 18:12:42 UTC
I don't know if his problem is he is extracting an invalid / old face thumbnail. Or whether it is the bug that if there is a facial thumbnail already with the coordinates, no new and correct one is created. The face thumbnail in the DB is found using a path + rectangle identifier.

Maik
Comment 3 Roberto 2020-11-12 18:31:35 UTC
(In reply to Maik Qualmann from comment #2)
> I don't know if his problem is he is extracting an invalid / old face
> thumbnail. Or whether it is the bug that if there is a facial thumbnail
> already with the coordinates, no new and correct one is created. The face
> thumbnail in the DB is found using a path + rectangle identifier.
> 
> Maik

I am not getting the thumbs from the DB (could not fathom how...). I am getting the image paths and using ImageMagic to extract the rectangle indicated by the coordinates in ImageTagProperties.
/Roberto
Comment 4 Maik Qualmann 2020-11-12 18:43:08 UTC
But that's not a bug in digiKam. You have to understand that digiKam always stores the face coordinates internally with the aligned image. When writing metadata to the image, as is the standard in many programs, the face coordinates for the unaligned image are then saved. So you have to read the orientation flag in the DB and rotate the image accordingly with ImageMagick.

Maik
Comment 5 Roberto 2020-11-12 19:21:53 UTC
(In reply to Maik Qualmann from comment #4)
> But that's not a bug in digiKam. You have to understand that digiKam always
> stores the face coordinates internally with the aligned image. When writing
> metadata to the image, as is the standard in many programs, the face
> coordinates for the unaligned image are then saved. So you have to read the
> orientation flag in the DB and rotate the image accordingly with ImageMagick.
> 
> Maik

All cases are fixed after I rotate the images back and forth and extract the thumbs again, without changing any option in ImageMagic...
Comment 6 Maik Qualmann 2020-11-12 19:29:42 UTC
If you rotate the images back and forth in digiKam, they are actually rotated losslessly depending on the setting in digiKam setup and the orientation flag is then "normal". It is clear that the face coordinates then match the image.

Maik
Comment 7 Roberto 2020-11-12 19:39:24 UTC
Hummm... You mean I indirectly "fixed" the face coordinates so it 
matches the image and creating the thumbs in ImageMagick now works? 
Intriguing only a (comparably) few images are affected. Also intriguing 
the behavior in digiKam when you rotate the image back and forth and the 
thumbnail flips in random directions first before rotation according to 
the image... It doesn't occur with "the correct" images.

On 2020-11-12 16:29, Maik Qualmann wrote:
> https://bugs.kde.org/show_bug.cgi?id=429021
> 
> Maik Qualmann <metzpinguin@gmail.com> changed:
> 
>             What    |Removed                     |Added
> ----------------------------------------------------------------------------
>               Status|REPORTED                    |RESOLVED
>           Resolution|---                         |NOT A BUG
>     Version Fixed In|                            |7.2.0
> 
> --- Comment #6 from Maik Qualmann <metzpinguin@gmail.com> ---
> If you rotate the images back and forth in digiKam, they are actually rotated
> losslessly depending on the setting in digiKam setup and the orientation flag
> is then "normal". It is clear that the face coordinates then match the image.
> 
> Maik
> 

/Roberto
Comment 8 Roberto 2020-11-12 23:57:36 UTC
(In reply to Beto Kella from comment #7)
> Hummm... You mean I indirectly "fixed" the face coordinates so it 
> matches the image and creating the thumbs in ImageMagick now works? 
> Intriguing only a (comparably) few images are affected. Also intriguing 
> the behavior in digiKam when you rotate the image back and forth and the 
> thumbnail flips in random directions first before rotation according to 
> the image... It doesn't occur with "the correct" images.
> 
> On 2020-11-12 16:29, Maik Qualmann wrote:
> > https://bugs.kde.org/show_bug.cgi?id=429021
> > 
> > Maik Qualmann <metzpinguin@gmail.com> changed:
> > 
> >             What    |Removed                     |Added
> > ----------------------------------------------------------------------------
> >               Status|REPORTED                    |RESOLVED
> >           Resolution|---                         |NOT A BUG
> >     Version Fixed In|                            |7.2.0
> > 
> > --- Comment #6 from Maik Qualmann <metzpinguin@gmail.com> ---
> > If you rotate the images back and forth in digiKam, they are actually rotated
> > losslessly depending on the setting in digiKam setup and the orientation flag
> > is then "normal". It is clear that the face coordinates then match the image.
> > 
> > Maik
> > 
> 
> /Roberto

Adding -auto-orient option to ImageMagic did the trick. Sorry for opening as a bug in digiKam. The behavior described above created bias.