goryhon commited on
Commit
4c31f71
·
verified ·
1 Parent(s): 363b71e

Update web-demos/hugging_face/app.py

Browse files
Files changed (1) hide show
  1. web-demos/hugging_face/app.py +23 -5
web-demos/hugging_face/app.py CHANGED
@@ -209,16 +209,27 @@ def sam_refine(video_state, point_prompt, click_state, interactive_state, evt:gr
209
  ("[Optional]", "Click image"), (": Try to click the image shown in step2 if you want to generate more masks.\n", None)]
210
  return painted_image, video_state, interactive_state, operation_log, operation_log
211
 
212
- def add_multi_mask(video_state, interactive_state, mask_dropdown):
213
  try:
214
- mask = video_state["masks"][video_state["select_frame_number"]]
 
 
 
 
 
 
 
 
 
215
  interactive_state["multi_mask"]["masks"].append(mask)
216
  interactive_state["multi_mask"]["mask_names"].append("mask_{:03d}".format(len(interactive_state["multi_mask"]["masks"])))
217
  mask_dropdown.append("mask_{:03d}".format(len(interactive_state["multi_mask"]["masks"])))
 
218
  select_frame, _, _ = show_mask(video_state, interactive_state, mask_dropdown)
219
  operation_log = [("",""),("Added a mask, use the mask select for target tracking or inpainting.","Normal")]
220
- except:
221
- operation_log = [("Please click the image in step2 to generate masks.", "Error"), ("","")]
 
222
  return interactive_state, gr.update(choices=interactive_state["multi_mask"]["mask_names"], value=mask_dropdown), select_frame, [[],[]], operation_log, operation_log
223
 
224
  def clear_click(video_state, click_state):
@@ -639,13 +650,20 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as iface:
639
  step2_title = gr.Markdown("---\n## Step2: Add masks", visible=False)
640
  with gr.Row(equal_height=True):
641
  with gr.Column(scale=2):
642
- template_frame = gr.ImageEditor(type="numpy",label="Draw mask manually or click to add",interactive=True,visible=False,elem_id="template_frame",elem_classes="image",brush=gr.Brush(colors=["#FFFFFF"], default_color="#FFFFFF", default_size=2))
643
  image_selection_slider = gr.Slider(minimum=1, maximum=100, step=1, value=1, label="Track start frame", visible=False)
644
  track_pause_number_slider = gr.Slider(minimum=1, maximum=100, step=1, value=1, label="Track end frame", visible=False)
645
  with gr.Column(scale=2, elem_classes="jc_center"):
646
  run_status2 = gr.HighlightedText(value=[("",""), ("Use click or draw to select mask, then track or inpaint.", "Normal")],
647
  color_map={"Normal": "green", "Error": "red", "Clear clicks": "gray", "Add mask": "green", "Remove mask": "red"},
648
  visible=False)
 
 
 
 
 
 
 
649
  mask_mode_selector = gr.Radio(
650
  choices=["Click", "Draw"],
651
  value="Click",
 
209
  ("[Optional]", "Click image"), (": Try to click the image shown in step2 if you want to generate more masks.\n", None)]
210
  return painted_image, video_state, interactive_state, operation_log, operation_log
211
 
212
+ def add_multi_mask(video_state, interactive_state, mask_dropdown, mask_input_mode, template_frame):
213
  try:
214
+ if mask_input_mode == "Click":
215
+ # 🟢 Стандартная логика SAM
216
+ mask = video_state["masks"][video_state["select_frame_number"]]
217
+ else:
218
+ # ⚪ Рисованная маска
219
+ drawn_mask = template_frame # это уже numpy-массив из gr.Sketchpad
220
+ if drawn_mask is None:
221
+ raise ValueError("No mask was drawn.")
222
+ mask = (np.array(drawn_mask)[:, :, 0] > 127).astype(np.uint8) # бинаризация
223
+
224
  interactive_state["multi_mask"]["masks"].append(mask)
225
  interactive_state["multi_mask"]["mask_names"].append("mask_{:03d}".format(len(interactive_state["multi_mask"]["masks"])))
226
  mask_dropdown.append("mask_{:03d}".format(len(interactive_state["multi_mask"]["masks"])))
227
+
228
  select_frame, _, _ = show_mask(video_state, interactive_state, mask_dropdown)
229
  operation_log = [("",""),("Added a mask, use the mask select for target tracking or inpainting.","Normal")]
230
+ except Exception as e:
231
+ operation_log = [(f"Error: {str(e)}", "Error"), ("","")]
232
+
233
  return interactive_state, gr.update(choices=interactive_state["multi_mask"]["mask_names"], value=mask_dropdown), select_frame, [[],[]], operation_log, operation_log
234
 
235
  def clear_click(video_state, click_state):
 
650
  step2_title = gr.Markdown("---\n## Step2: Add masks", visible=False)
651
  with gr.Row(equal_height=True):
652
  with gr.Column(scale=2):
653
+ template_frame = gr.Sketchpad(label="Draw mask manually or click to add",brush_radius=10,shape=(720, 1280),interactive=True,visible=False,type="numpy",elem_id="template_frame",elem_classes="image")
654
  image_selection_slider = gr.Slider(minimum=1, maximum=100, step=1, value=1, label="Track start frame", visible=False)
655
  track_pause_number_slider = gr.Slider(minimum=1, maximum=100, step=1, value=1, label="Track end frame", visible=False)
656
  with gr.Column(scale=2, elem_classes="jc_center"):
657
  run_status2 = gr.HighlightedText(value=[("",""), ("Use click or draw to select mask, then track or inpaint.", "Normal")],
658
  color_map={"Normal": "green", "Error": "red", "Clear clicks": "gray", "Add mask": "green", "Remove mask": "red"},
659
  visible=False)
660
+ mask_input_mode = gr.Radio(
661
+ choices=["Click", "Draw"],
662
+ value="Click",
663
+ label="Select Mask Input Mode",
664
+ interactive=True,
665
+ visible=True
666
+ )
667
  mask_mode_selector = gr.Radio(
668
  choices=["Click", "Draw"],
669
  value="Click",