Shangyunle commited on
Commit
497d608
·
verified ·
1 Parent(s): 91512cc

🍻 bug: Torch use cpu

Browse files
Files changed (1) hide show
  1. app.py +100 -96
app.py CHANGED
@@ -1,97 +1,101 @@
1
- import os
2
- import gradio as gr
3
- import torch
4
- from model import MattingRefine
5
- import numpy as np
6
- import cv2
7
-
8
- os.makedirs("checkpoints", exist_ok=True)
9
- os.system("wget https://github.com/PeterL1n/BackgroundMattingV2/releases/download/v1.0.0/pytorch_mobilenetv2.pth -O ./checkpoints/pytorch_mobilenetv2.pth")
10
- os.system("wget https://image.pipzza.pw/1732802083405.png -O ./examples/tar.png")
11
- os.system("wget https://image.pipzza.pw/1732806849571.png -O ./examples/source.png")
12
- os.system("wget https://image.pipzza.pw/1732806850072.png -O ./examples/bg.png")
13
-
14
-
15
- model = MattingRefine(
16
- backbone='mobilenetv2',
17
- backbone_scale=.25,
18
- refine_mode='sampling',
19
- refine_sample_pixels=80_000
20
- )
21
- model.load_state_dict(torch.load('checkpoints/pytorch_mobilenetv2.pth'))
22
- model.eval()
23
-
24
-
25
- def alignImages(src, bgr2, pha, fgr):
26
- """
27
- Composite the foreground onto a new background image and adjust foreground exposure to match the new background
28
-
29
- Parameters:
30
- src: Original image
31
- bgr2: Target background image
32
- pha: Alpha mask
33
- fgr: Extracted foreground
34
- Returns:
35
- tuple: (alpha, composite) - Alpha mask and composited image
36
- """
37
- # Ensure input images have consistent dimensions
38
- height, width = src.shape[:2]
39
- bgr2 = cv2.resize(bgr2, (width, height))
40
-
41
- # Ensure alpha and foreground have correct dimensions
42
- if len(pha.shape) == 3:
43
- pha = pha.squeeze() # Remove extra dimensions
44
- pha = cv2.resize(pha, (width, height))
45
- fgr = cv2.resize(fgr, (width, height))
46
-
47
- # Composite final image
48
- alpha_3channel = np.stack([pha, pha, pha], axis=2).astype(float) / 255
49
- composite = fgr * alpha_3channel + bgr2 * (1 - alpha_3channel)
50
-
51
- return pha, composite.astype(np.uint8)
52
-
53
-
54
-
55
-
56
- def process_image(src_image, bgr_image, new_bgr_image):
57
- if src_image is None or bgr_image is None or new_bgr_image is None:
58
- raise gr.Error("Please provide all required images")
59
-
60
- src_image = np.array(src_image)
61
- bgr_image = np.array(bgr_image)
62
- new_bgr_image = np.array(new_bgr_image)
63
-
64
- src = torch.from_numpy(src_image).float().permute(2, 0, 1).unsqueeze(0) / 255
65
- bgr = torch.from_numpy(bgr_image).float().permute(2, 0, 1).unsqueeze(0) / 255
66
-
67
- with torch.no_grad():
68
- pha, fgr = model(src, bgr)[:2]
69
-
70
- alpha = (pha[0].cpu().numpy() * 255).astype(np.uint8)
71
- foreground = (fgr[0].permute(1, 2, 0).cpu().numpy() * 255).astype(np.uint8)
72
-
73
- alpha_out, composite = alignImages(src_image, new_bgr_image, alpha, foreground)
74
-
75
- return alpha_out, composite
76
-
77
- interface = gr.Interface(
78
- fn=process_image,
79
- inputs=[
80
- gr.Image(label="Source Image"),
81
- gr.Image(label="Background Image"),
82
- gr.Image(label="New Background Image")
83
- ],
84
- outputs=[
85
- gr.Image(label="Alpha Mask"),
86
- gr.Image(label="Foreground")
87
- ],
88
- title="Image Matting",
89
- description="Upload three images: 1. Source image 2. Background image 3. New background image. The system will extract the foreground and composite it onto the new background.",
90
- examples=[
91
- ["examples/source.png", "examples/bg.png", "examples/tar.png"],
92
- ],
93
- cache_examples=True # 缓存示例结果以加快加载速度
94
- )
95
-
96
- if __name__ == "__main__":
 
 
 
 
97
  interface.launch()
 
1
+ import os
2
+ import gradio as gr
3
+ import torch
4
+ from model import MattingRefine
5
+ import numpy as np
6
+ import cv2
7
+
8
+ device = torch.device('cpu')
9
+
10
+ os.makedirs("checkpoints", exist_ok=True)
11
+ os.makedirs("examples", exist_ok=True)
12
+
13
+ os.system("wget https://github.com/PeterL1n/BackgroundMattingV2/releases/download/v1.0.0/pytorch_mobilenetv2.pth -O ./checkpoints/pytorch_mobilenetv2.pth")
14
+ os.system("wget https://image.pipzza.pw/1732802083405.png -O ./examples/tar.png")
15
+ os.system("wget https://image.pipzza.pw/1732806849571.png -O ./examples/source.png")
16
+ os.system("wget https://image.pipzza.pw/1732806850072.png -O ./examples/bg.png")
17
+
18
+
19
+ model = MattingRefine(
20
+ backbone='mobilenetv2',
21
+ backbone_scale=.25,
22
+ refine_mode='sampling',
23
+ refine_sample_pixels=80_000
24
+ )
25
+ model.load_state_dict(torch.load('checkpoints/pytorch_mobilenetv2.pth', map_location=torch.device('cpu')))
26
+ model.eval()
27
+
28
+
29
+ def alignImages(src, bgr2, pha, fgr):
30
+ """
31
+ Composite the foreground onto a new background image and adjust foreground exposure to match the new background
32
+
33
+ Parameters:
34
+ src: Original image
35
+ bgr2: Target background image
36
+ pha: Alpha mask
37
+ fgr: Extracted foreground
38
+ Returns:
39
+ tuple: (alpha, composite) - Alpha mask and composited image
40
+ """
41
+ # Ensure input images have consistent dimensions
42
+ height, width = src.shape[:2]
43
+ bgr2 = cv2.resize(bgr2, (width, height))
44
+
45
+ # Ensure alpha and foreground have correct dimensions
46
+ if len(pha.shape) == 3:
47
+ pha = pha.squeeze() # Remove extra dimensions
48
+ pha = cv2.resize(pha, (width, height))
49
+ fgr = cv2.resize(fgr, (width, height))
50
+
51
+ # Composite final image
52
+ alpha_3channel = np.stack([pha, pha, pha], axis=2).astype(float) / 255
53
+ composite = fgr * alpha_3channel + bgr2 * (1 - alpha_3channel)
54
+
55
+ return pha, composite.astype(np.uint8)
56
+
57
+
58
+
59
+
60
+ def process_image(src_image, bgr_image, new_bgr_image):
61
+ if src_image is None or bgr_image is None or new_bgr_image is None:
62
+ raise gr.Error("Please provide all required images")
63
+
64
+ src_image = np.array(src_image)
65
+ bgr_image = np.array(bgr_image)
66
+ new_bgr_image = np.array(new_bgr_image)
67
+
68
+ src = torch.from_numpy(src_image).float().permute(2, 0, 1).unsqueeze(0) / 255
69
+ bgr = torch.from_numpy(bgr_image).float().permute(2, 0, 1).unsqueeze(0) / 255
70
+
71
+ with torch.no_grad():
72
+ pha, fgr = model(src, bgr)[:2]
73
+
74
+ alpha = (pha[0].cpu().numpy() * 255).astype(np.uint8)
75
+ foreground = (fgr[0].permute(1, 2, 0).cpu().numpy() * 255).astype(np.uint8)
76
+
77
+ alpha_out, composite = alignImages(src_image, new_bgr_image, alpha, foreground)
78
+
79
+ return alpha_out, composite
80
+
81
+ interface = gr.Interface(
82
+ fn=process_image,
83
+ inputs=[
84
+ gr.Image(label="Source Image"),
85
+ gr.Image(label="Background Image"),
86
+ gr.Image(label="New Background Image")
87
+ ],
88
+ outputs=[
89
+ gr.Image(label="Alpha Mask"),
90
+ gr.Image(label="Foreground")
91
+ ],
92
+ title="Image Matting",
93
+ description="Upload three images: 1. Source image 2. Background image 3. New background image. The system will extract the foreground and composite it onto the new background.",
94
+ examples=[
95
+ ["examples/source.png", "examples/bg.png", "examples/tar.png"],
96
+ ],
97
+ cache_examples=True # 缓存示例结果以加快加载速度
98
+ )
99
+
100
+ if __name__ == "__main__":
101
  interface.launch()