AlirezaF138 commited on
Commit
c1bccc1
1 Parent(s): 8741690

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -50
app.py CHANGED
@@ -1,72 +1,80 @@
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()
 
 
1
  import numpy as np
2
  import cv2
3
  import gradio as gr
 
4
 
 
5
  PCA_MODEL_PATH = "pca_texture_model.npy"
6
 
7
  # Load PCA model
8
  pca = np.load(PCA_MODEL_PATH, allow_pickle=True).item()
9
+ mean_texture = pca['mean_']
10
+ components = pca['components_']
11
+ explained_variance = pca['explained_variance_']
12
 
 
 
 
13
  n_components = components.shape[0]
14
+ TEXTURE_SIZE = int(np.sqrt(mean_texture.shape[0] // 3))
15
 
16
  # Calculate slider ranges
17
+ slider_ranges = [3 * np.sqrt(var) for var in explained_variance]
18
 
19
+ def generate_texture(*component_values):
20
+ component_values = np.array(component_values)
21
  new_texture = mean_texture + np.dot(component_values, components)
22
  new_texture = np.clip(new_texture, 0, 255).astype(np.uint8)
23
  new_texture = new_texture.reshape((TEXTURE_SIZE, TEXTURE_SIZE, 3))
24
  new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB)
25
+ return new_texture
 
26
 
27
+ def randomize_texture():
28
+ sampled_coefficients = np.random.normal(0, np.sqrt(explained_variance), size=n_components)
29
+ return sampled_coefficients.tolist()
30
+
31
+ def update_texture(*component_values):
32
+ texture = generate_texture(*component_values)
33
+ return texture
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ def on_random_click():
36
+ random_values = randomize_texture()
37
+ texture = generate_texture(*random_values)
38
+ # Prepare updates for sliders and the image
39
+ updates = [gr.update(value=value) for value in random_values]
40
+ updates.append(texture)
41
+ return updates
 
 
 
42
 
43
+ # Create Gradio interface
44
+ with gr.Blocks() as demo:
45
+ with gr.Row():
46
+ with gr.Column():
47
+ sliders = []
48
+ for i in range(n_components):
49
+ range_limit = slider_ranges[i]
50
+ slider = gr.Slider(
51
+ minimum=-range_limit,
52
+ maximum=range_limit,
53
+ step=10,
54
+ value=0,
55
+ label=f"Component {i+1}"
56
+ )
57
+ sliders.append(slider)
58
+ random_button = gr.Button("Randomize Texture")
59
+ with gr.Column():
60
+ output_image = gr.Image(
61
+ shape=(TEXTURE_SIZE, TEXTURE_SIZE),
62
+ label="Generated Texture"
63
+ )
64
 
65
+ # Update texture when any slider changes
66
+ for slider in sliders:
67
+ slider.change(
68
+ fn=update_texture,
69
+ inputs=sliders,
70
+ outputs=output_image
71
+ )
72
+
73
+ # Randomize texture and update sliders and image
74
+ random_button.click(
75
+ fn=on_random_click,
76
+ inputs=None,
77
+ outputs=[*sliders, output_image]
78
+ )
79
 
80
+ demo.launch()