Created
January 25, 2011 15:06
-
-
Save algrs/795034 to your computer and use it in GitHub Desktop.
Relationship graphs from iPhoto '11 face detections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
# sudo gem install ruby-graphviz | |
# sudo port install graphviz | |
require 'graphviz' | |
# NB: The following line deals with the case where your iPhoto library is not in the usual place. | |
# If you have more than one iPhoto libraries, please set this manually. | |
IPHOTO_LIBRARY = `mdfind "kMDItemKind == 'iPhoto Library' && kMDItemContentTypeTree == com.apple.package"`.strip | |
LIBRARY = File.join(IPHOTO_LIBRARY, "Database", "Library.apdb") | |
FACES = File.join(IPHOTO_LIBRARY, "Database", "Faces.db") | |
def select(db,sql) | |
res = `sqlite3 '#{db}' '#{sql}'` | |
res.split("\n").map{|x|x.split("|")} | |
end | |
# images = Hash[select(LIBRARY,"select uuid,name from RKMaster;")] | |
sql = "select df.masterUuid, fn.name from RKFaceName as fn left join RKDetectedFace as df on fn.faceKey = df.faceKey where df.ignore = 0;" | |
faces = select(FACES,sql) | |
graph = Hash.new{|h,k|h[k] = []} | |
faces.each{|k,v| graph[k] << v} | |
counts = Hash.new(0) | |
graph = graph.reject{|k,v| v.length == 1} | |
graph.each do |k,v| | |
v.combination(2).each {|pair| counts[pair] += 1} | |
end | |
g = GraphViz::new( "structs", "type" => "graph" ) | |
def map_color(count,min,max) | |
v = (count.to_f - min.to_f) / (max.to_f - min.to_f) | |
v = v ** (0.5) | |
v = (v * 255).round | |
r = v.to_s(16).rjust(2,"0") | |
b = (255-v).to_s(16).rjust(2,"0") | |
g = "00" | |
"##{r}#{g}#{b}" | |
end | |
max_count = counts.values.max | |
min_count = counts.values.min | |
counts.each do |pair, count| | |
color = map_color(count,min_count,max_count) | |
e = g.add_edge(pair[0],pair[1], :color => color) | |
end | |
puts g.output( :none => String ) | |
g.output( :png => "iphoto_faces_graph.png" ) | |
`open iphoto_faces_graph.png` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment