BETA: accurate color extractor

This commit is contained in:
Claudio Ortolina
2026-03-03 15:41:24 +00:00
parent d838c29207
commit 1d3361c425
8 changed files with 51 additions and 2 deletions
@@ -0,0 +1,17 @@
defmodule MusicLibrary.Colors.KMeansExtractor do
@moduledoc """
Extracts dominant colors from images using K-Means clustering via the dominant_colors library.
"""
alias Vix.Vips.Image
def extract_dominant_colors(image_data, num_colors \\ 5) do
with {:ok, dir} <- Briefly.create(type: :directory),
path = dir <> "/temp_image.jpg",
{:ok, image} <- Image.new_from_buffer(image_data),
:ok <- Image.write_to_file(image, path),
{:ok, colors} <- DominantColors.dominant_colors(path) do
{:ok, Enum.take(colors, num_colors)}
end
end
end
+1 -1
View File
@@ -375,7 +375,7 @@ defmodule MusicLibrary.Records do
def create_record(attrs \\ %{}) do
with {:ok, record} <- do_create_record(attrs) do
extract_colors_async(record, :fast)
extract_colors_async(record, :accurate)
generate_embedding_async(record)
record
+4 -1
View File
@@ -2,7 +2,7 @@ defmodule MusicLibrary.Worker.ExtractColors do
use Oban.Worker, queue: :heavy_writes, max_attempts: 3
alias MusicLibrary.{Assets, Records}
alias MusicLibrary.Colors.{ColorFrequencyExtractor, EdgeWeightedExtractor}
alias MusicLibrary.Colors.{ColorFrequencyExtractor, EdgeWeightedExtractor, KMeansExtractor}
@impl Oban.Worker
def perform(%Oban.Job{args: %{"id" => record_id, "method" => method}}) do
@@ -21,4 +21,7 @@ defmodule MusicLibrary.Worker.ExtractColors do
defp extract_colors(image_data, :slow),
do: EdgeWeightedExtractor.extract_dominant_colors(image_data)
defp extract_colors(image_data, :accurate),
do: KMeansExtractor.extract_dominant_colors(image_data)
end
@@ -195,6 +195,21 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
{gettext("Extract colors (slow)")}
</.dropdown_link>
<.dropdown_link
id={"actions-#{@record.id}-extract-colors-accurate"}
phx-click={
JS.push("extract_colors", value: %{id: @record.id, method: :accurate})
}
>
<.icon
name="hero-paint-brush"
class="h-4 w-4 mr-1 phx-click-loading:animate-shake"
aria-hidden="true"
data-slot="icon"
/>
{gettext("Extract colors (accurate)")}
</.dropdown_link>
<.dropdown_separator />
<.dropdown_link
id={"actions-#{@record.id}-delete"}