wildoctopus commited on
Commit
6e7f058
·
verified ·
1 Parent(s): 1369068

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -21,7 +21,6 @@ def run(img):
21
  cloth_seg = generate_mask(img, net=net, palette=palette, device=device)
22
  return cloth_seg
23
 
24
- # CSS styling
25
  css = '''
26
  .container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
27
  #image_upload{min-height:400px}
@@ -34,8 +33,6 @@ css = '''
34
  #image_upload .touch-none{display: flex}
35
  '''
36
 
37
- # Collect example images
38
- example = {}
39
  image_dir = 'input'
40
  image_list = [os.path.join(image_dir, file) for file in os.listdir(image_dir)]
41
  image_list.sort()
@@ -43,23 +40,48 @@ image_list.sort()
43
  with gr.Blocks(css=css) as demo:
44
  gr.HTML(read_content("header.html"))
45
 
46
- with gr.Row():
47
- with gr.Column():
48
- image = gr.Image(source='upload', elem_id="image_upload", type="pil", label="Input Image")
 
 
 
 
 
49
 
50
- with gr.Column():
51
- image_out = gr.Image(label="Output", elem_id="output-img")
52
 
53
- with gr.Row():
54
- gr.Examples(
55
- examples=image_list,
56
- inputs=[image],
57
- label="Examples - Input Images",
58
- examples_per_page=12
 
 
 
 
 
 
 
59
  )
60
- btn = gr.Button("Run!")
61
 
62
- btn.click(fn=run, inputs=[image], outputs=[image_out])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  gr.HTML(
65
  """
@@ -73,5 +95,3 @@ with gr.Blocks(css=css) as demo:
73
  </div>
74
  """
75
  )
76
-
77
- demo.launch()
 
21
  cloth_seg = generate_mask(img, net=net, palette=palette, device=device)
22
  return cloth_seg
23
 
 
24
  css = '''
25
  .container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
26
  #image_upload{min-height:400px}
 
33
  #image_upload .touch-none{display: flex}
34
  '''
35
 
 
 
36
  image_dir = 'input'
37
  image_list = [os.path.join(image_dir, file) for file in os.listdir(image_dir)]
38
  image_list.sort()
 
40
  with gr.Blocks(css=css) as demo:
41
  gr.HTML(read_content("header.html"))
42
 
43
+ input_method = gr.Radio(
44
+ choices=["Upload Image", "Use Webcam"],
45
+ label="Choose Input Method",
46
+ value="Upload Image"
47
+ )
48
+
49
+ upload_image = gr.Image(label="Upload Image", type="pil", visible=True)
50
+ webcam_input = gr.Camera(label="Webcam Image", visible=False)
51
 
52
+ image_out = gr.Image(label="Output", elem_id="output-img")
53
+ btn = gr.Button("Run!")
54
 
55
+ # Examples only apply to uploaded images
56
+ gr.Examples(
57
+ examples=image_list,
58
+ inputs=[upload_image],
59
+ label="Examples - Input Images",
60
+ examples_per_page=12
61
+ )
62
+
63
+ # Logic to toggle input components based on radio selection
64
+ def toggle_inputs(choice):
65
+ return (
66
+ gr.update(visible=choice == "Upload Image"),
67
+ gr.update(visible=choice == "Use Webcam")
68
  )
 
69
 
70
+ input_method.change(
71
+ fn=toggle_inputs,
72
+ inputs=input_method,
73
+ outputs=[upload_image, webcam_input]
74
+ )
75
+
76
+ # Button logic: use the active image component
77
+ def conditional_run(uploaded, webcam, method):
78
+ return run(uploaded if method == "Upload Image" else webcam)
79
+
80
+ btn.click(
81
+ fn=conditional_run,
82
+ inputs=[upload_image, webcam_input, input_method],
83
+ outputs=[image_out]
84
+ )
85
 
86
  gr.HTML(
87
  """
 
95
  </div>
96
  """
97
  )