class FaceCropper

Constants

VERSION

Public Class Methods

new(params) click to toggle source
# File lib/face_cropper.rb, line 7
def initialize(params)
  @from_bucket = params[:from_bucket]
  @to_bucket   = params[:to_bucket]
  @image_key   = params[:image_key]
  @face_boxis  = params[:face_details]
  @region      = params[:region] || 'us-east-1'
end

Public Instance Methods

crop!() click to toggle source
# File lib/face_cropper.rb, line 15
def crop!
  faces = @face_boxis || detect_faces!

  tmp_original_image_path = download_original_image!
  crop_faces!(faces, tmp_original_image_path)
end

Private Instance Methods

crop_faces!(faces, image_path) click to toggle source
# File lib/face_cropper.rb, line 40
def crop_faces!(faces, image_path)
  faces.face_details.each_with_index do |detail, index|
    face_box = FaceBox.new(
      width:  detail.bounding_box.width,
      height: detail.bounding_box.height,
      top:    detail.bounding_box.top,
      left:   detail.bounding_box.left
    )
    crop_file = face_box.crop_face!(image_path)

    if @to_bucket
      s3_client.put_object(bucket: @to_bucket, key: crop_file, body: File.read(crop_file))
      File.unlink crop_file
    end
  end
end
debug_print(faces) click to toggle source
# File lib/face_cropper.rb, line 24
def debug_print(faces)
  pp faces if ENV['API_DEBUG']
end
detect_faces!() click to toggle source
# File lib/face_cropper.rb, line 28
def detect_faces!
  detector = AwsRekognitionFaceDetector.new(bucket: @from_bucket, image_key: @image_key, region: @region)
  detector.dcetect!.tap {|r| debug_print(r) }
end
download_original_image!() click to toggle source
# File lib/face_cropper.rb, line 33
def download_original_image!
  image_body = s3_client.get_object(bucket: @from_bucket, key: @image_key).body.read
  File.basename(@image_key).tap do |image_path|
    File.write(image_path, image_body)
  end
end
s3_client() click to toggle source
# File lib/face_cropper.rb, line 57
def s3_client
  @s3_client ||= Aws::S3::Client.new(region: @region)
end