AlirezaF138 commited on
Commit
56e1cfe
1 Parent(s): 3b2ba80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -45,34 +45,29 @@ def update_texture(*component_values):
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
  def process_uploaded_image(uploaded_image):
54
- # Resize the image to match the texture size
55
  resized_image = cv2.resize(uploaded_image, (TEXTURE_SIZE, TEXTURE_SIZE))
56
  resized_image = cv2.cvtColor(resized_image, cv2.COLOR_RGB2BGR)
57
  flattened_image = resized_image.flatten()
58
 
59
- # Project the image into the PCA component space
60
  centered_image = flattened_image - mean_texture
61
  coefficients = np.dot(centered_image, components.T)
62
-
63
- # Clip the coefficients within the allowed slider ranges
64
  clipped_coefficients = [np.clip(coeff, -slider_ranges[i], slider_ranges[i]) for i, coeff in enumerate(coefficients)]
65
  return clipped_coefficients
66
 
67
  def on_image_upload(image):
68
- # Process the uploaded image and get PCA coefficients
69
  coefficients = process_uploaded_image(image)
70
- texture = generate_texture(*coefficients)
71
- # Update sliders and image
72
  updates = [gr.update(value=value) for value in coefficients]
73
- updates.append(texture)
74
  return updates
75
 
 
 
 
 
76
  # Create Gradio interface
77
  with gr.Blocks() as demo:
78
  with gr.Row():
@@ -88,24 +83,27 @@ with gr.Blocks() as demo:
88
  label=component_names[i]
89
  )
90
  sliders.append(slider)
 
 
 
 
 
91
  with gr.Column():
92
  output_image = gr.Image(
93
  label="Generated Texture"
94
  )
95
- random_button = gr.Button("Randomize Texture")
96
  upload_image = gr.Image(
97
  label="Upload Image",
98
  sources=['upload', 'clipboard'],
99
  type="numpy"
100
  )
101
 
102
- # Update texture when any slider changes
103
- for slider in sliders:
104
- slider.change(
105
- fn=update_texture,
106
- inputs=sliders,
107
- outputs=output_image
108
- )
109
 
110
  # Randomize texture and update sliders and image
111
  random_button.click(
@@ -114,11 +112,18 @@ with gr.Blocks() as demo:
114
  outputs=[*sliders, output_image]
115
  )
116
 
117
- # Update sliders and image based on uploaded image
118
- upload_image.change(
119
  fn=on_image_upload,
120
  inputs=upload_image,
121
- outputs=[*sliders, output_image]
 
 
 
 
 
 
 
122
  )
123
 
124
- demo.launch()
 
45
  def on_random_click():
46
  random_values = randomize_texture()
47
  texture = generate_texture(*random_values)
 
48
  updates = [gr.update(value=value) for value in random_values]
49
  updates.append(texture)
50
  return updates
51
 
52
  def process_uploaded_image(uploaded_image):
 
53
  resized_image = cv2.resize(uploaded_image, (TEXTURE_SIZE, TEXTURE_SIZE))
54
  resized_image = cv2.cvtColor(resized_image, cv2.COLOR_RGB2BGR)
55
  flattened_image = resized_image.flatten()
56
 
 
57
  centered_image = flattened_image - mean_texture
58
  coefficients = np.dot(centered_image, components.T)
 
 
59
  clipped_coefficients = [np.clip(coeff, -slider_ranges[i], slider_ranges[i]) for i, coeff in enumerate(coefficients)]
60
  return clipped_coefficients
61
 
62
  def on_image_upload(image):
 
63
  coefficients = process_uploaded_image(image)
 
 
64
  updates = [gr.update(value=value) for value in coefficients]
 
65
  return updates
66
 
67
+ def on_update_click(*component_values):
68
+ texture = generate_texture(*component_values)
69
+ return texture
70
+
71
  # Create Gradio interface
72
  with gr.Blocks() as demo:
73
  with gr.Row():
 
83
  label=component_names[i]
84
  )
85
  sliders.append(slider)
86
+
87
+ update_texture_button = gr.Button("Update Texture")
88
+ random_button = gr.Button("Randomize Texture")
89
+ get_components_button = gr.Button("Get Components from Image")
90
+
91
  with gr.Column():
92
  output_image = gr.Image(
93
  label="Generated Texture"
94
  )
 
95
  upload_image = gr.Image(
96
  label="Upload Image",
97
  sources=['upload', 'clipboard'],
98
  type="numpy"
99
  )
100
 
101
+ # Update texture when clicking the "Update Texture" button
102
+ update_texture_button.click(
103
+ fn=on_update_click,
104
+ inputs=sliders,
105
+ outputs=output_image
106
+ )
 
107
 
108
  # Randomize texture and update sliders and image
109
  random_button.click(
 
112
  outputs=[*sliders, output_image]
113
  )
114
 
115
+ # Update sliders based on the uploaded image when clicking "Get Components from Image"
116
+ get_components_button.click(
117
  fn=on_image_upload,
118
  inputs=upload_image,
119
+ outputs=sliders
120
+ )
121
+
122
+ # Keep the uploaded image for reference (no update on texture yet)
123
+ upload_image.change(
124
+ fn=None,
125
+ inputs=None,
126
+ outputs=[]
127
  )
128
 
129
+ demo.launch()