Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,72 @@
|
|
|
|
1 |
import numpy as np
|
2 |
import cv2
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
PCA_MODEL_PATH = "pca_texture_model.npy"
|
|
|
|
|
8 |
pca = np.load(PCA_MODEL_PATH, allow_pickle=True).item()
|
|
|
9 |
|
10 |
# PCA attributes
|
11 |
mean_texture = pca.mean_
|
12 |
components = pca.components_
|
13 |
n_components = components.shape[0]
|
14 |
-
TEXTURE_SIZE = np.sqrt(mean_texture.shape[0] // 3).astype(int)
|
15 |
|
16 |
-
#
|
17 |
slider_ranges = [3 * np.sqrt(var) for var in pca.explained_variance_]
|
18 |
|
19 |
-
# Function to reconstruct texture from PCA components
|
20 |
def reconstruct_texture(component_values):
|
21 |
-
#
|
22 |
new_texture = mean_texture + np.dot(component_values, components)
|
23 |
-
new_texture = np.clip(new_texture, 0, 255).astype(np.uint8)
|
24 |
-
new_texture = new_texture.reshape((TEXTURE_SIZE, TEXTURE_SIZE, 3))
|
25 |
-
new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB)
|
26 |
-
|
27 |
-
|
28 |
-
# Function to handle slider values and generate the texture
|
29 |
-
def generate_texture(*component_values):
|
30 |
-
component_values = np.array(component_values)
|
31 |
-
return reconstruct_texture(component_values)
|
32 |
-
|
33 |
-
# Function to generate random textures
|
34 |
-
def random_texture():
|
35 |
-
# Generate random PCA component values
|
36 |
-
component_values = np.random.normal(0, 1, n_components)
|
37 |
-
# Generate the texture from the random PCA values
|
38 |
-
generated_image = reconstruct_texture(component_values)
|
39 |
-
# Return the image and each component value separately
|
40 |
-
return [generated_image] + list(component_values)
|
41 |
-
|
42 |
-
# Gradio interface
|
43 |
-
def create_app():
|
44 |
-
sliders = [
|
45 |
-
gr.Slider(
|
46 |
-
minimum=-slider_ranges[i],
|
47 |
-
maximum=slider_ranges[i],
|
48 |
-
step=0.1,
|
49 |
-
label=f"Component {i + 1}",
|
50 |
-
value=0,
|
51 |
-
)
|
52 |
-
for i in range(n_components)
|
53 |
-
]
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
with gr.Row():
|
58 |
with gr.Column():
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
random_button = gr.Button("Random")
|
63 |
-
|
64 |
with gr.Column():
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
|
|
|
|
|
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
-
|
81 |
-
app = create_app()
|
82 |
-
app.launch()
|
|
|
1 |
+
import os
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
6 |
|
7 |
+
# Paths (Adjust these paths according to your environment)
|
8 |
PCA_MODEL_PATH = "pca_texture_model.npy"
|
9 |
+
|
10 |
+
# Load PCA model
|
11 |
pca = np.load(PCA_MODEL_PATH, allow_pickle=True).item()
|
12 |
+
TEXTURE_SIZE = int(np.sqrt(pca.mean_.shape[0] // 3))
|
13 |
|
14 |
# PCA attributes
|
15 |
mean_texture = pca.mean_
|
16 |
components = pca.components_
|
17 |
n_components = components.shape[0]
|
|
|
18 |
|
19 |
+
# Calculate slider ranges
|
20 |
slider_ranges = [3 * np.sqrt(var) for var in pca.explained_variance_]
|
21 |
|
|
|
22 |
def reconstruct_texture(component_values):
|
23 |
+
# Reconstruct the texture using the PCA components
|
24 |
new_texture = mean_texture + np.dot(component_values, components)
|
25 |
+
new_texture = np.clip(new_texture, 0, 255).astype(np.uint8)
|
26 |
+
new_texture = new_texture.reshape((TEXTURE_SIZE, TEXTURE_SIZE, 3))
|
27 |
+
new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB)
|
28 |
+
image = Image.fromarray(new_texture)
|
29 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
def main():
|
32 |
+
# Create a list of sliders
|
33 |
+
sliders = []
|
34 |
+
for i in range(n_components):
|
35 |
+
range_limit = slider_ranges[i]
|
36 |
+
sliders.append(gr.Slider(minimum=-range_limit, maximum=range_limit, step=10, label=f"Component {i+1}", value=0))
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
with gr.Row():
|
40 |
with gr.Column():
|
41 |
+
# Create sliders
|
42 |
+
for slider in sliders:
|
43 |
+
slider.render()
|
|
|
|
|
44 |
with gr.Column():
|
45 |
+
image_output = gr.Image(label="Generated Texture", type="pil")
|
46 |
+
image_output.render()
|
47 |
+
randomize_button = gr.Button("Randomize")
|
48 |
+
randomize_button.render()
|
49 |
+
|
50 |
+
# Update texture when sliders change
|
51 |
+
def on_slider_change(*args):
|
52 |
+
component_values = np.array([s.value for s in sliders])
|
53 |
+
image = reconstruct_texture(component_values)
|
54 |
+
image_output.update(value=image)
|
55 |
|
56 |
+
for slider in sliders:
|
57 |
+
slider.change(fn=on_slider_change, inputs=None, outputs=None)
|
58 |
+
|
59 |
+
# Randomize button
|
60 |
+
def on_randomize_click():
|
61 |
+
sampled_coefficients = np.random.normal(0, np.sqrt(pca.explained_variance_), size=n_components)
|
62 |
+
for i, slider in enumerate(sliders):
|
63 |
+
slider.update(value=sampled_coefficients[i])
|
64 |
+
image = reconstruct_texture(sampled_coefficients)
|
65 |
+
image_output.update(value=image)
|
66 |
|
67 |
+
randomize_button.click(fn=on_randomize_click, inputs=None, outputs=None)
|
68 |
+
|
69 |
+
demo.launch()
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
+
main()
|
|
|
|