feat: Add BEN2 background removal functionality and integrate into SnapForm

- Introduced BEN2 model for background removal in the panel and properties.
- Updated runtime to handle background removal commands and model downloads.
- Implemented background removal logic in worker, including image processing and saving results.
- Enhanced tests to cover new background removal features and ensure proper integration.
- Updated version to 0.9.28 to reflect new features and improvements.
This commit is contained in:
2026-08-03 20:18:00 +08:00
parent 69eaefa016
commit d694980da7
19 changed files with 1523 additions and 84 deletions

View File

@@ -8,11 +8,12 @@ from pathlib import Path
import bpy
from .models import get_model, selected_model_key
from .models import BEN2_MODEL, get_downloadable_model, selected_model_key
DA3_COMMIT = "3d835ec1a5802d64a8b8b15f817a1ab54809bfe4"
RUNTIME_SCHEMA = 2
BEN2_COMMIT = "2c99a5da477b5523585bfa5c893888a6e818a8f6"
RUNTIME_SCHEMA = 3
DEFAULT_STORAGE_ROOT = Path.home() / ".snapform"
@@ -127,14 +128,16 @@ def install_runtime_command(status_path):
backend.lower(),
"--da3-commit",
DA3_COMMIT,
"--ben2-commit",
BEN2_COMMIT,
"--status",
str(status_path),
]
def download_model_command(model_key, status_path):
model = get_model(model_key)
return [
model = get_downloadable_model(model_key)
command = [
str(runtime_python()),
str(Path(__file__).with_name("worker.py")),
"download",
@@ -145,6 +148,33 @@ def download_model_command(model_key, status_path):
"--status",
str(status_path),
]
for pattern in model.download_patterns:
command.extend(["--allow-pattern", pattern])
return command
def remove_background_command(
settings,
input_path,
status_path,
result_directory,
model_key,
):
return [
str(runtime_python()),
str(Path(__file__).with_name("worker.py")),
"remove-background",
"--input",
str(input_path),
"--model-dir",
str(model_directory(model_key)),
"--output-dir",
str(result_directory),
"--status",
str(status_path),
"--device",
settings.device.lower(),
]
def reconstruct_command(
@@ -157,6 +187,7 @@ def reconstruct_command(
process_res=None,
process_res_method=None,
use_ray_pose=None,
remove_background=False,
):
snapform_selected_key = model_key or selected_model_key(settings, export_format)
snapform_selected_process_res = (
@@ -170,7 +201,7 @@ def reconstruct_command(
snapform_selected_ray_pose = (
settings.use_ray_pose if use_ray_pose is None else use_ray_pose
)
return [
command = [
str(runtime_python()),
str(Path(__file__).with_name("worker.py")),
"reconstruct",
@@ -209,6 +240,14 @@ def reconstruct_command(
"--ref-view-strategy",
settings.ref_view_strategy,
]
if remove_background:
command.extend(
[
"--remove-background-model-dir",
str(model_directory(BEN2_MODEL.key)),
]
)
return command
def creation_flags():