Identify all known faces in an iPhoto photo

Feb 19, '10 07:30:00AM

Contributed by: mr. applescript

The following AppleScript will display a dialog box showing the names of every identified face in a selected photo in iPhoto:

tell application "iPhoto"
  activate
  
  set the selected_items to (get the selection)
  repeat with z from 1 to the count of the selected_items
    set this_photo to item z of the selected_items
    select this_photo
    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 text
        else
          set the dialog_text to the dialog_text & return & return & the info_list as 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

on extract_face_record(this_photo)
  -- locate the current iPhoto library
  set iPhoto_library_path to do shell script "defaults read com.apple.iPhoto RootDirectory"
  -- expand the '~' if it's in there
  set iPhoto_library_path to do shell script "echo " & iPhoto_library_path
  -- add trailing slash if necessary
  if iPhoto_library_path does not end with "/" then
    set iPhoto_library_path to iPhoto_library_path & "/"
  end if
  
  -- locate the databases
  set the database_path to iPhoto_library_path & "face.db"
  set the iPhoto_database_path to iPhoto_library_path & "iPhotoMain.db"
  
  -- get the file path of the photo file
  tell application "iPhoto"
    if the class of this_photo is not photo then return false
    set the file_POSIX_path to the image path of this_photo
  end tell
  
  if the file_POSIX_path starts with iPhoto_library_path then
    set l to length of iPhoto_library_path
    set the file_POSIX_path to (text (l + 1) through -1 of the file_POSIX_path)
  end if
  
  -- get the file info record for the image file
  set the file_info_key to my SQL_command(iPhoto_database_path, "select primaryKey from SQFileInfo where relativePath=\"" & file_POSIX_path & "\";")
  if (count of paragraphs of the file_info_key) is greater than 1 then set the file_info_key to first paragraph of the file_info_key
  
  --get photo Key/ image key for the file info record
  set imageKey to my SQL_command(iPhoto_database_path, "select photoKey from SQFileImage where sqFileInfo=\"" & the file_info_key & "\";")
  
  --get detected faces for the image key
  set the face_keys to every paragraph of (my SQL_command(database_path, "select face_key from detected_face where image_key=\"" & imageKey & "\";"))
  
  -- 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(database_path, "select name from face_name where face_key=\"" & this_key & "\";")
    set the end of this_face_info to the short_name
    -- get full name
    set the full_name to my SQL_command(database_path, "select full_name from face_name where face_key=\"" & this_key & "\";")
    set the end of this_face_info to the full_name
    -- get email address
    set this_email to my SQL_command(database_path, "select email from face_name where face_key=\"" & 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 SQL_command(database_path, command_string)
  return (do shell script "sqlite3 " & (quoted form of database_path) & " '" & command_string & "'")
end SQL_command
To use this, save it as a script in your user's Library » Scripts » Applications » iPhoto folder; create any of those folders as necessary when saving. You can then run it directly from the Scripts menu within iPhoto, after first selecting a photo to work with.

[robg adds: For AppleScripters working with iPhoto, the above may be useful as an example of how to find face information for selected photos. I tested it, and it works as described to extract a list of faces for a selected photo.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090302060210294