forked from afeld/mustachio
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
81 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |