File size: 3,018 Bytes
c225a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Filename: ComfyUI_INSTARAW/nodes/utility_nodes/spectral_engine_node.py
# ---

import torch
import numpy as np
import os

from ...modules.detection_bypass.utils import direct_spectral_match

IPHONE_STATS = None

class INSTARAW_SpectralEngine:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "image": ("IMAGE",),
                "strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
                "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), # Seed is no longer used but good to keep
            },
        }

    RETURN_TYPES = ("IMAGE",)
    FUNCTION = "execute"
    CATEGORY = "INSTARAW/Post-Processing"

    def load_stats(self):
        global IPHONE_STATS
        if IPHONE_STATS is None:
            node_file_path = os.path.dirname(os.path.realpath(__file__))
            instaraw_root_path = os.path.abspath(os.path.join(node_file_path, "..", ".."))
            stats_path = os.path.join(instaraw_root_path, "pretrained", "iphone_stats.npz")
            if not os.path.exists(stats_path):
                raise FileNotFoundError(f"iPhone stats file not found! Expected at: {stats_path}")
            print("🧠 INSTARAW Spectral Engine: Loading iPhone stats profile...")
            IPHONE_STATS = np.load(stats_path)
            print("✅ INSTARAW Spectral Engine: Stats loaded.")
        return IPHONE_STATS

    def tensor_to_numpy(self, tensor: torch.Tensor) -> np.ndarray:
        return (tensor.squeeze(0).cpu().numpy() * 255).astype(np.uint8)

    def numpy_to_tensor(self, np_array: np.ndarray) -> torch.Tensor:
        return torch.from_numpy(np_array.astype(np.float32) / 255.0).unsqueeze(0)

    def execute(self, image: torch.Tensor, strength: float, seed: int):
        print(f"🚀 INSTARAW Spectral Engine (Direct Match v2): Starting.")
        stats = self.load_stats()
        
        # Create the dictionary of target spectra for each channel
        target_spectra = {
            'r': stats['spectra_r'].mean(axis=0),
            'g': stats['spectra_g'].mean(axis=0),
            'b': stats['spectra_b'].mean(axis=0),
        }

        processed_images = []
        for i in range(image.shape[0]):
            numpy_image = self.tensor_to_numpy(image[i:i+1])
            processed_numpy_image = direct_spectral_match(
                img_arr=numpy_image,
                target_spectra=target_spectra,
                strength=strength
            )
            processed_tensor = self.numpy_to_tensor(processed_numpy_image)
            processed_images.append(processed_tensor)

        final_batch = torch.cat(processed_images, dim=0)
        print("✅ INSTARAW Spectral Engine: Processing complete.")
        return (final_batch,)

NODE_CLASS_MAPPINGS = { "INSTARAW_SpectralEngine": INSTARAW_SpectralEngine }
NODE_DISPLAY_NAME_MAPPINGS = { "INSTARAW_SpectralEngine": "🛡️ INSTARAW Spectral Engine" }