jbilcke-hf HF Staff commited on
Commit
a0cbc48
·
verified ·
1 Parent(s): 4941fcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -49
app.py CHANGED
@@ -10,8 +10,29 @@ import PIL
10
  from PIL import Image
11
  from typing import Tuple
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  net=BriaRMBG()
14
- # model_path = "./model1.pth"
15
  model_path = hf_hub_download("briaai/RMBG-1.4", 'model.pth')
16
  if torch.cuda.is_available():
17
  net.load_state_dict(torch.load(model_path))
@@ -20,7 +41,7 @@ else:
20
  net.load_state_dict(torch.load(model_path,map_location="cpu"))
21
  net.eval()
22
 
23
-
24
  def resize_image(image):
25
  image = image.convert('RGB')
26
  model_input_size = (1024, 1024)
@@ -28,10 +49,10 @@ def resize_image(image):
28
  return image
29
 
30
 
31
- def process(image):
32
-
 
33
  # prepare input
34
- orig_image = Image.fromarray(image)
35
  w,h = orig_im_size = orig_image.size
36
  image = resize_image(orig_image)
37
  im_np = np.array(image)
@@ -55,52 +76,10 @@ def process(image):
55
  # paste the mask on the original image
56
  new_im = Image.new("RGBA", pil_im.size, (0,0,0,0))
57
  new_im.paste(orig_image, mask=pil_im)
58
- # new_orig_image = orig_image.convert('RGBA')
59
-
60
- return new_im
61
- # return [new_orig_image, new_im]
62
-
63
-
64
- # block = gr.Blocks().queue()
65
-
66
- # with block:
67
- # gr.Markdown("## BRIA RMBG 1.4")
68
- # gr.HTML('''
69
- # <p style="margin-bottom: 10px; font-size: 94%">
70
- # This is a demo for BRIA RMBG 1.4 that using
71
- # <a href="https://huggingface.co/briaai/RMBG-1.4" target="_blank">BRIA RMBG-1.4 image matting model</a> as backbone.
72
- # </p>
73
- # ''')
74
- # with gr.Row():
75
- # with gr.Column():
76
- # input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
77
- # # input_image = gr.Image(sources=None, type="numpy") # None for upload, ctrl+v and webcam
78
- # run_button = gr.Button(value="Run")
79
-
80
- # with gr.Column():
81
- # result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[1], height='auto')
82
- # ips = [input_image]
83
- # run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
84
-
85
- # block.launch(debug = True)
86
 
87
- # block = gr.Blocks().queue()
88
 
89
- gr.Markdown("## BRIA RMBG 1.4")
90
- gr.HTML('''
91
- <p style="margin-bottom: 10px; font-size: 94%">
92
- This is a demo for BRIA RMBG 1.4 that using
93
- <a href="https://huggingface.co/briaai/RMBG-1.4" target="_blank">BRIA RMBG-1.4 image matting model</a> as backbone.
94
- </p>
95
- ''')
96
- title = "Background Removal"
97
- description = r"""Background removal model developed by <a href='https://BRIA.AI' target='_blank'><b>BRIA.AI</b></a>, trained on a carefully selected dataset and is available as an open-source model for non-commercial use.<br>
98
- For test upload your image and wait. Read more at model card <a href='https://huggingface.co/briaai/RMBG-1.4' target='_blank'><b>briaai/RMBG-1.4</b></a>.<br>
99
- """
100
- examples = [['./input.jpg'],]
101
- # output = ImageSlider(position=0.5,label='Image without background', type="pil", show_download_button=True)
102
- # demo = gr.Interface(fn=process,inputs="image", outputs=output, examples=examples, title=title, description=description)
103
- demo = gr.Interface(fn=process,inputs="image", outputs="image", examples=examples, title=title, description=description)
104
 
105
  if __name__ == "__main__":
106
  demo.launch(share=False)
 
10
  from PIL import Image
11
  from typing import Tuple
12
 
13
+ from io import BytesIO
14
+ import base64
15
+ import re
16
+
17
+ # Regex pattern to match data URI scheme
18
+ data_uri_pattern = re.compile(r'data:image/(png|jpeg|jpg|webp);base64,')
19
+
20
+ def readb64(b64):
21
+ # Remove any data URI scheme prefix with regex
22
+ b64 = data_uri_pattern.sub("", b64)
23
+ # Decode and open the image with PIL
24
+ img = Image.open(BytesIO(base64.b64decode(b64)))
25
+ return img
26
+
27
+ # convert from PIL to base64
28
+ def writeb64(image):
29
+ buffered = BytesIO()
30
+ image.save(buffered, format="PNG")
31
+ b64image = base64.b64encode(buffered.getvalue())
32
+ b64image_str = b64image.decode("utf-8")
33
+ return b64image_str
34
+
35
  net=BriaRMBG()
 
36
  model_path = hf_hub_download("briaai/RMBG-1.4", 'model.pth')
37
  if torch.cuda.is_available():
38
  net.load_state_dict(torch.load(model_path))
 
41
  net.load_state_dict(torch.load(model_path,map_location="cpu"))
42
  net.eval()
43
 
44
+
45
  def resize_image(image):
46
  image = image.convert('RGB')
47
  model_input_size = (1024, 1024)
 
49
  return image
50
 
51
 
52
+ def process(image_base64):
53
+ orig_image = readb64(image_base64)
54
+
55
  # prepare input
 
56
  w,h = orig_im_size = orig_image.size
57
  image = resize_image(orig_image)
58
  im_np = np.array(image)
 
76
  # paste the mask on the original image
77
  new_im = Image.new("RGBA", pil_im.size, (0,0,0,0))
78
  new_im.paste(orig_image, mask=pil_im)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ return writeb64(new_im)
81
 
82
+ demo = gr.Interface(fn=process, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  if __name__ == "__main__":
85
  demo.launch(share=False)