File size: 1,678 Bytes
23d337e
 
 
892fa81
 
23d337e
 
 
 
 
 
 
892fa81
23d337e
892fa81
23d337e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892fa81
 
23d337e
 
892fa81
 
 
23d337e
 
892fa81
 
 
 
23d337e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Image properties provider.

Delegates color-profile guessing and dominant-color extraction to
cores.vision.color — no duplicated k-means logic.
"""

from __future__ import annotations

import numpy as np

from config.settings import Settings, settings as _default_settings
from cores.vision import guess_color_profile, dominant_colors
from pipeline.feature_extraction import PipelineOutput
from providers.base import BaseProvider, ProviderCapability


class ImagePropertiesProvider(BaseProvider):
    name = "image_properties"
    capability = ProviderCapability.IMAGE_ANALYSIS

    def __init__(self, settings: Settings | None = None) -> None:
        super().__init__(settings=settings or _default_settings)

    def is_available(self) -> bool:
        return True

    def _run(self, pipeline_output: PipelineOutput) -> tuple[dict, dict]:
        img: np.ndarray = pipeline_output.image
        h, w = img.shape[:2]
        channels = img.shape[2] if img.ndim == 3 else 1
        aspect = round(w / h, 4) if h > 0 else 0
        megapixels = round((w * h) / 1_000_000, 4)
        profile = guess_color_profile(img)
        colors = dominant_colors(img, k=5)

        raw = {
            "width": w, "height": h, "channels": channels,
            "aspect_ratio": aspect, "megapixels": megapixels,
            "color_profile": profile, "dominant_colors": colors,
        }
        normalized = {
            "quality_score": None,
            "width": w, "height": h, "channels": channels,
            "color_profile": profile, "dominant_colors": colors,
            "aspects": {"aspect_ratio": aspect, "megapixels": megapixels},
        }
        return raw, normalized