not-lain commited on
Commit
a72119e
1 Parent(s): fed2494
Files changed (2) hide show
  1. app.py +63 -0
  2. requirements.txt +16 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_imageslider import ImageSlider
3
+ from loadimg import load_img
4
+ import spaces
5
+ from transformers import AutoModelForImageSegmentation
6
+ import torch
7
+ from torchvision import transforms
8
+
9
+ torch.set_float32_matmul_precision(["high", "highest"][0])
10
+
11
+ birefnet = AutoModelForImageSegmentation.from_pretrained(
12
+ "ZhengPeng7/BiRefNet", trust_remote_code=True
13
+ )
14
+ birefnet.to("cuda")
15
+ transform_image = transforms.Compose(
16
+ [
17
+ transforms.Resize((1024, 1024)),
18
+ transforms.ToTensor(),
19
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
20
+ ]
21
+ )
22
+
23
+ def fn(vid):
24
+ # TODO
25
+ # loop over video and extract images and process each one
26
+ im = load_img(vid, output_type="pil")
27
+ im = im.convert("RGB")
28
+ image = process(im)
29
+ return image
30
+
31
+ @spaces.GPU
32
+ def process(image):
33
+ image_size = image.size
34
+ input_images = transform_image(image).unsqueeze(0).to("cuda")
35
+ # Prediction
36
+ with torch.no_grad():
37
+ preds = birefnet(input_images)[-1].sigmoid().cpu()
38
+ pred = preds[0].squeeze()
39
+ pred_pil = transforms.ToPILImage()(pred)
40
+ mask = pred_pil.resize(image_size)
41
+ image.putalpha(mask)
42
+ return image
43
+
44
+ def process_file(f):
45
+ name_path = f.rsplit(".",1)[0]+".png"
46
+ im = load_img(f, output_type="pil")
47
+ im = im.convert("RGB")
48
+ transparent = process(im)
49
+ transparent.save(name_path)
50
+ return name_path
51
+
52
+ in_video = gr.Video(label="birefnet")
53
+ out_video = gr.Video()
54
+
55
+
56
+ url = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
57
+ demo = gr.Interface(
58
+ fn, inputs=in_video, outputs=out_video, api_name="image"
59
+ )
60
+
61
+
62
+ if __name__ == "__main__":
63
+ demo.launch(show_error=True)
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ accelerate
3
+ opencv-python
4
+ spaces
5
+ pillow
6
+ numpy
7
+ timm
8
+ kornia
9
+ prettytable
10
+ typing
11
+ scikit-image
12
+ huggingface_hub
13
+ transformers>=4.39.1
14
+ gradio
15
+ gradio_imageslider
16
+ loadimg>=0.1.1