AlirezaF138 commited on
Commit
6f5c70c
·
verified ·
1 Parent(s): 2a7b267

Update app.py

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