feat: add Ultra quality mode with BiRefNet-matting for people photos

Adds a new "Ultra" quality tier for People subject type that uses
BiRefNet-matting (ONNX, 928MB) for true alpha matting instead of
binary segmentation. Produces per-pixel transparency for hair wisps
and fine edges that standard models miss.

- Custom rembg session class loads BiRefNet-matting ONNX from GitHub releases
- Zero new Python dependencies (reuses existing onnxruntime)
- Model pre-downloaded in Docker build alongside existing models
- Ultra option only visible when subject is People
- Falls back to Best when switching to Products/General
This commit is contained in:
Siddharth Kumar Sah
2026-04-12 18:09:59 +08:00
parent 6c58f12262
commit 93dd37017c
4 changed files with 119 additions and 8 deletions
+30
View File
@@ -27,6 +27,7 @@ REMBG_MODELS = [
"birefnet-general-lite",
"birefnet-portrait",
"birefnet-general",
"birefnet-matting",
]
# PaddleOCR language codes (not ISO). German/French/Spanish use "latin" model.
@@ -34,11 +35,40 @@ REMBG_MODELS = [
PADDLEOCR_LANGUAGES = ["en", "ch", "japan", "korean", "latin"]
def _register_birefnet_matting():
"""Register BiRefNet-matting ONNX session for Ultra quality mode."""
import os
import pooch
from rembg.sessions import sessions_class
from rembg.sessions.birefnet_general import BiRefNetSessionGeneral
class BiRefNetMattingSession(BiRefNetSessionGeneral):
@classmethod
def download_models(cls, *args, **kwargs):
fname = f"{cls.name(*args, **kwargs)}.onnx"
pooch.retrieve(
"https://github.com/ZhengPeng7/BiRefNet/releases/download/v1/BiRefNet-matting-epoch_100.onnx",
None, # Skip checksum for GitHub release assets
fname=fname,
path=cls.u2net_home(*args, **kwargs),
progressbar=True,
)
return os.path.join(cls.u2net_home(*args, **kwargs), fname)
@classmethod
def name(cls, *args, **kwargs):
return "birefnet-matting"
sessions_class.append(BiRefNetMattingSession)
def download_rembg_models():
"""Download all rembg ONNX models."""
print("=== Downloading rembg models ===")
from rembg import new_session
_register_birefnet_matting()
for model in REMBG_MODELS:
print(f" Downloading {model}...")
new_session(model)