attention-refocusing commited on
Commit
270f5ec
β€’
1 Parent(s): 526c7ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -141,6 +141,8 @@ class ImageMask(gr.components.Image):
141
 
142
  decode_image = processing_utils.decode_base64_to_image(x)
143
  width, height = decode_image.size
 
 
144
 
145
  mask = np.zeros((height, width, 4), dtype=np.uint8)
146
 
@@ -148,7 +150,9 @@ class ImageMask(gr.components.Image):
148
  mask = self.postprocess(mask)
149
  x = {'image': x, 'mask': mask}
150
  print('vao preprocess-------------------------')
151
- hh = super().preprocess(x)
 
 
152
 
153
  return hh
154
 
@@ -446,8 +450,9 @@ def draw(task, input, grounding_texts, new_image_trigger, state, generate_parsed
446
 
447
  return [box_image, new_image_trigger, image_scale, state, generate_parsed]
448
 
449
- def change_state(bboxes,layout, state, instruction):
450
-
 
451
  # mask =
452
  state['boxes'] = []
453
  state['masks'] = []
@@ -470,14 +475,13 @@ def change_state(bboxes,layout, state, instruction):
470
  grounding_texts += [f'Obj. {bid+1}' for bid in range(len(grounding_texts), len(result))]
471
 
472
  box_image = draw_box(result, grounding_texts)
473
- mask= np.asarray(box_image)
474
 
475
- mask = binarize_2(mask)
476
  state['masks'].append(mask.copy())
477
  # print('done change state', state)
478
  print('done change state')
479
  # import pdb; pdb.set_trace()
480
- return [box_image,state]
481
 
482
  def example_click(name, grounding_instruction, instruction, bboxes,generate_parsed, trigger_parsed):
483
 
@@ -497,15 +501,16 @@ def example_click(name, grounding_instruction, instruction, bboxes,generate_pars
497
  print('done the example click')
498
  return [box_image, trigger_parsed]
499
 
500
- def clear(task, sketch_pad_trigger, batch_size, state, switch_task=False):
501
 
502
  sketch_pad_trigger = sketch_pad_trigger + 1
 
503
  blank_samples = batch_size % 2 if batch_size > 1 else 0
504
  out_images = [gr.Image.update(value=None, visible=True) for i in range(batch_size)] \
505
  + [gr.Image.update(value=None, visible=True) for _ in range(blank_samples)] \
506
  + [gr.Image.update(value=None, visible=False) for _ in range(4 - batch_size - blank_samples)]
507
  state = {}
508
- return [None, sketch_pad_trigger, None, 1.0] + out_images + [state]
509
 
510
  css = """
511
  #img2img_image, #img2img_image > .fixed-height, #img2img_image > .fixed-height > div, #img2img_image > .fixed-height > div > img
@@ -655,8 +660,8 @@ with Blocks(
655
  )
656
  trigger_stage.change(
657
  change_state,
658
- inputs=[text_box,sketch_pad, state, grounding_instruction],
659
- outputs=[out_imagebox,state],
660
  queue=True
661
  )
662
  grounding_instruction.change(
@@ -667,8 +672,8 @@ with Blocks(
667
  )
668
  clear_btn.click(
669
  clear,
670
- inputs=[task, sketch_pad_trigger, batch_size, state],
671
- outputs=[sketch_pad, sketch_pad_trigger, out_imagebox, image_scale, out_gen_1, out_gen_2, out_gen_3, out_gen_4, state],
672
  queue=False)
673
 
674
  sketch_pad_trigger.change(
 
141
 
142
  decode_image = processing_utils.decode_base64_to_image(x)
143
  width, height = decode_image.size
144
+ img = np.asarray(decode_image)
145
+ return {'image':img, 'mask':binarize_2(img)}
146
 
147
  mask = np.zeros((height, width, 4), dtype=np.uint8)
148
 
 
150
  mask = self.postprocess(mask)
151
  x = {'image': x, 'mask': mask}
152
  print('vao preprocess-------------------------')
153
+ if (hh['image'].min()!=255) and (hh['mask'][:,:,:3].max()==0):
154
+
155
+ hh['mask'] = binarize_2(hh['image'])
156
 
157
  return hh
158
 
 
450
 
451
  return [box_image, new_image_trigger, image_scale, state, generate_parsed]
452
 
453
+ def change_state(bboxes,layout, state, instruction, trigger_stage, boxes):
454
+ if trigger_stage ==0 :
455
+ return [boxes, state, 0]
456
  # mask =
457
  state['boxes'] = []
458
  state['masks'] = []
 
475
  grounding_texts += [f'Obj. {bid+1}' for bid in range(len(grounding_texts), len(result))]
476
 
477
  box_image = draw_box(result, grounding_texts)
 
478
 
479
+ mask = binarize_2(layout['image'])
480
  state['masks'].append(mask.copy())
481
  # print('done change state', state)
482
  print('done change state')
483
  # import pdb; pdb.set_trace()
484
+ return [box_image,state, trigger_stage]
485
 
486
  def example_click(name, grounding_instruction, instruction, bboxes,generate_parsed, trigger_parsed):
487
 
 
501
  print('done the example click')
502
  return [box_image, trigger_parsed]
503
 
504
+ def clear(task, sketch_pad_trigger, batch_size, state,trigger_stage, switch_task=False):
505
 
506
  sketch_pad_trigger = sketch_pad_trigger + 1
507
+ trigger_stage = 0
508
  blank_samples = batch_size % 2 if batch_size > 1 else 0
509
  out_images = [gr.Image.update(value=None, visible=True) for i in range(batch_size)] \
510
  + [gr.Image.update(value=None, visible=True) for _ in range(blank_samples)] \
511
  + [gr.Image.update(value=None, visible=False) for _ in range(4 - batch_size - blank_samples)]
512
  state = {}
513
+ return [None, sketch_pad_trigger, None, 1.0] + out_images + [state] + [trigger_stage]
514
 
515
  css = """
516
  #img2img_image, #img2img_image > .fixed-height, #img2img_image > .fixed-height > div, #img2img_image > .fixed-height > div > img
 
660
  )
661
  trigger_stage.change(
662
  change_state,
663
+ inputs=[text_box,sketch_pad, state, grounding_instruction, trigger_stage,out_imagebox],
664
+ outputs=[out_imagebox,state,trigger_stage],
665
  queue=True
666
  )
667
  grounding_instruction.change(
 
672
  )
673
  clear_btn.click(
674
  clear,
675
+ inputs=[task, sketch_pad_trigger, batch_size,trigger_stage, state],
676
+ outputs=[sketch_pad, sketch_pad_trigger, out_imagebox, image_scale, out_gen_1, out_gen_2, out_gen_3, out_gen_4, state, trigger_stage],
677
  queue=False)
678
 
679
  sketch_pad_trigger.change(