Spaces:
Build error
Build error
Maia Guo commited on
Commit ·
f58c419
0
Parent(s):
init clean repo
Browse files- README.md +14 -0
- app.py +94 -0
- requirements.txt +20 -0
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Image Seg Inpaint Demo
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.43.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: base data generation pipline
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torchvision.models.segmentation import deeplabv3_resnet50, DeepLabV3_ResNet50_Weights
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import cv2
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ---------------- 下载并加载 LaMa 官方权重 ----------------
|
| 11 |
+
repo_id = "saic-mdal/lama-big"
|
| 12 |
+
model_path = hf_hub_download(repo_id=repo_id, filename="big-lama.pt")
|
| 13 |
+
lama_model = torch.jit.load(model_path, map_location="cpu")
|
| 14 |
+
lama_model.eval()
|
| 15 |
+
|
| 16 |
+
print("torch:", torch.__version__)
|
| 17 |
+
print("numpy:", np.__version__)
|
| 18 |
+
|
| 19 |
+
# ---- 加载分割模型(CPU) ----
|
| 20 |
+
device = torch.device("cpu")
|
| 21 |
+
weights = DeepLabV3_ResNet50_Weights.COCO_WITH_VOC_LABELS_V1
|
| 22 |
+
model = deeplabv3_resnet50(weights=weights).to(device).eval()
|
| 23 |
+
preprocess = weights.transforms()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
MAX_SIDE = 1024 # 为了速度与内存,限制输入最大边
|
| 27 |
+
|
| 28 |
+
def _resize_if_needed(pil_img: Image.Image, max_side=MAX_SIDE) -> Image.Image:
|
| 29 |
+
w, h = pil_img.size
|
| 30 |
+
if max(w, h) <= max_side:
|
| 31 |
+
return pil_img
|
| 32 |
+
r = max_side / float(max(w, h))
|
| 33 |
+
return pil_img.resize((int(w * r), int(h * r)), Image.BILINEAR)
|
| 34 |
+
|
| 35 |
+
def segment(image: Image.Image):
|
| 36 |
+
print("DEBUG: type(image) =", type(image), "mode=", getattr(image, "mode", None))
|
| 37 |
+
if not isinstance(image, Image.Image):
|
| 38 |
+
image = Image.fromarray(image)
|
| 39 |
+
|
| 40 |
+
image = image.convert("RGB")
|
| 41 |
+
image = _resize_if_needed(image)
|
| 42 |
+
|
| 43 |
+
# 预处理并推理
|
| 44 |
+
x = torch.from_numpy(np.array(image)).permute(2, 0, 1).float() / 255.0
|
| 45 |
+
x = x.unsqueeze(0).to(device) # [1,3,H,W]
|
| 46 |
+
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
out = model(x)["out"][0] # [C,H,W],C=21(含背景)
|
| 49 |
+
pred = out.argmax(0).cpu().numpy() # [H,W]
|
| 50 |
+
|
| 51 |
+
# 前景 = 非背景(背景类在COCO VOC权重下是0)
|
| 52 |
+
fg = (pred != 0).astype(np.uint8)
|
| 53 |
+
|
| 54 |
+
# ---------------- mask 膨胀 ----------------
|
| 55 |
+
kernel = np.ones((19,19), np.uint8)
|
| 56 |
+
fg_dilated = cv2.dilate(fg, kernel, iterations=1)
|
| 57 |
+
print("add dilated process!")
|
| 58 |
+
|
| 59 |
+
mask_img = Image.fromarray((fg_dilated * 255).astype(np.uint8), mode="L")
|
| 60 |
+
|
| 61 |
+
# 叠加彩色遮罩(红色半透明)
|
| 62 |
+
base = image.convert("RGBA")
|
| 63 |
+
overlay = Image.new("RGBA", base.size, (255, 0, 0, 0))
|
| 64 |
+
alpha = Image.fromarray((fg_dilated * 120).astype(np.uint8))
|
| 65 |
+
overlay.putalpha(alpha)
|
| 66 |
+
blended = Image.alpha_composite(base, overlay).convert("RGB")
|
| 67 |
+
|
| 68 |
+
# ---- LaMa 擦除 ----
|
| 69 |
+
img_np = np.array(image) # HWC, uint8
|
| 70 |
+
mask_np = np.array(mask_img) # H,W, 0/255
|
| 71 |
+
img_t = torch.from_numpy(img_np).permute(2, 0, 1).float().unsqueeze(0) / 255.0
|
| 72 |
+
mask_t = torch.from_numpy(mask_np).unsqueeze(0).unsqueeze(0).float() / 255.0
|
| 73 |
+
with torch.no_grad():
|
| 74 |
+
inpainted_t = lama_model(img_t, mask_t) # [1,3,H,W]
|
| 75 |
+
inpainted_np = (inpainted_t[0].permute(1, 2, 0).numpy() * 255).astype(np.uint8)
|
| 76 |
+
inpainted_img = Image.fromarray(inpainted_np)
|
| 77 |
+
|
| 78 |
+
return blended, mask_img, inpainted_img
|
| 79 |
+
|
| 80 |
+
# ---- Gradio 界面 ----
|
| 81 |
+
demo = gr.Interface(
|
| 82 |
+
fn=segment,
|
| 83 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 84 |
+
outputs=[
|
| 85 |
+
gr.Image(type="pil", label="Overlay (foreground)"),
|
| 86 |
+
gr.Image(type="pil", label="Binary Mask (foreground=white)"),
|
| 87 |
+
gr.Image(type="pil", label="inpaint result"),
|
| 88 |
+
],
|
| 89 |
+
title="Semantic Segmentation + LaMa Inpainting",
|
| 90 |
+
description="DeepLabV3 分割 + Mask 膨胀 + LaMa 擦除,运行在 CPU 环境。"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---- PyTorch (CPU 版本,适配 Hugging Face Space 免费环境) ----
|
| 2 |
+
torch==2.0.1+cpu
|
| 3 |
+
torchvision==0.15.2+cpu
|
| 4 |
+
-f https://download.pytorch.org/whl/torch_stable.html
|
| 5 |
+
numpy==1.24.4
|
| 6 |
+
|
| 7 |
+
# ---- Transformers (分割模型用) ----
|
| 8 |
+
transformers
|
| 9 |
+
timm # DETR 依赖
|
| 10 |
+
|
| 11 |
+
# ---- 图像处理 ----
|
| 12 |
+
Pillow
|
| 13 |
+
opencv-python
|
| 14 |
+
|
| 15 |
+
# ---- Web 界面 ----
|
| 16 |
+
gradio>=4.0.0
|
| 17 |
+
|
| 18 |
+
# ---- LaMa inpainting 后续需要 ----
|
| 19 |
+
#lama-cleaner==0.27.1
|
| 20 |
+
huggingface_hub
|