WildanJR commited on
Commit
889e680
·
verified ·
1 Parent(s): f18e166

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -21,44 +21,41 @@ else:
21
  net.eval()
22
 
23
 
24
- def resize_image(image):
25
  image = image.convert('RGB')
26
- model_input_size = (1024, 1024)
27
- image = image.resize(model_input_size, Image.BILINEAR)
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)
38
- im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2,0,1)
39
- im_tensor = torch.unsqueeze(im_tensor,0)
40
- im_tensor = torch.divide(im_tensor,255.0)
41
- im_tensor = normalize(im_tensor,[0.5,0.5,0.5],[1.0,1.0,1.0])
42
  if torch.cuda.is_available():
43
- im_tensor=im_tensor.cuda()
44
 
45
- #inference
46
- result=net(im_tensor)
47
- # post process
48
- result = torch.squeeze(F.interpolate(result[0][0], size=(h,w), mode='bilinear') ,0)
49
- ma = torch.max(result)
50
- mi = torch.min(result)
51
- result = (result-mi)/(ma-mi)
52
- # image to pil
53
- im_array = (result*255).cpu().data.numpy().astype(np.uint8)
54
- pil_im = Image.fromarray(np.squeeze(im_array))
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
  # block = gr.Blocks().queue()
64
 
@@ -96,10 +93,18 @@ title = "Human Body Segmentation"
96
  description = r"""Human Body Segmentation model developed by <a href='https://github.com/WildanJR09' target='_blank'><b>WildanJR</b></a>, Designed to effectively separate foreground from background in a range of categories and image types.<br>
97
  This model has been trained on a carefully selected dataset, which includes: general stock images, e-commerce, gaming, and advertising content, making it suitable for commercial use cases powering enterprise content creation at scale. The accuracy, efficiency, and versatility currently rival leading source-available models. It is ideal where content safety, legally licensed datasets, and bias mitigation are paramount. For test upload your image and wait. </a>.<br>
98
  """
99
- examples = [['./jisoo.jpg'],]
 
100
  # output = ImageSlider(position=0.5,label='Image without background', type="pil", show_download_button=True)
101
  # demo = gr.Interface(fn=process,inputs="image", outputs=output, examples=examples, title=title, description=description)
102
- demo = gr.Interface(fn=process,inputs="image", outputs="image", examples=examples, title=title, description=description)
 
 
 
 
 
 
 
103
 
104
  if __name__ == "__main__":
105
  demo.launch(share=False)
 
21
  net.eval()
22
 
23
 
24
+ def resize_image(image, size=(1024, 1024)):
25
  image = image.convert('RGB')
26
+ return image.resize(size, Image.BILINEAR)
 
 
27
 
28
 
29
+ def process(image, background_image):
30
+ # Prepare and resize images
31
+ orig_image = Image.fromarray(image).convert("RGBA")
32
+ background_image = resize_image(Image.fromarray(background_image).convert("RGBA"))
33
+
34
+ w, h = orig_image.size
35
  image = resize_image(orig_image)
36
  im_np = np.array(image)
37
+ im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
38
+ im_tensor = torch.unsqueeze(im_tensor, 0) / 255.0
39
+ im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
40
+
41
  if torch.cuda.is_available():
42
+ im_tensor = im_tensor.cuda()
43
 
44
+ # Inference
45
+ result = net(im_tensor)
46
+
47
+ # Post-process mask
48
+ result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
49
+ result = (result - result.min()) / (result.max() - result.min())
50
+ im_array = (result * 255).cpu().numpy().astype(np.uint8)
51
+ mask = Image.fromarray(im_array).convert("L")
 
 
 
 
 
 
52
 
53
+ # Composite with new background
54
+ foreground = orig_image.copy()
55
+ new_background = background_image.resize((w, h))
56
+ new_background.paste(foreground, mask=mask)
57
+
58
+ return new_background
59
 
60
  # block = gr.Blocks().queue()
61
 
 
93
  description = r"""Human Body Segmentation model developed by <a href='https://github.com/WildanJR09' target='_blank'><b>WildanJR</b></a>, Designed to effectively separate foreground from background in a range of categories and image types.<br>
94
  This model has been trained on a carefully selected dataset, which includes: general stock images, e-commerce, gaming, and advertising content, making it suitable for commercial use cases powering enterprise content creation at scale. The accuracy, efficiency, and versatility currently rival leading source-available models. It is ideal where content safety, legally licensed datasets, and bias mitigation are paramount. For test upload your image and wait. </a>.<br>
95
  """
96
+ examples = [['./jisoo.jpg', './background.jpg']]
97
+
98
  # output = ImageSlider(position=0.5,label='Image without background', type="pil", show_download_button=True)
99
  # demo = gr.Interface(fn=process,inputs="image", outputs=output, examples=examples, title=title, description=description)
100
+ demo = gr.Interface(
101
+ fn=process,
102
+ inputs=["image", "image"],
103
+ outputs="image",
104
+ examples=examples,
105
+ title=title,
106
+ description=description
107
+ )
108
 
109
  if __name__ == "__main__":
110
  demo.launch(share=False)