philomena/lib/philomena_web/image_reverse.ex

49 lines
1.2 KiB
Elixir
Raw Normal View History

2019-12-04 01:50:23 +01:00
defmodule PhilomenaWeb.ImageReverse do
2019-12-07 06:49:20 +01:00
alias Philomena.Analyzers
2019-12-04 01:50:23 +01:00
alias Philomena.Processors
alias Philomena.DuplicateReports
alias Philomena.Repo
import Ecto.Query
def images(image_params) do
image_params
|> Map.get("image")
|> analyze()
|> intensities()
|> case do
:error ->
[]
{analysis, intensities} ->
{width, height} = analysis.dimensions
aspect = width / height
dist = normalize_dist(image_params)
DuplicateReports.duplicates_of(intensities, aspect, dist, dist)
|> preload([:tags, :user, :intensity])
2019-12-04 01:50:23 +01:00
|> Repo.all()
end
end
2019-12-07 06:49:20 +01:00
defp analyze(%Plug.Upload{path: path}) do
{:ok, analysis} = Analyzers.analyze(path)
{analysis, path}
2019-12-04 01:50:23 +01:00
end
defp analyze(_upload), do: :error
2019-12-04 01:50:23 +01:00
defp intensities(:error), do: :error
2019-12-07 06:49:20 +01:00
defp intensities({analysis, path}) do
{analysis, Processors.intensities(analysis, path)}
2019-12-04 01:50:23 +01:00
end
# The distance metric is taxicab distance, not Euclidean,
# because this is more efficient to index.
defp normalize_dist(%{"distance" => distance}) do
distance
|> String.to_float()
|> max(0.01)
|> min(1.0)
end
defp normalize_dist(_dist), do: 0.25
end