ExifTool Forum

ExifTool => Newbies => Topic started by: wildcowboy on January 17, 2017, 11:53:36 AM

Title: Face tags duplicates
Post by: wildcowboy on January 17, 2017, 11:53:36 AM
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

Title: Re: Face tags duplicates
Post by: StarGeek on January 17, 2017, 02:07:37 PM
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
Title: Re: Face tags duplicates
Post by: StarGeek on January 17, 2017, 02:10:05 PM
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.
Title: Re: Face tags duplicates
Post by: StarGeek on January 17, 2017, 02:54:38 PM
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

Title: Re: Face tags duplicates
Post by: wildcowboy on January 17, 2017, 05:18:34 PM
StarGeek, Thank you a lot!
I will try it out in next couple of days and will post the results here.
Title: Re: Face tags duplicates
Post by: bungaman on April 15, 2022, 11:29:15 AM
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?
Title: Re: Face tags duplicates
Post by: StarGeek on April 15, 2022, 12:18:16 PM
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 (https://exiftool.org//exiftool_pod.html#Helper-functions) if there are already duplicates in PersonInImage.