face_cropper 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/face_cropper.rb +25 -28
- data/lib/face_cropper/aws_rekognition_face_detector.rb +24 -0
- data/lib/face_cropper/face_box.rb +36 -0
- data/lib/face_cropper/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 702e3d2fbe93f586b47c7e3f2848f05729cc81b5
|
4
|
+
data.tar.gz: c5b462c7ca47f0d8b535a9cb16074f62de01cbb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d730b640816b8195ee5ccf5e558838cefc9462eef2e2dc9ef6f1e1aa7aa237cdaec11d201bf77069986c4a43a826ad92f8a31381a887e75560561f2b2d3412f
|
7
|
+
data.tar.gz: 5a1fa3cab16bbefb0294cd8f8aaf141ea5fdb5a4f3498d4b8e5172de3494b2fb8d1ad28a75f0e0b0bf4452d23d17b054162a81592f8d340b5231d5736c230248
|
data/lib/face_cropper.rb
CHANGED
@@ -1,35 +1,32 @@
|
|
1
1
|
require "face_cropper/version"
|
2
|
+
require "face_cropper/aws_rekognition_face_detector"
|
3
|
+
require "face_cropper/face_box"
|
2
4
|
require 'aws-sdk'
|
3
|
-
require 'mini_magick'
|
4
|
-
require 'yaml'
|
5
5
|
|
6
6
|
class FaceCropper
|
7
7
|
def initialize(params)
|
8
8
|
@from_bucket = params[:from_bucket]
|
9
9
|
@to_bucket = params[:to_bucket]
|
10
10
|
@image_key = params[:image_key]
|
11
|
+
@face_boxis = params[:face_details]
|
11
12
|
end
|
12
13
|
|
13
14
|
def crop_and_upload!
|
14
|
-
faces = detect_faces!
|
15
|
-
|
15
|
+
faces = @face_boxis || detect_faces!
|
16
|
+
|
16
17
|
tmp_original_image_path = download_original_image!
|
17
|
-
|
18
|
+
crop_faces!(faces, tmp_original_image_path)
|
18
19
|
end
|
19
20
|
|
20
21
|
private
|
21
22
|
|
23
|
+
def debug_print(faces)
|
24
|
+
pp faces if ENV['API_DEBUG']
|
25
|
+
end
|
26
|
+
|
22
27
|
def detect_faces!
|
23
|
-
|
24
|
-
|
25
|
-
rekognition.detect_faces(
|
26
|
-
image: {
|
27
|
-
s3_object: {
|
28
|
-
bucket: @from_bucket,
|
29
|
-
name: @image_key
|
30
|
-
}
|
31
|
-
}
|
32
|
-
)
|
28
|
+
detector = AwsRekognitionFaceDetector.new(bucket: @from_bucket, image_key: @image_key)
|
29
|
+
detector.dcetect!.tap {|r| debug_print(r) }
|
33
30
|
end
|
34
31
|
|
35
32
|
def download_original_image!
|
@@ -39,20 +36,20 @@ class FaceCropper
|
|
39
36
|
end
|
40
37
|
end
|
41
38
|
|
42
|
-
def
|
39
|
+
def crop_faces!(faces, image_path)
|
43
40
|
faces.face_details.each_with_index do |detail, index|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
41
|
+
face_box = FaceBox.new(
|
42
|
+
width: detail.bounding_box.width,
|
43
|
+
height: detail.bounding_box.height,
|
44
|
+
top: detail.bounding_box.top,
|
45
|
+
left: detail.bounding_box.left
|
46
|
+
)
|
47
|
+
crop_file = face_box.crop_face!(image_path)
|
48
|
+
|
49
|
+
if @to_bucket
|
50
|
+
s3_client.put_object(bucket: @to_bucket, key: crop_file, body: File.read(crop_file))
|
51
|
+
File.unlink crop_file
|
52
|
+
end
|
56
53
|
end
|
57
54
|
end
|
58
55
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
class FaceCropper
|
5
|
+
class AwsRekognitionFaceDetector
|
6
|
+
def initialize(bucket:, image_key:)
|
7
|
+
@bucket = bucket
|
8
|
+
@imaget_key = image_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def dcetect!
|
12
|
+
rekognition = Aws::Rekognition::Client.new(region: 'us-east-1')
|
13
|
+
|
14
|
+
rekognition.detect_faces(
|
15
|
+
image: {
|
16
|
+
s3_object: {
|
17
|
+
bucket: @from_bucket,
|
18
|
+
name: @image_key
|
19
|
+
}
|
20
|
+
}
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'mini_magick'
|
2
|
+
|
3
|
+
class FaceCropper
|
4
|
+
class FaceBox
|
5
|
+
attr_reader :top, :left, :height, :width
|
6
|
+
|
7
|
+
def initialize(top: , left: , height: , width:)
|
8
|
+
@top = top
|
9
|
+
@left = left
|
10
|
+
@height = height
|
11
|
+
@width = width
|
12
|
+
end
|
13
|
+
|
14
|
+
def crop_face!(image_path)
|
15
|
+
image = MiniMagick::Image.open(image_path)
|
16
|
+
position = calculate_position(image_width: image.width, image_height: image.height)
|
17
|
+
|
18
|
+
crop_params = "#{position[:width]}x#{position[:height]}+#{position[:y]}+#{position[:x]}"
|
19
|
+
|
20
|
+
image.crop(crop_params)
|
21
|
+
crop_file = "#{crop_params}_#{@image_key}"
|
22
|
+
image.write(crop_file)
|
23
|
+
|
24
|
+
crop_file
|
25
|
+
end
|
26
|
+
|
27
|
+
def calculate_position(image_width: , image_height:)
|
28
|
+
{
|
29
|
+
width: (@width * image_width).to_i,
|
30
|
+
height: (@height * image.height).to_i,
|
31
|
+
x: (@top * image.height).to_i,
|
32
|
+
y: (@left * image.width).to_i
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/face_cropper/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: face_cropper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- takkanm
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|
@@ -112,6 +112,8 @@ files:
|
|
112
112
|
- bin/setup
|
113
113
|
- face_cropper.gemspec
|
114
114
|
- lib/face_cropper.rb
|
115
|
+
- lib/face_cropper/aws_rekognition_face_detector.rb
|
116
|
+
- lib/face_cropper/face_box.rb
|
115
117
|
- lib/face_cropper/version.rb
|
116
118
|
homepage: https://github.com/takkanm/face_cropper
|
117
119
|
licenses:
|