mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat(ai): add emit_progress() calls to all Python AI scripts
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
"""Background removal using rembg with state-of-the-art BiRefNet models."""
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
def emit_progress(percent, stage):
|
||||
"""Emit structured progress to stderr for bridge.ts to capture."""
|
||||
print(json.dumps({"progress": percent, "stage": stage}), file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
@@ -11,23 +17,26 @@ def main():
|
||||
model = settings.get("model", "u2net")
|
||||
bg_color = settings.get("backgroundColor", "")
|
||||
|
||||
# Redirect stdout to stderr so library download/progress output
|
||||
# cannot contaminate our JSON result on stdout.
|
||||
stdout_fd = os.dup(1)
|
||||
os.dup2(2, 1)
|
||||
|
||||
try:
|
||||
from rembg import remove, new_session
|
||||
import io
|
||||
|
||||
# Progress messages go to stderr (stdout reserved for JSON result)
|
||||
sys.stderr.write(f"Loading model: {model}\n")
|
||||
sys.stderr.flush()
|
||||
emit_progress(10, "Loading model")
|
||||
|
||||
session = new_session(model)
|
||||
|
||||
sys.stderr.write("Processing image...\n")
|
||||
sys.stderr.flush()
|
||||
emit_progress(25, "Model loaded")
|
||||
|
||||
with open(input_path, "rb") as f:
|
||||
input_data = f.read()
|
||||
|
||||
# Try with alpha matting for better edges, fall back without
|
||||
emit_progress(30, "Analyzing image")
|
||||
try:
|
||||
output_data = remove(
|
||||
input_data,
|
||||
@@ -39,8 +48,11 @@ def main():
|
||||
except Exception:
|
||||
output_data = remove(input_data, session=session)
|
||||
|
||||
emit_progress(80, "Background removed")
|
||||
|
||||
# If a background color is specified, composite onto it
|
||||
if bg_color and bg_color.startswith("#"):
|
||||
emit_progress(85, "Compositing background")
|
||||
from PIL import Image
|
||||
|
||||
img = Image.open(io.BytesIO(output_data)).convert("RGBA")
|
||||
@@ -54,22 +66,27 @@ def main():
|
||||
bg.save(buf, format="PNG")
|
||||
output_data = buf.getvalue()
|
||||
|
||||
emit_progress(95, "Saving result")
|
||||
with open(output_path, "wb") as f:
|
||||
f.write(output_data)
|
||||
|
||||
print(json.dumps({"success": True, "model": model}))
|
||||
result = json.dumps({"success": True, "model": model})
|
||||
|
||||
except ImportError:
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
"success": False,
|
||||
"error": "rembg is not installed. Install with: pip install rembg[cpu]",
|
||||
}
|
||||
)
|
||||
result = json.dumps(
|
||||
{
|
||||
"success": False,
|
||||
"error": "rembg is not installed. Install with: pip install rembg[cpu]",
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
print(json.dumps({"success": False, "error": str(e)}))
|
||||
result = json.dumps({"success": False, "error": str(e)})
|
||||
|
||||
# Restore original stdout and write only our JSON result
|
||||
os.dup2(stdout_fd, 1)
|
||||
os.close(stdout_fd)
|
||||
sys.stdout.write(result + "\n")
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user