<commit_before>require "face_cropper/version"
require 'aws-sdk'
require 'mini_magick'

class FaceCropper
  def initialize(params)
    @from_bucket = params[:from_bucket]
    @to_bucket   = params[:to_bucket]
    @image_key   = params[:image_key]
  end

  def crop_and_upload!
    faces = detect_faces!

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

  private

  def detect_faces!
    rekognition = Aws::Rekognition::Client.new(region: 'us-east-1')

    rekognition.detect_faces(
      image: {
        s3_object: {
          bucket: @from_bucket,
          name:   @image_key
        }
      }
    )
  end

  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

  def upload_faces!(faces, image_path)
    faces.face_details.each_with_index do |detail, index|
      image = MiniMagick::Image.open(image_path)

      w = detail.bounding_box.width  * image.width
      h = detail.bounding_box.height * image.height
      x = detail.bounding_box.top    * image.height
      y = detail.bounding_box.left   * image.width
      crop_params = "#{w.to_i}x#{h.to_i}+#{y.to_i}+#{x.to_i}"

      image.crop(crop_params)
      crop_file = "#{index}_#{@image_key}"
      image.write(crop_file)
      s3_client.put_object(bucket: @to_bucket, key: crop_file, body: File.read(crop_file))
    end
  end

  def s3_client
    @s3_client ||= Aws::S3::Client.new(region: 'us-east-1')
  end
end
<commit_msg>extract AwsRekognition Class<commit_after>require "face_cropper/version"
require 'aws-sdk'
require 'mini_magick'

class FaceCropper
  class AwsRekognitionFaceDetector
    def initialize(bucket:, image_key:)
      @bucket     = bucket
      @imaget_key = image_key
    end

    def dcetect!
      rekognition = Aws::Rekognition::Client.new(region: 'us-east-1')

      rekognition.detect_faces(
        image: {
          s3_object: {
            bucket: @from_bucket,
            name:   @image_key
          }
        }
      )
    end
  end

  def initialize(params)
    @from_bucket = params[:from_bucket]
    @to_bucket   = params[:to_bucket]
    @image_key   = params[:image_key]
  end

  def crop_and_upload!
    faces = detect_faces!

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

  private

  def detect_faces!
    detector = AwsRekognitionFaceDetector.new(bucket: @from_bucket, image_key: @image_key)
    detector.dcetect!
  end

  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

  def upload_faces!(faces, image_path)
    faces.face_details.each_with_index do |detail, index|
      image = MiniMagick::Image.open(image_path)

      w = detail.bounding_box.width  * image.width
      h = detail.bounding_box.height * image.height
      x = detail.bounding_box.top    * image.height
      y = detail.bounding_box.left   * image.width
      crop_params = "#{w.to_i}x#{h.to_i}+#{y.to_i}+#{x.to_i}"

      image.crop(crop_params)
      crop_file = "#{index}_#{@image_key}"
      image.write(crop_file)
      s3_client.put_object(bucket: @to_bucket, key: crop_file, body: File.read(crop_file))
    end
  end

  def s3_client
    @s3_client ||= Aws::S3::Client.new(region: 'us-east-1')
  end
end

{'fraction_non_alphanumeric': 0.11161090806859714, 'fraction_numerical': 0.004498172617374192, 'mean_word_length': 3.1662763466042154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1414429', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1272, 'n_words': 214}