Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
from io import BytesIO
|
| 3 |
-
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from transformers import AutoModelForImageSegmentation
|
|
@@ -52,6 +52,9 @@ def process_image(image: Image.Image) -> Image.Image:
|
|
| 52 |
# -------------------------
|
| 53 |
app = FastAPI(title="Background Removal API")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
@app.post("/remove-background")
|
| 56 |
async def remove_background(file: UploadFile = File(None), image_url: str = Form(None)):
|
| 57 |
"""
|
|
@@ -60,17 +63,34 @@ async def remove_background(file: UploadFile = File(None), image_url: str = Form
|
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
if file:
|
| 63 |
-
|
|
|
|
| 64 |
elif image_url:
|
| 65 |
-
resp = requests.get(image_url)
|
|
|
|
| 66 |
img = Image.open(BytesIO(resp.content)).convert("RGB")
|
| 67 |
else:
|
| 68 |
raise HTTPException(status_code=400, detail="Provide file or image_url")
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
buf = BytesIO()
|
| 72 |
-
|
| 73 |
buf.seek(0)
|
| 74 |
return StreamingResponse(buf, media_type="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from io import BytesIO
|
| 3 |
+
from PIL import Image, UnidentifiedImageError
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from transformers import AutoModelForImageSegmentation
|
|
|
|
| 52 |
# -------------------------
|
| 53 |
app = FastAPI(title="Background Removal API")
|
| 54 |
|
| 55 |
+
# -------------------------
|
| 56 |
+
# API Endpoint: Return PNG
|
| 57 |
+
# -------------------------
|
| 58 |
@app.post("/remove-background")
|
| 59 |
async def remove_background(file: UploadFile = File(None), image_url: str = Form(None)):
|
| 60 |
"""
|
|
|
|
| 63 |
"""
|
| 64 |
try:
|
| 65 |
if file:
|
| 66 |
+
img_bytes = await file.read()
|
| 67 |
+
img = Image.open(BytesIO(img_bytes)).convert("RGB")
|
| 68 |
elif image_url:
|
| 69 |
+
resp = requests.get(image_url, timeout=10)
|
| 70 |
+
resp.raise_for_status()
|
| 71 |
img = Image.open(BytesIO(resp.content)).convert("RGB")
|
| 72 |
else:
|
| 73 |
raise HTTPException(status_code=400, detail="Provide file or image_url")
|
| 74 |
|
| 75 |
+
# Process background removal
|
| 76 |
+
result_img = process_image(img)
|
| 77 |
+
|
| 78 |
+
# Convert to PNG bytes
|
| 79 |
buf = BytesIO()
|
| 80 |
+
result_img.save(buf, format="PNG")
|
| 81 |
buf.seek(0)
|
| 82 |
return StreamingResponse(buf, media_type="image/png")
|
| 83 |
+
|
| 84 |
+
except UnidentifiedImageError:
|
| 85 |
+
raise HTTPException(status_code=400, detail="Invalid image format")
|
| 86 |
+
except requests.RequestException:
|
| 87 |
+
raise HTTPException(status_code=400, detail="Failed to fetch image from URL")
|
| 88 |
except Exception as e:
|
| 89 |
raise HTTPException(status_code=500, detail=str(e))
|
| 90 |
+
|
| 91 |
+
# -------------------------
|
| 92 |
+
# Optional Root Info
|
| 93 |
+
# -------------------------
|
| 94 |
+
@app.get("/")
|
| 95 |
+
async def index():
|
| 96 |
+
return {"message": "POST /remove-background with 'file' or 'image_url'. Returns PNG."}
|