Moving faces to keywords with shell processing

Started by danb, May 27, 2015, 02:18:45 PM

Previous topic - Next topic

danb

Hello,

I love exiftool, has really revolutionised the way I store my photos! One thing that I am getting stuck on is how to move Faces (regionnames) to Keywords after some processing.

I would like all of the RegionNames (eg: John Smith, Tom Jones,...), with a prefix of "P/" (standing for person in picture), to be added to existing keywords (eg: P/John Smith, P/Tom Jones,...). This is so I can search images using Spotlight and can distinguish between photos with somebody in from a tag that I will add of a photo being taken by someone.

I have a little bit of code, but I think it is way too complicated (especially for how long it took me!).

Really appreciate any help! Thanks all in advance,

Dan

Some more notes:
- I would like to run it recursively
- How would it handle photos with no regions or non-jpg
- How does it ensure no duplicate tags
- It would be good if it also deleted all keywords starting with P/

Script so far (NOT WORKING!):

#!/bin/bash
# Get Region names
PeopleKeywords="$(exiftool /Users/danb/Pictures/D/Test/testphoto.jpg -RegionName -s3)"
echo $PeopleKeywords

# Add first P/
PeopleKeywords=P/$PeopleKeywords
echo
echo $PeopleKeywords

# Do Substitute
PeopleKeywords=${PeopleKeywords//, /, P/}
echo
echo $PeopleKeywords

# Print with substitute
echo 'Next line is exiftool'
exiftool /Users/danb/Pictures/D/Test/testphoto.jpg -keywords+="$PeopleKeywords" -sep ", "


Outputs as expected until

'Next line is exiftool'
Error: File not found - "
    1 image files updated
    1 files weren't updated due to errors


Checking keywords returns:
Keywords                        : "??, P/Arran, '??, "??

StarGeek

This command will add RegionName to IPTC:Keywords and prefix P/:
exiftool -sep '||' '-keywords<${RegionName;$_=join("||", map {"P/" . $_ } split/\|\|/ ) }' DIR

Add -r to recurse.  I used the two pipe characters instead of the default ", " because I've had too many occasions with names have ", " in them. 

That command doesn't deal with duplicates, but here's a command you could run afterwards to remove duplicates:
exiftool -sep '||' '-keywords<${keywords;my %seen; $_=join("||",grep { ! $seen{ $_ }++ } split /\|\|/)}' DIR

Since both of these commands can be run on entire directories and recursive, it would be much quicker than your script which runs ExifTool for each file.

Since it looks like you're using linux, I tried to swap the single and double quotes appropriately, but I'm not sure if it's right.

Edit: Slight change to command to use map
"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

danb

Thank you so much StarGeek! Good call on the two pipe, I probably do have names like that also. I am actually on a Mac, but the code seemed to like it.

The only other question I have is: how to make the code make additions to keywords in place rather than overwriting?

Your help is really appreciated!

danb

Got it! Plus was needed.

exiftool -sep '||' '-keywords+<${RegionName;$_=join("||", map {"P/" . $_ } split/\|\|/ ) }' DIR

Really impressed with this tool and the forum's support! I am getting another friend on board with it tomorrow.

Ps. To make this a keyword circle of life, it would be interesting to know how to delete any keywords that begin with P/...

StarGeek

Quote from: danb on May 27, 2015, 04:45:41 PM
Got it! Plus was needed.

exiftool -sep '||' '-keywords+<${RegionName;$_=join("||", map {"P/" . $_ } split/\|\|/ ) }' DIR

Ooops, sorry 'bout that.

QuotePs. To make this a keyword circle of life, it would be interesting to know how to delete any keywords that begin with P/...

Try this
exiftool -sep "||" "-Keywords<${keywords; $_= join('||',grep(!/^P\//, split/\|\|/)) }" DIR

There may be better ways of doing all of these commands and if so, Phil will probably post something.
"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

Phil Harvey

Quote from: StarGeek on May 27, 2015, 05:54:35 PM
There may be better ways of doing all of these commands and if so, Phil will probably post something.

StarGeek's Perl wizardry is truly impressive.  I can't think of a better solution.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

No wizardry involved, it's all google+copy/paste, sorta.  These are variations on user defined tags and other commands I've had to work up before and saved for future reference.
"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