Spaces:
Runtime error
Runtime error
🍻 bug: Torch use cpu
Browse files
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 |
-
|
| 9 |
-
|
| 10 |
-
os.
|
| 11 |
-
os.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Ensure
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
gr.Image(label="
|
| 86 |
-
gr.Image(label="
|
| 87 |
-
],
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|