face_cropper 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53597329a198f8e0bed4fd9c64cba57ff000acde
4
- data.tar.gz: 0f4d4a56e77c6823dd755c2575ca3cdac164c396
3
+ metadata.gz: 702e3d2fbe93f586b47c7e3f2848f05729cc81b5
4
+ data.tar.gz: c5b462c7ca47f0d8b535a9cb16074f62de01cbb9
5
5
  SHA512:
6
- metadata.gz: cd10291b7478597d330694b2f0a769ab8d31ed44a74d8615fd3cda76731775946a5e451d0c93c9830594f2660fed53ea3d5c9f239d50e6e624711a85570230b4
7
- data.tar.gz: 0fda9a49cc182c72d4732c84d799190af447854bacfeb2e8ff6c16525fb9b707d8db928d5c1519c64ef0cd767c794f453071a7fd43bd8989efc9893e35245a09
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
- puts faces.to_yaml
15
+ faces = @face_boxis || detect_faces!
16
+
16
17
  tmp_original_image_path = download_original_image!
17
- upload_faces!(faces, tmp_original_image_path)
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
- rekognition = Aws::Rekognition::Client.new(region: 'us-east-1')
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 upload_faces!(faces, image_path)
39
+ def crop_faces!(faces, image_path)
43
40
  faces.face_details.each_with_index do |detail, index|
44
- image = MiniMagick::Image.open(image_path)
45
-
46
- w = detail.bounding_box.width * image.width
47
- h = detail.bounding_box.height * image.height
48
- x = detail.bounding_box.top * image.height
49
- y = detail.bounding_box.left * image.width
50
- crop_params = "#{w.to_i}x#{h.to_i}+#{y.to_i}+#{x.to_i}"
51
-
52
- image.crop(crop_params)
53
- crop_file = "#{index}_#{@image_key}"
54
- image.write(crop_file)
55
- s3_client.put_object(bucket: @to_bucket, key: crop_file, body: File.read(crop_file))
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
@@ -1,3 +1,3 @@
1
1
  class FaceCropper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-08 00:00:00.000000000 Z
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: