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)
|
2023-05-29 13:06:41 +02:00
|
|
|
|> preload([:user, :intensity, [:sources, tags: :aliases]])
|
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
|
2020-09-08 06:02:24 +02:00
|
|
|
case Analyzers.analyze(path) do
|
|
|
|
{:ok, analysis} -> {analysis, path}
|
|
|
|
_ -> :error
|
|
|
|
end
|
2019-12-04 01:50:23 +01:00
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-12 16:51:44 +01:00
|
|
|
defp analyze(_upload), do: :error
|
2019-12-04 01:50:23 +01:00
|
|
|
|
|
|
|
defp intensities(:error), do: :error
|
2020-01-11 05:20:19 +01:00
|
|
|
|
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
|
2024-06-03 04:48:24 +02:00
|
|
|
|> parse_dist()
|
2019-12-04 01:50:23 +01:00
|
|
|
|> max(0.01)
|
|
|
|
|> min(1.0)
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-04 01:50:23 +01:00
|
|
|
defp normalize_dist(_dist), do: 0.25
|
2024-06-03 04:48:24 +02:00
|
|
|
|
|
|
|
defp parse_dist(dist) do
|
|
|
|
case Decimal.parse(dist) do
|
|
|
|
{value, _rest} ->
|
|
|
|
Decimal.to_float(value)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
0.0
|
|
|
|
end
|
|
|
|
end
|
2019-12-30 15:18:38 +01:00
|
|
|
end
|