Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Identify all known faces in an iPhoto photo' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Identify all known faces in an iPhoto photo
Authored by: tdoyle on Feb 05, '12 07:46:36AM

I've converted this script to access Aperture (version 3.2.2) databases. I also added a routine to grab all of the face names in the database. I believe this script to be safe, but be sure to back up your data before using.


-- Get Aperture Face Names
-- © 2012, Tim Doyle
-- Based on mr. applescript's iPhoto script, posted at http://hints.macworld.com/article.php?story=20090302060210294

tell application "Aperture"
activate

set the selected_items to the selection
repeat with z from 1 to the number of items in selected_items
set this_photo to item z of selected_items
set the query_results to my extract_face_record(this_photo)
if the the query_results is not false then
set AppleScript's text item delimiters to return

repeat with i from 1 to the count of the query_results
set the info_list to item i of the query_results
if i is 1 then
set the dialog_text to the info_list as rich text
else
set the dialog_text to the dialog_text & return & the info_list as rich text
end if
end repeat
set AppleScript's text item delimiters to ""
display dialog dialog_text
end if
end repeat
--select selected_items
end tell

-- Get and display the names of all Faces in the current Library
set the face_names to my get_all_faces()
repeat with z from 1 to the number of items in face_names
set this_face to item z of face_names
if z is 1 then
set the dialog_text to this_face
else
set the dialog_text to the dialog_text & return & this_face
end if
end repeat
tell application "Aperture"
display dialog dialog_text
end tell



on extract_face_record(this_photo)
set Aperture_library_path to do shell script "defaults read com.apple.Aperture LibraryPath"
-- expand the '~' if it's in there
set Aperture_library_path to do shell script "echo " & Aperture_library_path
if Aperture_library_path does not end with "/" then
set Aperture_library_path to Aperture_library_path & "/"
end if

set the Faces_database_path to Aperture_library_path & "Database/apdb/faces.db"
set the Aperture_database_path to Aperture_library_path & "Database/apdb/Library.apdb"

-- Get the UUID for this photo version
set the photoID to the id of this_photo

-- Look up the UUID for the master of this version
set the masterID to my SQL_command(Aperture_database_path, "select masterUuid from RKVersion where uuid=\"" & photoID & "\";")

-- Look up the face keys for all faces in this master photo, list them from left to right
set the face_keys to every paragraph of (my SQL_command(Faces_database_path, "select faceKey from RKDetectedFace where masterUuid=\"" & masterID & "\" AND rejected=0 AND ignore=0 ORDER BY topLeftX;"))

-- create a list for each face {short name, full name, email address}
set the face_records to {}
repeat with this_key in the face_keys
set this_face_info to {}
-- get name
set the short_name to my SQL_command(Faces_database_path, "select name from RKFaceName where faceKey=\"" & this_key & "\";")
if short_name is "" then
set short_name to "unnamed"
end if

set the end of this_face_info to the short_name

-- Additional fields, if desired

-- get full name
-- set the full_name to my SQL_command(Faces_database_path, "select fullName from RKFaceName where faceKey=\"" & this_key & "\";")
-- set the end of this_face_info to the full_name

-- get email address
-- set this_email to my SQL_command(Faces_database_path, "select email from RKFaceName where faceKey=\"" & this_key & "\";")
-- set the end of this_face_info to this_email

set the end of face_records to this_face_info
end repeat

return face_records
end extract_face_record


on get_all_faces()
-- Get the names of all faces in this database
set Aperture_library_path to do shell script "defaults read com.apple.Aperture LibraryPath"
-- expand the '~' if it's in there
set Aperture_library_path to do shell script "echo " & Aperture_library_path
if Aperture_library_path does not end with "/" then
set Aperture_library_path to Aperture_library_path & "/"
end if
set the Faces_database_path to Aperture_library_path & "Database/apdb/faces.db"
set the face_names to every paragraph of (my SQL_command(Faces_database_path, "select name from RKFaceName ORDER BY name;"))
end get_all_faces


on SQL_command(database_path, command_string)
return (do shell script "sqlite3 " & (quoted form of database_path) & " '" & command_string & "'")
end SQL_command



[ Reply to This | # ]