Skip to content

Commit

Permalink
moved shortcuts to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Jun 18, 2011
1 parent 1bc7273 commit 623d43b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 81 deletions.
82 changes: 1 addition & 81 deletions lib/mustachio.rb
@@ -1,86 +1,6 @@
require 'magickly'
require 'image_size'
require 'face'

Magickly.dragonfly.configure do |c|
c.log_commands = true

c.analyser.add :face_data do |temp_object|
Mustachio.face_client.faces_detect(:file => temp_object.file, :attributes => 'none')['photos'].first
end

c.analyser.add :face_data_as_px do |temp_object|
data = Mustachio.face_client.faces_detect(:file => temp_object.file, :attributes => 'none')['photos'].first # TODO use #face_data

new_tags = []
data['tags'].map do |face|
has_all_attrs = Mustachio::FACE_POS_ATTRS.all? do |pos_attr|
if face[pos_attr]
face[pos_attr]['x'] *= (data['width'] / 100.0)
face[pos_attr]['y'] *= (data['height'] / 100.0)
true
else
# face attribute missing
false
end
end

new_tags << face if has_all_attrs
end

data['tags'] = new_tags
data
end

c.job :mustachify do |stache_num_param|
photo_data = @job.face_data_as_px
width = photo_data['width']

commands = ['-virtual-pixel transparent']
photo_data['tags'].each do |face|
stache_num = case stache_num_param
when true
0
when 'true'
0
when 'rand'
rand(Mustachio.mustaches.size)
else
stache_num_param.to_i
end

mustache = Mustachio.mustaches[stache_num]

# perform transform such that the mustache is the height
# of the upper lip, and the bottom-center of the stache
# is mapped to the center of the mouth
rotation = Math.atan(
( face['mouth_right']['y'] - face['mouth_left']['y'] ).to_f /
( face['mouth_right']['x'] - face['mouth_left']['x'] ).to_f
) / Math::PI * 180.0
desired_height = Math.sqrt(
( face['nose']['x'] - face['mouth_center']['x'] ).to_f**2 +
( face['nose']['y'] - face['mouth_center']['y'] ).to_f**2
)
mouth_intersect = mustache['height'] - mustache['mouth_overlap']
scale = desired_height / mouth_intersect

srt_params = [
[ mustache['width'] / 2.0, mouth_intersect - mustache['vert_offset'] ].map{|e| e.to_i }.join(','), # bottom-center of stache
scale, # scale
rotation, # rotate
[ face['mouth_center']['x'], face['mouth_center']['y'] ].map{|e| e.to_i }.join(',') # middle of mouth
]
srt_params_str = srt_params.join(' ')

commands << "\\( #{mustache['file_path']} +distort SRT '#{srt_params_str}' \\)"
end
commands << "-flatten"

command_str = commands.join(' ')
process :convert, command_str
end
end
require File.join(File.dirname(__FILE__), 'mustachio', 'shortcuts')

module Mustachio
FACE_POS_ATTRS = ['center', 'eye_left', 'eye_right', 'mouth_left', 'mouth_center', 'mouth_right', 'nose']
Expand Down
81 changes: 81 additions & 0 deletions lib/mustachio/shortcuts.rb
@@ -0,0 +1,81 @@
require 'face'

Magickly.dragonfly.configure do |c|
c.log_commands = true

c.analyser.add :face_data do |temp_object|
Mustachio.face_client.faces_detect(:file => temp_object.file, :attributes => 'none')['photos'].first
end

c.analyser.add :face_data_as_px do |temp_object|
data = Mustachio.face_client.faces_detect(:file => temp_object.file, :attributes => 'none')['photos'].first # TODO use #face_data

new_tags = []
data['tags'].map do |face|
has_all_attrs = Mustachio::FACE_POS_ATTRS.all? do |pos_attr|
if face[pos_attr]
face[pos_attr]['x'] *= (data['width'] / 100.0)
face[pos_attr]['y'] *= (data['height'] / 100.0)
true
else
# face attribute missing
false
end
end

new_tags << face if has_all_attrs
end

data['tags'] = new_tags
data
end

c.job :mustachify do |stache_num_param|
photo_data = @job.face_data_as_px
width = photo_data['width']

commands = ['-virtual-pixel transparent']
photo_data['tags'].each do |face|
stache_num = case stache_num_param
when true
0
when 'true'
0
when 'rand'
rand(Mustachio.mustaches.size)
else
stache_num_param.to_i
end

mustache = Mustachio.mustaches[stache_num]

# perform transform such that the mustache is the height
# of the upper lip, and the bottom-center of the stache
# is mapped to the center of the mouth
rotation = Math.atan(
( face['mouth_right']['y'] - face['mouth_left']['y'] ).to_f /
( face['mouth_right']['x'] - face['mouth_left']['x'] ).to_f
) / Math::PI * 180.0
desired_height = Math.sqrt(
( face['nose']['x'] - face['mouth_center']['x'] ).to_f**2 +
( face['nose']['y'] - face['mouth_center']['y'] ).to_f**2
)
mouth_intersect = mustache['height'] - mustache['mouth_overlap']
scale = desired_height / mouth_intersect

srt_params = [
[ mustache['width'] / 2.0, mouth_intersect - mustache['vert_offset'] ].map{|e| e.to_i }.join(','), # bottom-center of stache
scale, # scale
rotation, # rotate
[ face['mouth_center']['x'], face['mouth_center']['y'] ].map{|e| e.to_i }.join(',') # middle of mouth
]
srt_params_str = srt_params.join(' ')

commands << "\\( #{mustache['file_path']} +distort SRT '#{srt_params_str}' \\)"
end
commands << "-flatten"

command_str = commands.join(' ')
process :convert, command_str
end
end

0 comments on commit 623d43b

Please sign in to comment.