mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: use BiRefNet-Lite as default model and fix JSON parsing
- Switch default from birefnet-general (973MB, 4min) to birefnet-general-lite (faster, still SOTA quality) - Fix Python script stdout pollution — progress messages now go to stderr so the JSON result parser doesn't break - Pre-download birefnet-general-lite in Docker build
This commit is contained in:
@@ -34,7 +34,7 @@ export function RemoveBgSettings() {
|
|||||||
const { processFiles, processing, error, downloadUrl, originalSize, processedSize } =
|
const { processFiles, processing, error, downloadUrl, originalSize, processedSize } =
|
||||||
useToolProcessor("remove-background");
|
useToolProcessor("remove-background");
|
||||||
|
|
||||||
const [model, setModel] = useState<BgModel>("birefnet-general");
|
const [model, setModel] = useState<BgModel>("birefnet-general-lite");
|
||||||
const [bgColor, setBgColor] = useState("");
|
const [bgColor, setBgColor] = useState("");
|
||||||
const [elapsed, setElapsed] = useState(0);
|
const [elapsed, setElapsed] = useState(0);
|
||||||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||||
|
|||||||
+2
-2
@@ -71,8 +71,8 @@ RUN /opt/venv/bin/pip install --no-cache-dir --upgrade pip && \
|
|||||||
# This makes the Docker image fully self-contained — works offline
|
# This makes the Docker image fully self-contained — works offline
|
||||||
RUN /opt/venv/bin/python3 -c "\
|
RUN /opt/venv/bin/python3 -c "\
|
||||||
from rembg import new_session; \
|
from rembg import new_session; \
|
||||||
print('Downloading BiRefNet model (SOTA)...'); \
|
print('Downloading BiRefNet-Lite model (SOTA, fast)...'); \
|
||||||
new_session('birefnet-general'); \
|
new_session('birefnet-general-lite'); \
|
||||||
print('Downloading u2net model (fallback)...'); \
|
print('Downloading u2net model (fallback)...'); \
|
||||||
new_session('u2net'); \
|
new_session('u2net'); \
|
||||||
print('Background removal models ready') \
|
print('Background removal models ready') \
|
||||||
|
|||||||
@@ -8,25 +8,26 @@ def main():
|
|||||||
output_path = sys.argv[2]
|
output_path = sys.argv[2]
|
||||||
settings = json.loads(sys.argv[3]) if len(sys.argv) > 3 else {}
|
settings = json.loads(sys.argv[3]) if len(sys.argv) > 3 else {}
|
||||||
|
|
||||||
model = settings.get("model", "birefnet-general")
|
model = settings.get("model", "birefnet-general-lite")
|
||||||
bg_color = settings.get("backgroundColor", "")
|
bg_color = settings.get("backgroundColor", "")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from rembg import remove, new_session
|
from rembg import remove, new_session
|
||||||
from PIL import Image
|
|
||||||
import io
|
import io
|
||||||
|
|
||||||
print(json.dumps({"progress": "loading_model"}), flush=True)
|
# Progress messages go to stderr (stdout reserved for JSON result)
|
||||||
|
sys.stderr.write(f"Loading model: {model}\n")
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
# Create a session with the selected model
|
|
||||||
session = new_session(model)
|
session = new_session(model)
|
||||||
|
|
||||||
|
sys.stderr.write("Processing image...\n")
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
with open(input_path, "rb") as f:
|
with open(input_path, "rb") as f:
|
||||||
input_data = f.read()
|
input_data = f.read()
|
||||||
|
|
||||||
print(json.dumps({"progress": "processing"}), flush=True)
|
# Try with alpha matting for better edges, fall back without
|
||||||
|
|
||||||
# Try with alpha matting first for better edges
|
|
||||||
try:
|
try:
|
||||||
output_data = remove(
|
output_data = remove(
|
||||||
input_data,
|
input_data,
|
||||||
@@ -40,6 +41,8 @@ def main():
|
|||||||
|
|
||||||
# If a background color is specified, composite onto it
|
# If a background color is specified, composite onto it
|
||||||
if bg_color and bg_color.startswith("#"):
|
if bg_color and bg_color.startswith("#"):
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
img = Image.open(io.BytesIO(output_data)).convert("RGBA")
|
img = Image.open(io.BytesIO(output_data)).convert("RGBA")
|
||||||
hex_color = bg_color.lstrip("#")
|
hex_color = bg_color.lstrip("#")
|
||||||
r = int(hex_color[0:2], 16)
|
r = int(hex_color[0:2], 16)
|
||||||
|
|||||||
Reference in New Issue
Block a user