File size: 2,508 Bytes
ac73e0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
import numpy as np
import cv2
import gradio as gr
from PIL import Image

# Load the PCA model
PCA_MODEL_PATH = "pca_texture_model.npy"
pca = np.load(PCA_MODEL_PATH, allow_pickle=True).item()

# PCA attributes
mean_texture = pca.mean_
components = pca.components_
n_components = components.shape[0]
TEXTURE_SIZE = np.sqrt(mean_texture.shape[0] // 3).astype(int)

# Slider ranges based on PCA variance
slider_ranges = [3 * np.sqrt(var) for var in pca.explained_variance_]

# Function to reconstruct texture from PCA components
def reconstruct_texture(component_values):
    # Calculate the texture using PCA components
    new_texture = mean_texture + np.dot(component_values, components)
    new_texture = np.clip(new_texture, 0, 255).astype(np.uint8)  # Ensure valid pixel range
    new_texture = new_texture.reshape((TEXTURE_SIZE, TEXTURE_SIZE, 3))  # Reshape back to image
    new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB)  # Convert to RGB for display
    return Image.fromarray(new_texture)

# Function to handle slider values and generate the texture
def generate_texture(*component_values):
    component_values = np.array(component_values)
    return reconstruct_texture(component_values)

# Function to generate random textures
def random_texture():
    component_values = np.random.normal(0, 1, n_components)
    return reconstruct_texture(component_values), list(component_values)

# Gradio interface
def create_app():
    sliders = [
        gr.Slider(
            minimum=-slider_ranges[i],
            maximum=slider_ranges[i],
            step=0.1,
            label=f"Component {i + 1}",
            value=0,
        )
        for i in range(n_components)
    ]

    # Define interface elements
    with gr.Blocks() as app:
        with gr.Row():
            with gr.Column():
                gr.Markdown("### PCA Texture Generator")
                sliders_container = gr.Group(sliders)
                generate_button = gr.Button("Generate")
                random_button = gr.Button("Random")

            with gr.Column():
                preview = gr.Image(shape=(TEXTURE_SIZE, TEXTURE_SIZE), label="Preview")

        # Link sliders and preview
        generate_button.click(
            generate_texture, inputs=sliders, outputs=preview
        )

        random_button.click(
            random_texture, inputs=None, outputs=[preview] + sliders
        )

    return app


if __name__ == "__main__":
    # Create and launch the app
    app = create_app()
    app.launch()