Main Menu

Face tags duplicates

Started by wildcowboy, January 17, 2017, 11:53:36 AM

Previous topic - Next topic

wildcowboy

Hello,

I have noticed that some of my pictures have duplicated face tags.
See an extract of the output of "exiftool -a -G1 -s" command below. Software (Digikam) shows only one "Jack" in tags/people but when you preview the image and hover mouse over the face you can see two face regions. One face area would cover just the face (from eyes to lips, no hair), the second one would cover the whole head (with hair). Each region has the name Jack.
I was wondering if there is any automated way to find all such pictures and get rid of duplicate face tags using ExifTool. I don't really care which one of them should be left/deleted.
So far I found this topic: https://exiftool.org/forum/index.php/topic,4137.msg19595.html#msg19595 Looks like it is possible to mark all the duplicates via a script and process them manually after.

Please share if you have a better idea.

[XMP-digiKam]   TagsList                        : People/Jack, Places/zoo
[XMP-microsoft] LastKeywordXMP                  : People/Jack, Places/zoo
[XMP-microsoft] LastKeywordIPTC                 : Places/zoo, Jack
[XMP-acdsee]    Categories                      : <Categories><Category Assigned="0">People<Category Assigned="1">Jack</Category></Category><Category Assigned="0">Places<Category Assigned="1">zoo</Category></Categories>
[XMP-mwg-rs]    RegionAppliedToDimensionsW      : 2048
[XMP-mwg-rs]    RegionAppliedToDimensionsH      : 1536
[XMP-mwg-rs]    RegionAppliedToDimensionsUnit   : pixel
[XMP-mwg-rs]    RegionName                      : Jack, Jack
[XMP-mwg-rs]    RegionType                      : Face, Face
[XMP-mwg-rs]    RegionAreaX                     : 0.805176, 0.802246
[XMP-mwg-rs]    RegionAreaY                     : 0.470703, 0.501953
[XMP-mwg-rs]    RegionAreaW                     : 0.198242, 0.109375
[XMP-mwg-rs]    RegionAreaH                     : 0.316406, 0.145833
[XMP-mwg-rs]    RegionAreaUnit                  : normalized, normalized
[XMP-MP]        RegionRectangle                 : 0.706055, 0.3125, 0.198242, 0.316406, 0.747559, 0.429036, 0.109375, 0.145833
[XMP-MP]        RegionPersonDisplayName         : Jack, Jack
[XMP-lr]        HierarchicalSubject             : People|Jack, Places|zoo
[XMP-mediapro]  CatalogSets                     : People|Jack, Places|zoo
[XMP-dc]        Subject                         : Jack, zoo
[IPTC]          Keywords                        : Jack, zoo


StarGeek

#1
Here's a config file, test it out to make sure it works for you.  I have not tested it extensively but it worked on the couple of test case I used.

It will remove any regions with duplicate RegionName tags with no guarantee as to which one is removed.  Your command to use it would be:
exiftool -config /path/to/ExifTool-RemoveRegionInfoDuplicates.config "-regioninfo<RemoveRegionInfoDuplicates" FileOrDir

Edit: removed, see updated below
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

StarGeek

Oops, I just notice you had MP regions in there as well.  This config file only deals with MWG regions.  I'll see what I can do to remedy that.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

StarGeek

Updated to include RegionInfoMP

Command for RegionInfoMP
exiftool -config /path/to/ExifTool-RemoveRegionInfoDuplicates.config "-regioninfoMP<RemoveRegionInfoMPDuplicates" FileOrDir

#------------------------------------------------------------------------------
# File:         RemoveRegionInfoDuplicates.config
#
# Description:  User-defined Composite tag definitions to remove MWG region tags (Metadata Working Group
#               region, used by Picasa) with the same Name.  Does not prioritize which region to remove
#
# Revisions:    2017/01/17 - Bryan K. Williams AKA StarGeek Created

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        RemoveRegionInfoDuplicates =>{
            Require => 'RegionInfo',
            ValueConv => q{
                my @RL = @{$val[0]{RegionList}};
                my %seen;
                my @R2 =grep !$seen{$_->{Name}}++, @RL;
                $val[0]->{RegionList}= \@R2;
                return ((@RL != @R2) ? $val[0]:undef);
            },
        }, #End
        RemoveRegionInfoMPDuplicates =>{
            Require => 'RegionInfoMP',
            ValueConv => q{
                my @RL = @{$val[0]{Regions}};
                my %seen;
                my @R2 = grep !$seen{$_->{PersonDisplayName}}++, @RL;
                $val[0]->{Regions} = \@R2;
                return ((@RL != @R2) ? $val[0]:undef);
            },
        }, #End
#
    },
);
1;  #end

"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

wildcowboy

StarGeek, Thank you a lot!
I will try it out in next couple of days and will post the results here.

bungaman

This is a great script!    :)

I noticed that it doesn't update the "Person In Image" tag that is usually a mirror of the MP face tags.  Would it be possible to extend this script to update that tag?  Or am I missing something?

StarGeek

At the time of writing this config, PersonInImage wasn't used by any software I had known of.  And it's in a completely different namespace, XMP-iptcExt instead of XMP-mwg-rs or XMP-MP.

But you can update PersonInImage after removing the duplicate regions with
"-PersonInImage<RegionName"
or
"-PersonInImage<RegionPersonDisplayName"
depending upon what region type you have in the file.

Or you can use the NoDups helper function if there are already duplicates in PersonInImage.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

ericnotthered

I'm struggling with a large number of duplicated face tags and I am pleased to see this thread. 

However, there is one significant problem for me:  I use Digikam, which allows you to mark face regions as "Ignored".  This allows you to ignore these regions if you perform additional iterations of face detection or recognition on photos.  Of course, there can be multiple such ignored faces in a given photo.

@StarGeek, what would be very helpful would be the ability to skip de-duplication of regions with the name "Ignored".  Is there any chance that you would consider updating the script to offer this?  It would be greatly appreciated.

 


StarGeek

It's been so long since I wrote that, I can't quite figure out what I did.  Well, I think I do, but I'll have to go over it.  (I should have put in comments describing it)

But I think all that has to be done is add a check to the grep line.  I'll have to test it but I think it would be something like this, but I'm not sure

my @R2 =grep ($_=~/^ignored$/i or !$seen{$_->{Name}}++), @RL;
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

ericnotthered

Thank you for looking into this. 

On further consideration, however, I think I may brush up on my python and write something to parse the xmp sidecar files and resolve these issues.  I would like to be able to put some logic into which region to select.  Looks like a longer term project.

StarGeek

Quote from: ericnotthered on November 12, 2023, 04:31:56 PMI would like to be able to put some logic into which region to select.  Looks like a longer term project.

Yeah, this config doesn't do any comparison about the actual regions. It just removes duplicates.  The OP didn't care which one was saved.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype