Sagar Bharadwaj commited on
Commit
b3d91b5
·
1 Parent(s): d4675c1

Added live updates for font size and thicness.

Browse files
gradio_server/app.py CHANGED
@@ -1,59 +1,15 @@
1
  import gradio as gr
2
 
3
- from colorbynumber.main import ColorByNumber
4
  from colorbynumber.config import default_config
 
5
  from . import doc
6
 
7
  MAX_NUM_COLORS = 50 # Mostly for UI purposes
8
 
9
- def get_color_by_number(image_path, number_of_colors,
10
- is_automatic_colors, num_colors,
11
- denoise_flag, denoise_order, denoise_type,
12
- blur_size, denoise_h,
13
- open_kernel_size, area_perc_threshold,
14
- check_shape_validity, arc_length_area_ratio_threshold,
15
- *color_list):
16
- # Convert each color to r,g,b tuple
17
- color_list = color_list[:num_colors]
18
- def _hex_to_rgb(hex_color):
19
- hex_color = hex_color.lstrip("#")
20
- return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
21
- color_list = [_hex_to_rgb(h) for h in color_list]
22
-
23
- # Update config
24
- config = default_config.copy()
25
- config["denoise"] = denoise_flag
26
- config["denoise_order"] = denoise_order
27
- config["denoise_type"] = denoise_type
28
- config["blur_size"] = blur_size
29
- config["denoise_h"] = denoise_h
30
- config["open_kernel_size"] = open_kernel_size
31
- config["area_perc_threshold"] = area_perc_threshold
32
- config["check_shape_validity"] = check_shape_validity
33
- config["arc_length_area_ratio_threshold"] = arc_length_area_ratio_threshold
34
-
35
- if is_automatic_colors:
36
- colorbynumber_obj = ColorByNumber(
37
- image_path = image_path,
38
- num_colors = number_of_colors,
39
- config = config,
40
- )
41
- else:
42
- colorbynumber_obj = ColorByNumber(
43
- image_path = image_path,
44
- color_list = color_list,
45
- config = config,
46
- )
47
-
48
- numbered_islands = colorbynumber_obj.create_color_by_number()
49
- return numbered_islands, \
50
- colorbynumber_obj.generate_color_legend(), \
51
- colorbynumber_obj.simplified_image
52
-
53
  with gr.Blocks(title = "Color by number") as demo:
54
  with gr.Row():
55
  # Inputs
56
- with gr.Column():
57
  image_path = gr.Image(type="filepath")
58
  image_examples = gr.Examples(
59
  examples=[
@@ -204,12 +160,35 @@ with gr.Blocks(title = "Color by number") as demo:
204
  # Outputs
205
  with gr.Column():
206
  color_by_number_image = gr.Image(label = "Color by number")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  legend_image = gr.Image(label = "Legend")
208
  simplified_image = gr.Image(label = "Simplified image")
 
 
209
 
210
  # Submit button callback
211
  submit_button.click(
212
- fn = get_color_by_number,
213
  inputs = [
214
  image_path,
215
  number_of_colors,
@@ -224,9 +203,26 @@ with gr.Blocks(title = "Color by number") as demo:
224
  area_perc_threshold,
225
  check_shape_validity,
226
  arc_length_area_ratio_threshold,
 
 
 
227
  *color_pickers
228
  ],
229
- outputs = [color_by_number_image, legend_image, simplified_image]
230
  )
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  demo.launch()
 
1
  import gradio as gr
2
 
 
3
  from colorbynumber.config import default_config
4
+ from . import callbacks
5
  from . import doc
6
 
7
  MAX_NUM_COLORS = 50 # Mostly for UI purposes
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  with gr.Blocks(title = "Color by number") as demo:
10
  with gr.Row():
11
  # Inputs
12
+ with gr.Column(elem_id="inputColumn"):
13
  image_path = gr.Image(type="filepath")
14
  image_examples = gr.Examples(
15
  examples=[
 
160
  # Outputs
161
  with gr.Column():
162
  color_by_number_image = gr.Image(label = "Color by number")
163
+
164
+ # Edit coloring page
165
+ with gr.Row():
166
+ font_size = gr.Slider(
167
+ label = "Font size",
168
+ minimum = 0.1,
169
+ maximum = 10,
170
+ value = default_config["font_size"],
171
+ )
172
+ font_thickness = gr.Slider(
173
+ label = "Font thickness",
174
+ minimum = 1,
175
+ maximum = 10,
176
+ step=1,
177
+ value = default_config["font_thickness"],
178
+ )
179
+ font_color = gr.ColorPicker(
180
+ label = "Font color",
181
+ value = "#8c8c8c",
182
+ visible=False,
183
+ )
184
  legend_image = gr.Image(label = "Legend")
185
  simplified_image = gr.Image(label = "Simplified image")
186
+ islands_image = gr.Image(label = "Islands (no numbers)", visible=False)
187
+ data = gr.State() # To store the data for font change
188
 
189
  # Submit button callback
190
  submit_button.click(
191
+ fn = callbacks.get_color_by_number,
192
  inputs = [
193
  image_path,
194
  number_of_colors,
 
203
  area_perc_threshold,
204
  check_shape_validity,
205
  arc_length_area_ratio_threshold,
206
+ font_size,
207
+ font_color,
208
+ font_thickness,
209
  *color_pickers
210
  ],
211
+ outputs = [color_by_number_image, legend_image, simplified_image, islands_image, data]
212
  )
213
 
214
+ # Callback to change font on image
215
+ gr.on(
216
+ triggers=[font_size.change, font_thickness.change],
217
+ fn = callbacks.change_font_on_image,
218
+ inputs = [
219
+ islands_image,
220
+ data,
221
+ font_size,
222
+ font_color,
223
+ font_thickness
224
+ ],
225
+ outputs = color_by_number_image
226
+ )
227
+
228
  demo.launch()
gradio_server/callbacks.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from colorbynumber.config import default_config
2
+ from colorbynumber.main import ColorByNumber
3
+ from colorbynumber.numbered_islands import add_numbers_to_image
4
+
5
+
6
+ def _hex_to_rgb(hex_color):
7
+ hex_color = hex_color.lstrip("#")
8
+ return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
9
+
10
+ def get_color_by_number(image_path, number_of_colors,
11
+ is_automatic_colors, num_colors,
12
+ denoise_flag, denoise_order, denoise_type,
13
+ blur_size, denoise_h,
14
+ open_kernel_size, area_perc_threshold,
15
+ check_shape_validity, arc_length_area_ratio_threshold,
16
+ font_size, font_color, font_thickness,
17
+ *color_list):
18
+ # Convert each color to r,g,b tuple
19
+ color_list = color_list[:num_colors]
20
+ color_list = [_hex_to_rgb(h) for h in color_list]
21
+
22
+ # Update config
23
+ config = default_config.copy()
24
+ config["denoise"] = denoise_flag
25
+ config["denoise_order"] = denoise_order
26
+ config["denoise_type"] = denoise_type
27
+ config["blur_size"] = blur_size
28
+ config["denoise_h"] = denoise_h
29
+ config["open_kernel_size"] = open_kernel_size
30
+ config["area_perc_threshold"] = area_perc_threshold
31
+ config["check_shape_validity"] = check_shape_validity
32
+ config["arc_length_area_ratio_threshold"] = arc_length_area_ratio_threshold
33
+ config["font_size"] = font_size
34
+ config["font_color"] = _hex_to_rgb(font_color)
35
+ config["font_thickness"] = font_thickness
36
+
37
+ if is_automatic_colors:
38
+ colorbynumber_obj = ColorByNumber(
39
+ image_path = image_path,
40
+ num_colors = number_of_colors,
41
+ config = config,
42
+ )
43
+ else:
44
+ colorbynumber_obj = ColorByNumber(
45
+ image_path = image_path,
46
+ color_list = color_list,
47
+ config = config,
48
+ )
49
+
50
+ numbered_islands = colorbynumber_obj.create_color_by_number()
51
+ data = {
52
+ "centroid_coords_list": colorbynumber_obj.centroid_coords_list,
53
+ "color_id_list": [color_id for color_id, _ in colorbynumber_obj.island_borders_list]
54
+ }
55
+ return numbered_islands, \
56
+ colorbynumber_obj.generate_color_legend(), \
57
+ colorbynumber_obj.simplified_image, \
58
+ colorbynumber_obj.islands_image, \
59
+ data
60
+
61
+ def change_font_on_image(image, data, font_size, font_color, font_thickness):
62
+ if image is None:
63
+ return None
64
+
65
+ centroid_coords_list = data["centroid_coords_list"]
66
+ color_id_list = data["color_id_list"]
67
+
68
+ font_color = _hex_to_rgb(font_color)
69
+ return add_numbers_to_image(
70
+ image = image,
71
+ centroid_coords_list = centroid_coords_list,
72
+ color_id_list = color_id_list,
73
+ font_size = font_size,
74
+ font_color = font_color,
75
+ font_thickness = font_thickness
76
+ )
gradio_server/doc.py CHANGED
@@ -6,6 +6,13 @@ def color_selection_block():
6
  or let the algorithm choose the best colors for you.
7
  """
8
 
 
 
 
 
 
 
 
9
  def parameters_block_header():
10
  return """
11
  ---
 
6
  or let the algorithm choose the best colors for you.
7
  """
8
 
9
+ def edit_coloring_page_block_header():
10
+ return """
11
+ ---
12
+ ## Edit Coloring Page
13
+ Change the font size and thickness of the numbers on the coloring page.
14
+ """
15
+
16
  def parameters_block_header():
17
  return """
18
  ---