Spaces:
Running
on
Zero
Running
on
Zero
gradio==5.24.0
Browse files- .pre-commit-config.yaml +11 -33
- .python-version +1 -0
- .vscode/extensions.json +8 -0
- .vscode/settings.json +10 -14
- README.md +1 -2
- app.py +75 -56
- pyproject.toml +54 -0
- requirements.txt +233 -6
- style.css +1 -7
- uv.lock +0 -0
.pre-commit-config.yaml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
repos:
|
| 2 |
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 3 |
-
rev:
|
| 4 |
hooks:
|
| 5 |
- id: check-executables-have-shebangs
|
| 6 |
- id: check-json
|
|
@@ -13,43 +13,21 @@ repos:
|
|
| 13 |
args: ["--fix=lf"]
|
| 14 |
- id: requirements-txt-fixer
|
| 15 |
- id: trailing-whitespace
|
| 16 |
-
- repo: https://github.com/
|
| 17 |
-
rev:
|
| 18 |
hooks:
|
| 19 |
-
- id:
|
| 20 |
-
args: ["--
|
| 21 |
-
|
| 22 |
-
rev: 5.12.0
|
| 23 |
-
hooks:
|
| 24 |
-
- id: isort
|
| 25 |
-
args: ["--profile", "black"]
|
| 26 |
- repo: https://github.com/pre-commit/mirrors-mypy
|
| 27 |
-
rev: v1.
|
| 28 |
hooks:
|
| 29 |
- id: mypy
|
| 30 |
args: ["--ignore-missing-imports"]
|
| 31 |
additional_dependencies:
|
| 32 |
-
["types-python-slugify", "types-requests", "types-PyYAML"]
|
| 33 |
-
- repo: https://github.com/psf/black
|
| 34 |
-
rev: 23.9.1
|
| 35 |
-
hooks:
|
| 36 |
-
- id: black
|
| 37 |
-
language_version: python3.10
|
| 38 |
-
args: ["--line-length", "119"]
|
| 39 |
-
- repo: https://github.com/kynan/nbstripout
|
| 40 |
-
rev: 0.6.1
|
| 41 |
-
hooks:
|
| 42 |
-
- id: nbstripout
|
| 43 |
-
args:
|
| 44 |
[
|
| 45 |
-
"
|
| 46 |
-
"
|
|
|
|
|
|
|
| 47 |
]
|
| 48 |
-
- repo: https://github.com/nbQA-dev/nbQA
|
| 49 |
-
rev: 1.7.0
|
| 50 |
-
hooks:
|
| 51 |
-
- id: nbqa-black
|
| 52 |
-
- id: nbqa-pyupgrade
|
| 53 |
-
args: ["--py37-plus"]
|
| 54 |
-
- id: nbqa-isort
|
| 55 |
-
args: ["--float-to-top"]
|
|
|
|
| 1 |
repos:
|
| 2 |
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 3 |
+
rev: v5.0.0
|
| 4 |
hooks:
|
| 5 |
- id: check-executables-have-shebangs
|
| 6 |
- id: check-json
|
|
|
|
| 13 |
args: ["--fix=lf"]
|
| 14 |
- id: requirements-txt-fixer
|
| 15 |
- id: trailing-whitespace
|
| 16 |
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
| 17 |
+
rev: v0.11.2
|
| 18 |
hooks:
|
| 19 |
+
- id: ruff
|
| 20 |
+
args: ["--fix"]
|
| 21 |
+
- id: ruff-format
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
- repo: https://github.com/pre-commit/mirrors-mypy
|
| 23 |
+
rev: v1.15.0
|
| 24 |
hooks:
|
| 25 |
- id: mypy
|
| 26 |
args: ["--ignore-missing-imports"]
|
| 27 |
additional_dependencies:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
[
|
| 29 |
+
"types-python-slugify",
|
| 30 |
+
"types-pytz",
|
| 31 |
+
"types-PyYAML",
|
| 32 |
+
"types-requests",
|
| 33 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.10
|
.vscode/extensions.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"recommendations": [
|
| 3 |
+
"ms-python.python",
|
| 4 |
+
"charliermarsh.ruff",
|
| 5 |
+
"streetsidesoftware.code-spell-checker",
|
| 6 |
+
"tamasfe.even-better-toml"
|
| 7 |
+
]
|
| 8 |
+
}
|
.vscode/settings.json
CHANGED
|
@@ -1,21 +1,17 @@
|
|
| 1 |
{
|
|
|
|
|
|
|
| 2 |
"[python]": {
|
| 3 |
-
"editor.defaultFormatter": "
|
| 4 |
"editor.formatOnType": true,
|
| 5 |
"editor.codeActionsOnSave": {
|
| 6 |
-
"source.
|
|
|
|
| 7 |
}
|
| 8 |
},
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"--max-line-length=119"
|
| 15 |
-
],
|
| 16 |
-
"ruff.args": [
|
| 17 |
-
"--line-length=119"
|
| 18 |
-
],
|
| 19 |
-
"editor.formatOnSave": true,
|
| 20 |
-
"files.insertFinalNewline": true
|
| 21 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"editor.formatOnSave": true,
|
| 3 |
+
"files.insertFinalNewline": false,
|
| 4 |
"[python]": {
|
| 5 |
+
"editor.defaultFormatter": "charliermarsh.ruff",
|
| 6 |
"editor.formatOnType": true,
|
| 7 |
"editor.codeActionsOnSave": {
|
| 8 |
+
"source.fixAll.ruff": "explicit",
|
| 9 |
+
"source.organizeImports": "explicit"
|
| 10 |
}
|
| 11 |
},
|
| 12 |
+
"[jupyter]": {
|
| 13 |
+
"files.insertFinalNewline": false
|
| 14 |
+
},
|
| 15 |
+
"notebook.output.scrolling": true,
|
| 16 |
+
"notebook.formatOnSave.enabled": true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
README.md
CHANGED
|
@@ -4,8 +4,7 @@ emoji: 📉
|
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: 3.10.13
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
|
|
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.24.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
CHANGED
|
@@ -12,12 +12,33 @@ from transformers import VitMatteForImageMatting, VitMatteImageProcessor
|
|
| 12 |
DESCRIPTION = """\
|
| 13 |
# [ViTMatte](https://github.com/hustvl/ViTMatte)
|
| 14 |
|
| 15 |
-
This is
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
If you wish to replace background of the image, simply select the checkbox and drag and drop your background image.
|
| 19 |
|
| 20 |
-
You can draw your own foreground mask and unknown (border) mask using the canvas.
|
| 21 |
"""
|
| 22 |
|
| 23 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
@@ -29,22 +50,31 @@ processor = VitMatteImageProcessor.from_pretrained(MODEL_ID)
|
|
| 29 |
model = VitMatteForImageMatting.from_pretrained(MODEL_ID).to(device)
|
| 30 |
|
| 31 |
|
| 32 |
-
def
|
|
|
|
|
|
|
| 33 |
if max(image.size) > MAX_IMAGE_SIZE:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def binarize_mask(mask: np.ndarray) -> np.ndarray:
|
| 38 |
-
mask[mask < 128] = 0
|
| 39 |
mask[mask > 0] = 1
|
| 40 |
return mask
|
| 41 |
|
| 42 |
|
| 43 |
-
def update_trimap(
|
| 44 |
-
foreground =
|
| 45 |
foreground = binarize_mask(foreground)
|
| 46 |
|
| 47 |
-
unknown =
|
| 48 |
unknown = binarize_mask(unknown)
|
| 49 |
|
| 50 |
trimap = np.zeros_like(foreground)
|
|
@@ -65,8 +95,7 @@ def adjust_background_image(background_image: PIL.Image.Image, target_size: tupl
|
|
| 65 |
top = (new_bg_h - target_h) // 2
|
| 66 |
right = left + target_w
|
| 67 |
bottom = top + target_h
|
| 68 |
-
|
| 69 |
-
return background_image
|
| 70 |
|
| 71 |
|
| 72 |
def replace_background(
|
|
@@ -84,8 +113,7 @@ def replace_background(
|
|
| 84 |
image = np.array(image).astype(float) / 255
|
| 85 |
background_image = np.array(background_image).astype(float) / 255
|
| 86 |
result = image * alpha[:, :, None] + background_image * (1 - alpha[:, :, None])
|
| 87 |
-
|
| 88 |
-
return result
|
| 89 |
|
| 90 |
|
| 91 |
@spaces.GPU
|
|
@@ -99,7 +127,8 @@ def run(
|
|
| 99 |
if image.size != trimap.size:
|
| 100 |
raise gr.Error("Image and trimap must have the same size.")
|
| 101 |
if max(image.size) > MAX_IMAGE_SIZE:
|
| 102 |
-
|
|
|
|
| 103 |
if image.mode != "RGB":
|
| 104 |
raise gr.Error("Image must be RGB.")
|
| 105 |
if trimap.mode != "L":
|
|
@@ -116,56 +145,51 @@ def run(
|
|
| 116 |
foreground = (foreground * 255).astype(np.uint8)
|
| 117 |
foreground = PIL.Image.fromarray(foreground)
|
| 118 |
|
| 119 |
-
if apply_background_replacement
|
| 120 |
-
res_bg_replacement = replace_background(image, alpha, background_image)
|
| 121 |
-
else:
|
| 122 |
-
res_bg_replacement = None
|
| 123 |
|
| 124 |
return alpha, foreground, res_bg_replacement
|
| 125 |
|
| 126 |
|
| 127 |
-
with gr.Blocks(
|
| 128 |
gr.Markdown(DESCRIPTION)
|
| 129 |
-
gr.DuplicateButton(
|
| 130 |
-
value="Duplicate Space for private use",
|
| 131 |
-
elem_id="duplicate-button",
|
| 132 |
-
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
| 133 |
-
)
|
| 134 |
|
| 135 |
with gr.Row():
|
| 136 |
with gr.Column():
|
| 137 |
-
with gr.
|
| 138 |
-
image = gr.Image(label="Input image", type="pil"
|
| 139 |
with gr.Tabs():
|
| 140 |
with gr.Tab(label="Trimap"):
|
| 141 |
-
trimap = gr.Image(label="Trimap", type="pil", image_mode="L"
|
| 142 |
with gr.Tab(label="Draw trimap"):
|
| 143 |
-
|
| 144 |
-
foreground_mask = gr.Image(
|
| 145 |
label="Foreground",
|
| 146 |
-
tool="sketch",
|
| 147 |
type="numpy",
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
height=500,
|
|
|
|
|
|
|
| 151 |
)
|
| 152 |
-
unknown_mask = gr.
|
| 153 |
label="Unknown",
|
| 154 |
-
tool="sketch",
|
| 155 |
type="numpy",
|
| 156 |
-
|
| 157 |
-
|
|
|
|
| 158 |
height=500,
|
|
|
|
|
|
|
| 159 |
)
|
| 160 |
-
|
| 161 |
-
apply_background_replacement = gr.Checkbox(label="
|
| 162 |
-
background_image = gr.Image(label="Background image", type="pil",
|
| 163 |
run_button = gr.Button("Run")
|
| 164 |
with gr.Column():
|
| 165 |
-
with gr.
|
| 166 |
-
out_alpha = gr.Image(label="Alpha"
|
| 167 |
-
out_foreground = gr.Image(label="Foreground"
|
| 168 |
-
out_background_replacement = gr.Image(label="Background replacement",
|
| 169 |
|
| 170 |
inputs = [
|
| 171 |
image,
|
|
@@ -186,34 +210,30 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 186 |
inputs=inputs,
|
| 187 |
outputs=outputs,
|
| 188 |
fn=run,
|
| 189 |
-
cache_examples=
|
| 190 |
)
|
| 191 |
|
| 192 |
-
image.
|
| 193 |
-
fn=
|
| 194 |
inputs=image,
|
| 195 |
-
|
| 196 |
api_name=False,
|
| 197 |
-
)
|
| 198 |
-
load_image_button.click(
|
| 199 |
fn=lambda image: (image, image),
|
| 200 |
inputs=image,
|
| 201 |
outputs=[foreground_mask, unknown_mask],
|
| 202 |
-
queue=False,
|
| 203 |
api_name=False,
|
| 204 |
)
|
| 205 |
-
|
| 206 |
fn=update_trimap,
|
| 207 |
inputs=[foreground_mask, unknown_mask],
|
| 208 |
outputs=trimap,
|
| 209 |
-
queue=False,
|
| 210 |
api_name=False,
|
| 211 |
)
|
| 212 |
apply_background_replacement.change(
|
| 213 |
fn=lambda checked: (gr.Image(visible=checked), gr.Image(visible=checked)),
|
| 214 |
inputs=apply_background_replacement,
|
| 215 |
outputs=[background_image, out_background_replacement],
|
| 216 |
-
queue=False,
|
| 217 |
api_name=False,
|
| 218 |
)
|
| 219 |
|
|
@@ -221,8 +241,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 221 |
fn=run,
|
| 222 |
inputs=inputs,
|
| 223 |
outputs=outputs,
|
| 224 |
-
api_name="run",
|
| 225 |
)
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
-
demo.
|
|
|
|
| 12 |
DESCRIPTION = """\
|
| 13 |
# [ViTMatte](https://github.com/hustvl/ViTMatte)
|
| 14 |
|
| 15 |
+
This is a demo of [ViTMatte](https://github.com/hustvl/ViTMatte), an image matting method that uses Vision Transformers (ViT) to accurately extract the foreground from an image.
|
| 16 |
+
It predicts a soft alpha matte to help separate the subject from the background — even tricky areas like hair and fur!
|
| 17 |
+
|
| 18 |
+
You've got two ways to get started:
|
| 19 |
+
|
| 20 |
+
### 🖼️ Option 1: Upload Image & Trimap
|
| 21 |
+
- Upload your original image.
|
| 22 |
+
- Upload a **trimap**: a helper image that labels regions as **foreground (white)**, **background (black)**, and **unknown (gray)**.
|
| 23 |
+
- The trimap must be a **grayscale image** containing only three pixel values:
|
| 24 |
+
- `0` for **background**
|
| 25 |
+
- `128` for **unknown**
|
| 26 |
+
- `255` for **foreground**
|
| 27 |
+
- The model will use this trimap to generate the alpha matte and extract the foreground.
|
| 28 |
+
|
| 29 |
+
### ✏️ Option 2: Draw Your Own Trimap
|
| 30 |
+
- Upload just your image.
|
| 31 |
+
- Go to the **"Draw Trimap"** tab to start drawing masks.
|
| 32 |
+
- Use the tools to mark:
|
| 33 |
+
- **Foreground** (e.g. the subject),
|
| 34 |
+
- **Unknown** (areas where the boundary is unclear).
|
| 35 |
+
- Once you're done, click the **"Generate Trimap"** button to generate the trimap from your drawing.
|
| 36 |
+
|
| 37 |
+
### ✨ Optional: Replace Background
|
| 38 |
+
Want to swap the background? Just check the **"Replace Background"** option and choose a new background image.
|
| 39 |
+
The app will blend your extracted subject with the new background seamlessly!
|
| 40 |
|
|
|
|
| 41 |
|
|
|
|
| 42 |
"""
|
| 43 |
|
| 44 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 50 |
model = VitMatteForImageMatting.from_pretrained(MODEL_ID).to(device)
|
| 51 |
|
| 52 |
|
| 53 |
+
def resize_input_image(image: PIL.Image.Image | None) -> PIL.Image.Image:
|
| 54 |
+
if image is None:
|
| 55 |
+
return None
|
| 56 |
if max(image.size) > MAX_IMAGE_SIZE:
|
| 57 |
+
w, h = image.size
|
| 58 |
+
scale = MAX_IMAGE_SIZE / max(w, h)
|
| 59 |
+
new_w = int(w * scale)
|
| 60 |
+
new_h = int(h * scale)
|
| 61 |
+
gr.Info(
|
| 62 |
+
f"The uploaded image exceeded the maximum resolution limit of {MAX_IMAGE_SIZE}px. It has been resized to {new_w}x{new_h}."
|
| 63 |
+
)
|
| 64 |
+
return image.resize((new_w, new_h))
|
| 65 |
+
return image
|
| 66 |
|
| 67 |
|
| 68 |
def binarize_mask(mask: np.ndarray) -> np.ndarray:
|
|
|
|
| 69 |
mask[mask > 0] = 1
|
| 70 |
return mask
|
| 71 |
|
| 72 |
|
| 73 |
+
def update_trimap(foreground_mask_editor: gr.ImageEditor, unknown_mask_editor: gr.ImageEditor) -> np.ndarray:
|
| 74 |
+
foreground = foreground_mask_editor["layers"][0]
|
| 75 |
foreground = binarize_mask(foreground)
|
| 76 |
|
| 77 |
+
unknown = unknown_mask_editor["layers"][0]
|
| 78 |
unknown = binarize_mask(unknown)
|
| 79 |
|
| 80 |
trimap = np.zeros_like(foreground)
|
|
|
|
| 95 |
top = (new_bg_h - target_h) // 2
|
| 96 |
right = left + target_w
|
| 97 |
bottom = top + target_h
|
| 98 |
+
return background_image.crop((left, top, right, bottom))
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
def replace_background(
|
|
|
|
| 113 |
image = np.array(image).astype(float) / 255
|
| 114 |
background_image = np.array(background_image).astype(float) / 255
|
| 115 |
result = image * alpha[:, :, None] + background_image * (1 - alpha[:, :, None])
|
| 116 |
+
return (result * 255).astype(np.uint8)
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
@spaces.GPU
|
|
|
|
| 127 |
if image.size != trimap.size:
|
| 128 |
raise gr.Error("Image and trimap must have the same size.")
|
| 129 |
if max(image.size) > MAX_IMAGE_SIZE:
|
| 130 |
+
error_message = f"Image size is too large. Max image size is {MAX_IMAGE_SIZE} pixels."
|
| 131 |
+
raise gr.Error(error_message)
|
| 132 |
if image.mode != "RGB":
|
| 133 |
raise gr.Error("Image must be RGB.")
|
| 134 |
if trimap.mode != "L":
|
|
|
|
| 145 |
foreground = (foreground * 255).astype(np.uint8)
|
| 146 |
foreground = PIL.Image.fromarray(foreground)
|
| 147 |
|
| 148 |
+
res_bg_replacement = replace_background(image, alpha, background_image) if apply_background_replacement else None
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
return alpha, foreground, res_bg_replacement
|
| 151 |
|
| 152 |
|
| 153 |
+
with gr.Blocks(css_paths="style.css") as demo:
|
| 154 |
gr.Markdown(DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column():
|
| 158 |
+
with gr.Group():
|
| 159 |
+
image = gr.Image(label="Input image", type="pil")
|
| 160 |
with gr.Tabs():
|
| 161 |
with gr.Tab(label="Trimap"):
|
| 162 |
+
trimap = gr.Image(label="Trimap", type="pil", image_mode="L")
|
| 163 |
with gr.Tab(label="Draw trimap"):
|
| 164 |
+
foreground_mask = gr.ImageEditor(
|
|
|
|
| 165 |
label="Foreground",
|
|
|
|
| 166 |
type="numpy",
|
| 167 |
+
sources=("upload",),
|
| 168 |
+
transforms=(),
|
| 169 |
+
image_mode="L",
|
| 170 |
height=500,
|
| 171 |
+
brush=gr.Brush(default_color=("#00ff00", 0.6)),
|
| 172 |
+
layers=gr.LayerOptions(allow_additional_layers=False, layers=["Foreground mask"]),
|
| 173 |
)
|
| 174 |
+
unknown_mask = gr.ImageEditor(
|
| 175 |
label="Unknown",
|
|
|
|
| 176 |
type="numpy",
|
| 177 |
+
sources=("upload",),
|
| 178 |
+
transforms=(),
|
| 179 |
+
image_mode="L",
|
| 180 |
height=500,
|
| 181 |
+
brush=gr.Brush(default_color=("#00ff00", 0.6)),
|
| 182 |
+
layers=gr.LayerOptions(allow_additional_layers=False, layers=["Unknown mask"]),
|
| 183 |
)
|
| 184 |
+
generate_trimap_button = gr.Button("Generate trimap")
|
| 185 |
+
apply_background_replacement = gr.Checkbox(label="Replace background", value=False)
|
| 186 |
+
background_image = gr.Image(label="Background image", type="pil", visible=False)
|
| 187 |
run_button = gr.Button("Run")
|
| 188 |
with gr.Column():
|
| 189 |
+
with gr.Group():
|
| 190 |
+
out_alpha = gr.Image(label="Alpha")
|
| 191 |
+
out_foreground = gr.Image(label="Foreground")
|
| 192 |
+
out_background_replacement = gr.Image(label="Background replacement", visible=False)
|
| 193 |
|
| 194 |
inputs = [
|
| 195 |
image,
|
|
|
|
| 210 |
inputs=inputs,
|
| 211 |
outputs=outputs,
|
| 212 |
fn=run,
|
| 213 |
+
cache_examples=False,
|
| 214 |
)
|
| 215 |
|
| 216 |
+
image.input(
|
| 217 |
+
fn=resize_input_image,
|
| 218 |
inputs=image,
|
| 219 |
+
outputs=image,
|
| 220 |
api_name=False,
|
| 221 |
+
).then(
|
|
|
|
| 222 |
fn=lambda image: (image, image),
|
| 223 |
inputs=image,
|
| 224 |
outputs=[foreground_mask, unknown_mask],
|
|
|
|
| 225 |
api_name=False,
|
| 226 |
)
|
| 227 |
+
generate_trimap_button.click(
|
| 228 |
fn=update_trimap,
|
| 229 |
inputs=[foreground_mask, unknown_mask],
|
| 230 |
outputs=trimap,
|
|
|
|
| 231 |
api_name=False,
|
| 232 |
)
|
| 233 |
apply_background_replacement.change(
|
| 234 |
fn=lambda checked: (gr.Image(visible=checked), gr.Image(visible=checked)),
|
| 235 |
inputs=apply_background_replacement,
|
| 236 |
outputs=[background_image, out_background_replacement],
|
|
|
|
| 237 |
api_name=False,
|
| 238 |
)
|
| 239 |
|
|
|
|
| 241 |
fn=run,
|
| 242 |
inputs=inputs,
|
| 243 |
outputs=outputs,
|
|
|
|
| 244 |
)
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
+
demo.launch()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "vitmatte"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.10"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"gradio>=5.24.0",
|
| 9 |
+
"hf-transfer>=0.1.9",
|
| 10 |
+
"spaces>=0.34.2",
|
| 11 |
+
"torch==2.4.0",
|
| 12 |
+
"transformers>=4.51.1",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
[tool.ruff]
|
| 16 |
+
line-length = 119
|
| 17 |
+
|
| 18 |
+
[tool.ruff.lint]
|
| 19 |
+
select = ["ALL"]
|
| 20 |
+
ignore = [
|
| 21 |
+
"COM812", # missing-trailing-comma
|
| 22 |
+
"D203", # one-blank-line-before-class
|
| 23 |
+
"D213", # multi-line-summary-second-line
|
| 24 |
+
"E501", # line-too-long
|
| 25 |
+
"SIM117", # multiple-with-statements
|
| 26 |
+
#
|
| 27 |
+
"D100", # undocumented-public-module
|
| 28 |
+
"D101", # undocumented-public-class
|
| 29 |
+
"D102", # undocumented-public-method
|
| 30 |
+
"D103", # undocumented-public-function
|
| 31 |
+
"D104", # undocumented-public-package
|
| 32 |
+
"D105", # undocumented-magic-method
|
| 33 |
+
"D107", # undocumented-public-init
|
| 34 |
+
"EM101", # raw-string-in-exception
|
| 35 |
+
"FBT001", # boolean-type-hint-positional-argument
|
| 36 |
+
"FBT002", # boolean-default-value-positional-argument
|
| 37 |
+
"PD901", # pandas-df-variable-name
|
| 38 |
+
"PGH003", # blanket-type-ignore
|
| 39 |
+
"PLR0913", # too-many-arguments
|
| 40 |
+
"PLR0915", # too-many-statements
|
| 41 |
+
"TRY003", # raise-vanilla-args
|
| 42 |
+
]
|
| 43 |
+
unfixable = [
|
| 44 |
+
"F401", # unused-import
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
[tool.ruff.lint.pydocstyle]
|
| 48 |
+
convention = "google"
|
| 49 |
+
|
| 50 |
+
[tool.ruff.lint.per-file-ignores]
|
| 51 |
+
"*.ipynb" = ["T201", "T203"]
|
| 52 |
+
|
| 53 |
+
[tool.ruff.format]
|
| 54 |
+
docstring-code-format = true
|
requirements.txt
CHANGED
|
@@ -1,6 +1,233 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file was autogenerated by uv via the following command:
|
| 2 |
+
# uv pip compile pyproject.toml -o requirements.txt
|
| 3 |
+
aiofiles==24.1.0
|
| 4 |
+
# via gradio
|
| 5 |
+
annotated-types==0.7.0
|
| 6 |
+
# via pydantic
|
| 7 |
+
anyio==4.9.0
|
| 8 |
+
# via
|
| 9 |
+
# gradio
|
| 10 |
+
# httpx
|
| 11 |
+
# starlette
|
| 12 |
+
certifi==2025.1.31
|
| 13 |
+
# via
|
| 14 |
+
# httpcore
|
| 15 |
+
# httpx
|
| 16 |
+
# requests
|
| 17 |
+
charset-normalizer==3.4.1
|
| 18 |
+
# via requests
|
| 19 |
+
click==8.1.8
|
| 20 |
+
# via
|
| 21 |
+
# typer
|
| 22 |
+
# uvicorn
|
| 23 |
+
exceptiongroup==1.2.2
|
| 24 |
+
# via anyio
|
| 25 |
+
fastapi==0.115.12
|
| 26 |
+
# via gradio
|
| 27 |
+
ffmpy==0.5.0
|
| 28 |
+
# via gradio
|
| 29 |
+
filelock==3.18.0
|
| 30 |
+
# via
|
| 31 |
+
# huggingface-hub
|
| 32 |
+
# torch
|
| 33 |
+
# transformers
|
| 34 |
+
# triton
|
| 35 |
+
fsspec==2025.3.2
|
| 36 |
+
# via
|
| 37 |
+
# gradio-client
|
| 38 |
+
# huggingface-hub
|
| 39 |
+
# torch
|
| 40 |
+
gradio==5.24.0
|
| 41 |
+
# via
|
| 42 |
+
# vitmatte (pyproject.toml)
|
| 43 |
+
# spaces
|
| 44 |
+
gradio-client==1.8.0
|
| 45 |
+
# via gradio
|
| 46 |
+
groovy==0.1.2
|
| 47 |
+
# via gradio
|
| 48 |
+
h11==0.14.0
|
| 49 |
+
# via
|
| 50 |
+
# httpcore
|
| 51 |
+
# uvicorn
|
| 52 |
+
hf-transfer==0.1.9
|
| 53 |
+
# via vitmatte (pyproject.toml)
|
| 54 |
+
httpcore==1.0.7
|
| 55 |
+
# via httpx
|
| 56 |
+
httpx==0.28.1
|
| 57 |
+
# via
|
| 58 |
+
# gradio
|
| 59 |
+
# gradio-client
|
| 60 |
+
# safehttpx
|
| 61 |
+
# spaces
|
| 62 |
+
huggingface-hub==0.30.2
|
| 63 |
+
# via
|
| 64 |
+
# gradio
|
| 65 |
+
# gradio-client
|
| 66 |
+
# tokenizers
|
| 67 |
+
# transformers
|
| 68 |
+
idna==3.10
|
| 69 |
+
# via
|
| 70 |
+
# anyio
|
| 71 |
+
# httpx
|
| 72 |
+
# requests
|
| 73 |
+
jinja2==3.1.6
|
| 74 |
+
# via
|
| 75 |
+
# gradio
|
| 76 |
+
# torch
|
| 77 |
+
markdown-it-py==3.0.0
|
| 78 |
+
# via rich
|
| 79 |
+
markupsafe==3.0.2
|
| 80 |
+
# via
|
| 81 |
+
# gradio
|
| 82 |
+
# jinja2
|
| 83 |
+
mdurl==0.1.2
|
| 84 |
+
# via markdown-it-py
|
| 85 |
+
mpmath==1.3.0
|
| 86 |
+
# via sympy
|
| 87 |
+
networkx==3.4.2
|
| 88 |
+
# via torch
|
| 89 |
+
numpy==2.2.4
|
| 90 |
+
# via
|
| 91 |
+
# gradio
|
| 92 |
+
# pandas
|
| 93 |
+
# transformers
|
| 94 |
+
nvidia-cublas-cu12==12.1.3.1
|
| 95 |
+
# via
|
| 96 |
+
# nvidia-cudnn-cu12
|
| 97 |
+
# nvidia-cusolver-cu12
|
| 98 |
+
# torch
|
| 99 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
| 100 |
+
# via torch
|
| 101 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
| 102 |
+
# via torch
|
| 103 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
| 104 |
+
# via torch
|
| 105 |
+
nvidia-cudnn-cu12==9.1.0.70
|
| 106 |
+
# via torch
|
| 107 |
+
nvidia-cufft-cu12==11.0.2.54
|
| 108 |
+
# via torch
|
| 109 |
+
nvidia-curand-cu12==10.3.2.106
|
| 110 |
+
# via torch
|
| 111 |
+
nvidia-cusolver-cu12==11.4.5.107
|
| 112 |
+
# via torch
|
| 113 |
+
nvidia-cusparse-cu12==12.1.0.106
|
| 114 |
+
# via
|
| 115 |
+
# nvidia-cusolver-cu12
|
| 116 |
+
# torch
|
| 117 |
+
nvidia-nccl-cu12==2.20.5
|
| 118 |
+
# via torch
|
| 119 |
+
nvidia-nvjitlink-cu12==12.8.93
|
| 120 |
+
# via
|
| 121 |
+
# nvidia-cusolver-cu12
|
| 122 |
+
# nvidia-cusparse-cu12
|
| 123 |
+
nvidia-nvtx-cu12==12.1.105
|
| 124 |
+
# via torch
|
| 125 |
+
orjson==3.10.16
|
| 126 |
+
# via gradio
|
| 127 |
+
packaging==24.2
|
| 128 |
+
# via
|
| 129 |
+
# gradio
|
| 130 |
+
# gradio-client
|
| 131 |
+
# huggingface-hub
|
| 132 |
+
# spaces
|
| 133 |
+
# transformers
|
| 134 |
+
pandas==2.2.3
|
| 135 |
+
# via gradio
|
| 136 |
+
pillow==11.1.0
|
| 137 |
+
# via gradio
|
| 138 |
+
psutil==5.9.8
|
| 139 |
+
# via spaces
|
| 140 |
+
pydantic==2.11.3
|
| 141 |
+
# via
|
| 142 |
+
# fastapi
|
| 143 |
+
# gradio
|
| 144 |
+
# spaces
|
| 145 |
+
pydantic-core==2.33.1
|
| 146 |
+
# via pydantic
|
| 147 |
+
pydub==0.25.1
|
| 148 |
+
# via gradio
|
| 149 |
+
pygments==2.19.1
|
| 150 |
+
# via rich
|
| 151 |
+
python-dateutil==2.9.0.post0
|
| 152 |
+
# via pandas
|
| 153 |
+
python-multipart==0.0.20
|
| 154 |
+
# via gradio
|
| 155 |
+
pytz==2025.2
|
| 156 |
+
# via pandas
|
| 157 |
+
pyyaml==6.0.2
|
| 158 |
+
# via
|
| 159 |
+
# gradio
|
| 160 |
+
# huggingface-hub
|
| 161 |
+
# transformers
|
| 162 |
+
regex==2024.11.6
|
| 163 |
+
# via transformers
|
| 164 |
+
requests==2.32.3
|
| 165 |
+
# via
|
| 166 |
+
# huggingface-hub
|
| 167 |
+
# spaces
|
| 168 |
+
# transformers
|
| 169 |
+
rich==14.0.0
|
| 170 |
+
# via typer
|
| 171 |
+
ruff==0.11.4
|
| 172 |
+
# via gradio
|
| 173 |
+
safehttpx==0.1.6
|
| 174 |
+
# via gradio
|
| 175 |
+
safetensors==0.5.3
|
| 176 |
+
# via transformers
|
| 177 |
+
semantic-version==2.10.0
|
| 178 |
+
# via gradio
|
| 179 |
+
shellingham==1.5.4
|
| 180 |
+
# via typer
|
| 181 |
+
six==1.17.0
|
| 182 |
+
# via python-dateutil
|
| 183 |
+
sniffio==1.3.1
|
| 184 |
+
# via anyio
|
| 185 |
+
spaces==0.34.2
|
| 186 |
+
# via vitmatte (pyproject.toml)
|
| 187 |
+
starlette==0.46.1
|
| 188 |
+
# via
|
| 189 |
+
# fastapi
|
| 190 |
+
# gradio
|
| 191 |
+
sympy==1.13.3
|
| 192 |
+
# via torch
|
| 193 |
+
tokenizers==0.21.1
|
| 194 |
+
# via transformers
|
| 195 |
+
tomlkit==0.13.2
|
| 196 |
+
# via gradio
|
| 197 |
+
torch==2.4.0
|
| 198 |
+
# via vitmatte (pyproject.toml)
|
| 199 |
+
tqdm==4.67.1
|
| 200 |
+
# via
|
| 201 |
+
# huggingface-hub
|
| 202 |
+
# transformers
|
| 203 |
+
transformers==4.51.1
|
| 204 |
+
# via vitmatte (pyproject.toml)
|
| 205 |
+
triton==3.0.0
|
| 206 |
+
# via torch
|
| 207 |
+
typer==0.15.2
|
| 208 |
+
# via gradio
|
| 209 |
+
typing-extensions==4.13.1
|
| 210 |
+
# via
|
| 211 |
+
# anyio
|
| 212 |
+
# fastapi
|
| 213 |
+
# gradio
|
| 214 |
+
# gradio-client
|
| 215 |
+
# huggingface-hub
|
| 216 |
+
# pydantic
|
| 217 |
+
# pydantic-core
|
| 218 |
+
# rich
|
| 219 |
+
# spaces
|
| 220 |
+
# torch
|
| 221 |
+
# typer
|
| 222 |
+
# typing-inspection
|
| 223 |
+
# uvicorn
|
| 224 |
+
typing-inspection==0.4.0
|
| 225 |
+
# via pydantic
|
| 226 |
+
tzdata==2025.2
|
| 227 |
+
# via pandas
|
| 228 |
+
urllib3==2.3.0
|
| 229 |
+
# via requests
|
| 230 |
+
uvicorn==0.34.0
|
| 231 |
+
# via gradio
|
| 232 |
+
websockets==15.0.1
|
| 233 |
+
# via gradio-client
|
style.css
CHANGED
|
@@ -1,10 +1,4 @@
|
|
| 1 |
h1 {
|
| 2 |
text-align: center;
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
#duplicate-button {
|
| 6 |
-
margin: auto;
|
| 7 |
-
color: #fff;
|
| 8 |
-
background: #1565c0;
|
| 9 |
-
border-radius: 100vh;
|
| 10 |
}
|
|
|
|
| 1 |
h1 {
|
| 2 |
text-align: center;
|
| 3 |
+
display: block;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
}
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|