Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import numpy as np
|
2 |
import cv2
|
3 |
import gradio as gr
|
4 |
-
import asyncio
|
5 |
-
import time
|
6 |
|
7 |
PCA_MODEL_PATH = "pca_texture_model.npy"
|
8 |
COMPONENT_NAMES_PATH = "component_names.txt"
|
@@ -22,17 +20,13 @@ slider_ranges = [3 * np.sqrt(var) for var in explained_variance]
|
|
22 |
# Load component names if available
|
23 |
try:
|
24 |
with open(COMPONENT_NAMES_PATH, "r") as f:
|
25 |
-
component_names = [
|
26 |
-
f"{line.strip()} (Component {i + 1})" if line.strip() else f"Component {i + 1}"
|
27 |
-
for i, line in enumerate(f.readlines())
|
28 |
-
]
|
29 |
if len(component_names) < n_components:
|
30 |
-
component_names += [f"Component {i
|
31 |
except FileNotFoundError:
|
32 |
-
component_names = [f"Component {i
|
33 |
|
34 |
-
|
35 |
-
def generate_texture(component_values):
|
36 |
component_values = np.array(component_values)
|
37 |
new_texture = mean_texture + np.dot(component_values, components)
|
38 |
new_texture = np.clip(new_texture, 0, 255).astype(np.uint8)
|
@@ -40,37 +34,24 @@ def generate_texture(component_values):
|
|
40 |
new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB)
|
41 |
return new_texture
|
42 |
|
43 |
-
|
44 |
def randomize_texture():
|
45 |
sampled_coefficients = np.random.normal(0, np.sqrt(explained_variance), size=n_components)
|
46 |
return sampled_coefficients.tolist()
|
47 |
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
current_time = time.time()
|
51 |
-
state["last_update"] = current_time
|
52 |
-
await asyncio.sleep(0.2) # Debounce delay in seconds
|
53 |
-
if state["last_update"] == current_time:
|
54 |
-
texture = generate_texture(component_values)
|
55 |
-
return texture
|
56 |
-
else:
|
57 |
-
return gr.update()
|
58 |
-
|
59 |
-
|
60 |
-
async def on_random_click(state):
|
61 |
random_values = randomize_texture()
|
62 |
-
|
63 |
-
texture = generate_texture(random_values)
|
64 |
# Prepare updates for sliders and the image
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
|
69 |
# Create Gradio interface
|
70 |
with gr.Blocks() as demo:
|
71 |
-
# Initialize a state to keep track of the last update time
|
72 |
-
state = gr.State({"last_update": 0})
|
73 |
-
|
74 |
with gr.Row():
|
75 |
with gr.Column():
|
76 |
sliders = []
|
@@ -81,27 +62,27 @@ with gr.Blocks() as demo:
|
|
81 |
maximum=range_limit,
|
82 |
step=10,
|
83 |
value=0,
|
84 |
-
label=
|
85 |
)
|
86 |
sliders.append(slider)
|
87 |
random_button = gr.Button("Randomize Texture")
|
88 |
with gr.Column():
|
89 |
output_image = gr.Image(
|
90 |
-
label="Generated Texture"
|
91 |
)
|
92 |
-
|
93 |
-
# Update texture when any slider changes
|
94 |
for slider in sliders:
|
95 |
slider.change(
|
96 |
-
fn=
|
97 |
-
inputs=
|
98 |
-
outputs=output_image
|
99 |
)
|
100 |
|
101 |
-
# Randomize texture and update sliders and image
|
102 |
random_button.click(
|
103 |
fn=on_random_click,
|
104 |
-
inputs=
|
105 |
outputs=[*sliders, output_image]
|
106 |
)
|
107 |
|
|
|
1 |
import numpy as np
|
2 |
import cv2
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
PCA_MODEL_PATH = "pca_texture_model.npy"
|
6 |
COMPONENT_NAMES_PATH = "component_names.txt"
|
|
|
20 |
# Load component names if available
|
21 |
try:
|
22 |
with open(COMPONENT_NAMES_PATH, "r") as f:
|
23 |
+
component_names = [f"Component {i+1} ({line.strip()})" if line.strip() else f"Component {i+1}" for i, line in enumerate(f.readlines())]
|
|
|
|
|
|
|
24 |
if len(component_names) < n_components:
|
25 |
+
component_names += [f"Component {i+1}" for i in range(len(component_names), n_components)]
|
26 |
except FileNotFoundError:
|
27 |
+
component_names = [f"Component {i+1}" for i in range(n_components)]
|
28 |
|
29 |
+
def generate_texture(*component_values):
|
|
|
30 |
component_values = np.array(component_values)
|
31 |
new_texture = mean_texture + np.dot(component_values, components)
|
32 |
new_texture = np.clip(new_texture, 0, 255).astype(np.uint8)
|
|
|
34 |
new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB)
|
35 |
return new_texture
|
36 |
|
|
|
37 |
def randomize_texture():
|
38 |
sampled_coefficients = np.random.normal(0, np.sqrt(explained_variance), size=n_components)
|
39 |
return sampled_coefficients.tolist()
|
40 |
|
41 |
+
def update_texture(*component_values):
|
42 |
+
texture = generate_texture(*component_values)
|
43 |
+
return texture
|
44 |
|
45 |
+
def on_random_click():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
random_values = randomize_texture()
|
47 |
+
texture = generate_texture(*random_values)
|
|
|
48 |
# Prepare updates for sliders and the image
|
49 |
+
updates = [gr.update(value=value) for value in random_values]
|
50 |
+
updates.append(texture)
|
51 |
+
return updates
|
52 |
|
53 |
# Create Gradio interface
|
54 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
55 |
with gr.Row():
|
56 |
with gr.Column():
|
57 |
sliders = []
|
|
|
62 |
maximum=range_limit,
|
63 |
step=10,
|
64 |
value=0,
|
65 |
+
label=component_names[i]
|
66 |
)
|
67 |
sliders.append(slider)
|
68 |
random_button = gr.Button("Randomize Texture")
|
69 |
with gr.Column():
|
70 |
output_image = gr.Image(
|
71 |
+
label="Generated Texture"
|
72 |
)
|
73 |
+
|
74 |
+
# Update texture when any slider changes
|
75 |
for slider in sliders:
|
76 |
slider.change(
|
77 |
+
fn=update_texture,
|
78 |
+
inputs=sliders,
|
79 |
+
outputs=output_image
|
80 |
)
|
81 |
|
82 |
+
# Randomize texture and update sliders and image
|
83 |
random_button.click(
|
84 |
fn=on_random_click,
|
85 |
+
inputs=None,
|
86 |
outputs=[*sliders, output_image]
|
87 |
)
|
88 |
|