mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-19 22:27:59 +01:00
Add intensity recalculation task
This commit is contained in:
parent
18d385283b
commit
3709a09d6b
2 changed files with 62 additions and 9 deletions
53
lib/mix/tasks/recalculate_intensities.ex
Normal file
53
lib/mix/tasks/recalculate_intensities.ex
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
defmodule Mix.Tasks.RecalculateIntensities do
|
||||||
|
use Mix.Task
|
||||||
|
|
||||||
|
alias Philomena.Images.{Image, Thumbnailer}
|
||||||
|
alias Philomena.ImageIntensities.ImageIntensity
|
||||||
|
alias Philomena.Batch
|
||||||
|
alias Philomena.Repo
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
@shortdoc "Recalculates all intensities for reverse search."
|
||||||
|
@requirements ["app.start"]
|
||||||
|
@impl Mix.Task
|
||||||
|
def run(_args) do
|
||||||
|
Batch.record_batches(Image, fn batch ->
|
||||||
|
batch
|
||||||
|
|> Stream.with_index()
|
||||||
|
|> Stream.each(fn {image, i} ->
|
||||||
|
image_file =
|
||||||
|
cond do
|
||||||
|
image.image_mime_type in ["image/png", "image/jpeg"] ->
|
||||||
|
Thumbnailer.image_file(image)
|
||||||
|
|
||||||
|
true ->
|
||||||
|
Path.join(Thumbnailer.image_thumb_dir(image), "rendered.png")
|
||||||
|
end
|
||||||
|
|
||||||
|
case System.cmd("image-intensities", [image_file]) do
|
||||||
|
{output, 0} ->
|
||||||
|
[nw, ne, sw, se] =
|
||||||
|
output
|
||||||
|
|> String.trim()
|
||||||
|
|> String.split("\t")
|
||||||
|
|> Enum.map(&String.to_float/1)
|
||||||
|
|
||||||
|
ImageIntensity
|
||||||
|
|> where(image_id: ^image.id)
|
||||||
|
|> Repo.update_all(set: [nw: nw, ne: ne, sw: sw, se: se])
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
:err
|
||||||
|
end
|
||||||
|
|
||||||
|
if rem(i, 100) == 0 do
|
||||||
|
IO.write("\r#{image.id}")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> Stream.run()
|
||||||
|
end)
|
||||||
|
|
||||||
|
IO.puts("\nDone")
|
||||||
|
end
|
||||||
|
end
|
|
@ -132,18 +132,18 @@ defmodule Philomena.Images.Thumbnailer do
|
||||||
|> File.mkdir_p!()
|
|> File.mkdir_p!()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp image_file(%Image{image: image}),
|
def image_file(%Image{image: image}),
|
||||||
do: Path.join(image_file_root(), image)
|
do: Path.join(image_file_root(), image)
|
||||||
|
|
||||||
defp image_thumb_dir(%Image{
|
def image_thumb_dir(%Image{
|
||||||
created_at: created_at,
|
created_at: created_at,
|
||||||
id: id,
|
id: id,
|
||||||
hidden_from_users: true,
|
hidden_from_users: true,
|
||||||
hidden_image_key: key
|
hidden_image_key: key
|
||||||
}),
|
}),
|
||||||
do: Path.join([image_thumbnail_root(), time_identifier(created_at), "#{id}-#{key}"])
|
do: Path.join([image_thumbnail_root(), time_identifier(created_at), "#{id}-#{key}"])
|
||||||
|
|
||||||
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
|
def image_thumb_dir(%Image{created_at: created_at, id: id}),
|
||||||
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])
|
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])
|
||||||
|
|
||||||
defp image_url_base(%Image{created_at: created_at, id: id}, nil),
|
defp image_url_base(%Image{created_at: created_at, id: id}, nil),
|
||||||
|
|
Loading…
Reference in a new issue