defmodule ItsfoundRekognition.ProcessImage do @moduledoc """ This module processes an AWS Rekognition request for the labels / descriptions of what is in an image you need to pass it an # # ItsfoundRecognition.ProcessImage.recognise_label("1d826fd7-38b1-4349-bdc9-f2990f590a11", "png", &ImageRecognition.LoggerImageResponseParser.parse/1) # # ItsfoundRecognition.ProcessImage.recognise_label("1d826fd7-38b1-4349-bdc9-f2990f590a11", "png", &ImageRecognition.ImageLabelParser.parse/1) # # ItsfoundRecognition.ProcessImage.recognise_label("1d826fd7-38b1-4349-bdc9-f2990f590a11", "png", &ImageRecognition.ImageLabelParser.parse_top/1) """ alias ExAws.Rekognition.S3Object alias ItsfoundRekognition.LoggerImageResponseParser def recognise_label( image_uuid, ending, process_func \\ &LoggerImageResponseParser.parse/1 ) do case ending do "jpg" -> process_image(image_uuid, process_func, "original.jpg") "jpeg" -> process_image(image_uuid, process_func, "original.jpeg") "png" -> process_image(image_uuid, process_func, "original.png") _ -> %{ error: "invalid file ending" } end end defp process_image(image_uuid, process_func, image_name) do bucket = ItsfoundRekognition.Utils.aws_bucket() object_id = "#{ItsfoundRekognition.Utils.aws_bucket_divider()}/items/#{image_uuid}/#{image_name}" process_image_from_s3(%S3Object{bucket: bucket, name: object_id, version: nil}, process_func) end defp process_image_from_s3( %S3Object{ bucket: _bucket, name: _name, version: _version } = image, process_func ) do case process_detect_labels(image) do {:ok, response} -> process_func.(response) {:error, errors} -> process_func.(errors) end end defp process_detect_labels(image) do opts = ItsfoundRekognition.Utils.aws_config_options() image |> ExAws.Rekognition.detect_labels() |> ExAws.request(opts) end end