File size: 1,254 Bytes
695fbf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Adaptive sampler helper node (moved to mod/).

Keeps class/key name AdaptiveSamplerHelper for backward compatibility.
"""

import numpy as np
from scipy.ndimage import laplace


class AdaptiveSamplerHelper:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "image": ("IMAGE", {}),
                "steps": ("INT", {"default": 20, "min": 1, "max": 200}),
                "cfg": ("FLOAT", {"default": 7.0, "min": 0.1, "max": 20.0, "step": 0.1}),
                "denoise": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
            }
        }

    RETURN_TYPES = ("INT", "FLOAT", "FLOAT")
    RETURN_NAMES = ("steps", "cfg", "denoise")
    FUNCTION = "tune"
    CATEGORY = "MagicNodes"

    def tune(self, image, steps, cfg, denoise):
        img = image[0].cpu().numpy()
        gray = img.mean(axis=2)
        brightness = float(gray.mean())
        contrast = float(gray.std())
        sharpness = float(np.var(laplace(gray)))

        tuned_steps = int(max(1, round(steps + sharpness * 10)))
        tuned_cfg = float(cfg + contrast * 2.0)
        tuned_denoise = float(np.clip(denoise + (0.5 - brightness), 0.0, 1.0))

        return (tuned_steps, tuned_cfg, tuned_denoise)