paperclip-facecrop 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,158 +1,148 @@
1
- require 'opencv'
1
+ require 'opencv_ext'
2
2
 
3
- class OpenCV::CvAvgComp
4
- def to_s
5
- "#{self.x},#{self.y}-#{self.width}x#{self.height}"
6
- end
7
- end
8
-
9
- class Paperclip::FaceCrop < Paperclip::Thumbnail
3
+ module Paperclip
4
+ class FaceCrop < Paperclip::Thumbnail
10
5
 
11
- @@debug = false
6
+ @@debug = false
12
7
 
13
- cattr_accessor :classifiers
14
- cattr_accessor :debug
8
+ cattr_accessor :classifiers
9
+ cattr_accessor :debug
15
10
 
16
- def initialize(file, options = {}, attachment = nil)
17
- super(file, options, attachment)
11
+ def initialize(file, options = {}, attachment = nil)
12
+ super(file, options, attachment)
18
13
 
19
- faces_regions = []
20
- faces_parts_regions = []
14
+ faces_regions = []
15
+ faces_parts_regions = []
21
16
 
22
- raise "No classifiers were defined" if self.classifiers.nil?
17
+ raise "No classifiers were defined" if self.classifiers.nil?
23
18
 
24
- image = OpenCV::IplImage.load(file.path, 1)
19
+ image = OpenCV::IplImage.load(file.path, 1)
25
20
 
26
- faces_regions = detect_regions(image, self.classifiers[:face])
21
+ faces_regions = detect_regions(image, self.classifiers[:face])
27
22
 
28
- #Paperclip::FaceCrop.classifiers[:nose]
29
- unless self.classifiers[:parts].nil?
30
- faces_parts_regions = detect_regions(image, self.classifiers[:parts], OpenCV::CvColor::Red)
23
+ #Paperclip::FaceCrop.classifiers[:nose]
24
+ unless self.classifiers[:parts].nil?
25
+ faces_parts_regions = detect_regions(image, self.classifiers[:parts], OpenCV::CvColor::Red)
31
26
 
32
- faces_regions.reject! do |face_region|
33
- region = faces_parts_regions.detect do |part_region|
34
- regions_overlap?(face_region, part_region)
35
- end
27
+ faces_regions.reject! do |face_region|
28
+ region = faces_parts_regions.detect do |part_region|
29
+ face_region.collide?(part_region)
30
+ end
36
31
 
37
- region.nil?
32
+ region.nil?
33
+ end
38
34
  end
39
- end
40
35
 
41
- x_coords = []
42
- y_coords = []
43
- widths = []
44
- heights = []
36
+ x_coords = []
37
+ y_coords = []
38
+ widths = []
39
+ heights = []
45
40
 
46
- faces_regions.each do |region|
47
- x_coords << region.top_left.x << region.bottom_right.x
48
- y_coords << region.top_left.y << region.bottom_right.y
49
- widths << region.width
50
- heights << region.height
51
- end
41
+ faces_regions.each do |region|
42
+ x_coords << region.top_left.x << region.bottom_right.x
43
+ y_coords << region.top_left.y << region.bottom_right.y
44
+ widths << region.width
45
+ heights << region.height
46
+ end
52
47
 
53
- @has_faces = faces_regions.size > 0
48
+ @has_faces = faces_regions.size > 0
54
49
 
55
- if @has_faces
56
- @top_left_x = x_coords.min
57
- @top_left_y = y_coords.min
58
- @bottom_right_x = x_coords.max
59
- @bottom_right_y = y_coords.max
50
+ if @has_faces
51
+ @top_left_x = x_coords.min
52
+ @top_left_y = y_coords.min
53
+ @bottom_right_x = x_coords.max
54
+ @bottom_right_y = y_coords.max
60
55
 
61
- # average faces areas
62
- average_face_width = widths.sum / widths.size
63
- average_face_height = heights.sum / heights.size
56
+ # average faces areas
57
+ average_face_width = widths.sum / widths.size
58
+ average_face_height = heights.sum / heights.size
64
59
 
65
- # calculating the surrounding margin of the area that covers all the found faces
66
- #
60
+ # calculating the surrounding margin of the area that covers all the found faces
61
+ #
67
62
 
68
- # new width
69
- @top_left_x -= average_face_width / 2
70
- @bottom_right_x += average_face_width / 2
63
+ # new width
64
+ @top_left_x -= average_face_width / 2
65
+ @bottom_right_x += average_face_width / 2
71
66
 
72
- # new height
73
- @top_left_y -= average_face_height / 2
74
- @bottom_right_y += average_face_height / 1.6
67
+ # new height
68
+ @top_left_y -= average_face_height / 2
69
+ @bottom_right_y += average_face_height / 1.6
75
70
 
76
- calculate_bounds
77
-
78
- # if the new area is smaller than the target geometry, it's scaled so the final image isn't resampled
79
- #
80
- if @faces_width < @target_geometry.width
81
- delta_width = (@target_geometry.width - @faces_width) / 2
82
- @top_left_x -= delta_width
83
- @bottom_right_x += delta_width
84
71
  calculate_bounds
85
- end
86
72
 
87
- #raise (@target_geometry.height > 0 and @faces_height < @target_geometry.height).to_s
73
+ # if the new area is smaller than the target geometry, it's scaled so the final image isn't resampled
74
+ #
75
+ if @faces_width < @target_geometry.width
76
+ delta_width = (@target_geometry.width - @faces_width) / 2
77
+ @top_left_x -= delta_width
78
+ @bottom_right_x += delta_width
79
+ calculate_bounds
80
+ end
88
81
 
89
- if (@target_geometry.height > 0 and @faces_height < @target_geometry.height)
90
- delta_height = (@target_geometry.height - @faces_height) / 2
91
- @top_left_y -= delta_height
92
- @bottom_right_y += delta_height
93
- calculate_bounds
94
- end
82
+ #raise (@target_geometry.height > 0 and @faces_height < @target_geometry.height).to_s
95
83
 
96
- @faces_height = @faces_width if @target_geometry.height == 0
84
+ if (@target_geometry.height > 0 and @faces_height < @target_geometry.height)
85
+ delta_height = (@target_geometry.height - @faces_height) / 2
86
+ @top_left_y -= delta_height
87
+ @bottom_right_y += delta_height
88
+ calculate_bounds
89
+ end
97
90
 
98
- @current_geometry = Paperclip::Geometry.new(@faces_width, @faces_height)
99
- end
91
+ @faces_height = @faces_width if @target_geometry.height == 0
92
+
93
+ @current_geometry = Paperclip::Geometry.new(@faces_width, @faces_height)
94
+ end
100
95
 
101
- end
96
+ end
102
97
 
103
98
 
104
- def transformation_command
105
- return super unless @has_faces
99
+ def transformation_command
100
+ return super unless @has_faces
106
101
 
107
- scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
108
- faces_crop = "%dx%d+%d+%d" % [@faces_width, @faces_height, @top_left_x, @top_left_y]
102
+ scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
103
+ faces_crop = "%dx%d+%d+%d" % [@faces_width, @faces_height, @top_left_x, @top_left_y]
109
104
 
110
- trans = []
111
- trans << "-crop" << %["#{faces_crop}"] << "+repage"
112
- trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
113
- trans << "-crop" << %["#{crop}"] << "+repage" if crop
114
- trans
115
- end
105
+ trans = []
106
+ trans << "-crop" << %["#{faces_crop}"] << "+repage"
107
+ trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
108
+ trans << "-crop" << %["#{crop}"] << "+repage" if crop
109
+ trans
110
+ end
116
111
 
117
- private
112
+ private
118
113
 
119
- # calculate_bounds
120
- #
121
- def calculate_bounds
122
- @top_left_x = 0 if @top_left_x < 0
123
- @bottom_right_x = @current_geometry.width if @bottom_right_x > @current_geometry.width
114
+ # calculate_bounds
115
+ #
116
+ def calculate_bounds
117
+ @top_left_x = 0 if @top_left_x < 0
118
+ @bottom_right_x = @current_geometry.width if @bottom_right_x > @current_geometry.width
124
119
 
125
- @top_left_y = 0 if @top_left_y < 0
126
- @bottom_right_y = @current_geometry.height if @bottom_right_y > @current_geometry.height
120
+ @top_left_y = 0 if @top_left_y < 0
121
+ @bottom_right_y = @current_geometry.height if @bottom_right_y > @current_geometry.height
127
122
 
128
- @faces_width = @bottom_right_x - @top_left_x
129
- @faces_height = @bottom_right_y - @top_left_y
130
- end
123
+ @faces_width = @bottom_right_x - @top_left_x
124
+ @faces_height = @bottom_right_y - @top_left_y
125
+ end
131
126
 
132
- # detect_regions
133
- #
134
- def detect_regions(image, classifiers, color = OpenCV::CvColor::Blue)
135
- regions = []
127
+ # detect_regions
128
+ #
129
+ def detect_regions(image, classifiers, color = OpenCV::CvColor::Blue)
130
+ regions = []
136
131
 
137
- classifiers.each do |classifier|
138
- detector = OpenCV::CvHaarClassifierCascade::load(classifier)
139
- detector.detect_objects(image) do |region|
140
- regions << region
141
- image.rectangle!(region.top_left, region.bottom_right, :color => color) if self.debug
132
+ classifiers.each do |classifier|
133
+ detector = OpenCV::CvHaarClassifierCascade::load(classifier)
134
+ detector.detect_objects(image) do |region|
135
+ regions << region
136
+ image.rectangle!(region.top_left, region.bottom_right, :color => color) if self.debug
137
+ end
142
138
  end
143
- end
144
139
 
145
- if self.debug
146
- image.save_image(@file.path)
147
- Rails.logger.info(regions)
148
- end
140
+ if self.debug
141
+ image.save_image(@file.path)
142
+ Rails.logger.info(regions)
143
+ end
149
144
 
150
- regions
151
- end
152
-
153
- def regions_overlap?(r1, r2)
154
- dx = (r2.x - r1.x).abs + r2.width
155
- dy = (r2.y - r1.y).abs + r2.height;
156
- return (dx < r1.width + r2.width && dy < r1.height + r2.height);
145
+ regions
146
+ end
157
147
  end
158
148
  end
@@ -1,9 +1,9 @@
1
1
  # encoding: utf-8
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require 'version'
3
+
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "paperclip-facecrop"
6
- s.version = Paperclip::FaceCrop::VERSION
6
+ s.version = "0.0.3"
7
7
  s.authors = ["Borja Martín"]
8
8
  s.description = %q{Paperclip processor that is aware of the faces detected on the image so that they don't get cropped or aren't shown too small while generating the thumbnails}
9
9
  s.summary = %q{Paperclip processor that is aware of the faces found on the image}
@@ -13,5 +13,5 @@ Gem::Specification.new do |s|
13
13
  s.files = `git ls-files`.split("\n")
14
14
  s.has_rdoc = false
15
15
  s.add_runtime_dependency("paperclip")
16
- s.add_runtime_dependency("opencv", "~> 0.0.6")
16
+ s.add_runtime_dependency("opencv", "= 0.0.6")
17
17
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Borja Mart\xC3\xADn"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-06 00:00:00 +02:00
17
+ date: 2011-05-10 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -36,7 +36,7 @@ dependencies:
36
36
  requirement: &id002 !ruby/object:Gem::Requirement
37
37
  none: false
38
38
  requirements:
39
- - - ~>
39
+ - - "="
40
40
  - !ruby/object:Gem::Version
41
41
  segments:
42
42
  - 0
@@ -61,7 +61,6 @@ files:
61
61
  - README_example_b.jpg
62
62
  - Rakefile
63
63
  - lib/paperclip-facecrop.rb
64
- - lib/version.rb
65
64
  - paperclip-facecrop.gemspec
66
65
  has_rdoc: true
67
66
  homepage: http://github.com/dagi3d/paperclip-facecrop
data/lib/version.rb DELETED
@@ -1,5 +0,0 @@
1
- class Paperclip
2
- class FaceCrop
3
- VERSION = "0.0.2"
4
- end
5
- end