yucelgumus61 commited on
Commit
0d558b5
·
verified ·
1 Parent(s): 4118a4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -6,13 +6,16 @@ from transformers import AutoModelForImageSegmentation
6
  import torch
7
  from torchvision import transforms
8
 
 
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
  torch.set_float32_matmul_precision("high")
11
 
 
12
  model = AutoModelForImageSegmentation.from_pretrained(
13
  "ZhengPeng7/BiRefNet", trust_remote_code=True
14
  ).to(device)
15
 
 
16
  transform_image = transforms.Compose(
17
  [
18
  transforms.Resize((1024, 1024)),
@@ -26,27 +29,32 @@ def remove_background(image):
26
  im = load_img(image, output_type="pil").convert("RGB")
27
  image_size = im.size
28
 
 
29
  input_image = transform_image(im).unsqueeze(0).to(device)
30
  with torch.no_grad():
31
  preds = model(input_image)[-1].sigmoid().cpu()
32
-
 
33
  mask = transforms.ToPILImage()(preds[0].squeeze()).resize(image_size)
34
- im.putalpha(mask)
35
-
36
- return im, im.copy()
37
 
38
- slider1 = ImageSlider(label="Arka Plan Kaldırıldı", type="pil")
39
- slider2 = ImageSlider(label="Arka Plan Kaldırıldı", type="pil")
 
 
40
  image_input = gr.Image(label="Resim Yükle")
41
  url_input = gr.Textbox(label="Resim URL'si")
42
 
 
43
  chameleon = load_img("elon.webp", output_type="pil")
44
  url_example = "https://dadanizm.com/wp-content/uploads/2022/11/elon-musk-992x2656.jpeg"
45
 
 
46
  tab1 = gr.Interface(
47
  fn=remove_background,
48
  inputs=image_input,
49
- outputs=slider1,
50
  examples=[chameleon],
51
  api_name="image"
52
  )
@@ -54,11 +62,12 @@ tab1 = gr.Interface(
54
  tab2 = gr.Interface(
55
  fn=remove_background,
56
  inputs=url_input,
57
- outputs=slider2,
58
  examples=[url_example],
59
  api_name="url"
60
  )
61
 
 
62
  demo = gr.TabbedInterface(
63
  [tab1, tab2],
64
  ["Resim", "URL"],
@@ -66,4 +75,4 @@ demo = gr.TabbedInterface(
66
  )
67
 
68
  if __name__ == "__main__":
69
- demo.launch(show_error=True)
 
6
  import torch
7
  from torchvision import transforms
8
 
9
+
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  torch.set_float32_matmul_precision("high")
12
 
13
+
14
  model = AutoModelForImageSegmentation.from_pretrained(
15
  "ZhengPeng7/BiRefNet", trust_remote_code=True
16
  ).to(device)
17
 
18
+
19
  transform_image = transforms.Compose(
20
  [
21
  transforms.Resize((1024, 1024)),
 
29
  im = load_img(image, output_type="pil").convert("RGB")
30
  image_size = im.size
31
 
32
+
33
  input_image = transform_image(im).unsqueeze(0).to(device)
34
  with torch.no_grad():
35
  preds = model(input_image)[-1].sigmoid().cpu()
36
+
37
+
38
  mask = transforms.ToPILImage()(preds[0].squeeze()).resize(image_size)
39
+ im_with_alpha = im.copy()
40
+ im_with_alpha.putalpha(mask)
 
41
 
42
+ return [im_with_alpha, im]
43
+
44
+
45
+ slider = ImageSlider(label="Arka Plan Kaldırıldı", images=[""])
46
  image_input = gr.Image(label="Resim Yükle")
47
  url_input = gr.Textbox(label="Resim URL'si")
48
 
49
+
50
  chameleon = load_img("elon.webp", output_type="pil")
51
  url_example = "https://dadanizm.com/wp-content/uploads/2022/11/elon-musk-992x2656.jpeg"
52
 
53
+
54
  tab1 = gr.Interface(
55
  fn=remove_background,
56
  inputs=image_input,
57
+ outputs=slider,
58
  examples=[chameleon],
59
  api_name="image"
60
  )
 
62
  tab2 = gr.Interface(
63
  fn=remove_background,
64
  inputs=url_input,
65
+ outputs=slider,
66
  examples=[url_example],
67
  api_name="url"
68
  )
69
 
70
+
71
  demo = gr.TabbedInterface(
72
  [tab1, tab2],
73
  ["Resim", "URL"],
 
75
  )
76
 
77
  if __name__ == "__main__":
78
+ demo.launch(show_error=True)