Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,19 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
from fastapi.responses import Response
|
| 3 |
from insightface.app import FaceAnalysis
|
| 4 |
from rembg import remove
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
import io
|
| 8 |
|
| 9 |
-
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 14 |
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 15 |
|
|
@@ -26,9 +30,8 @@ async def process_img(file: UploadFile = File(...)):
|
|
| 26 |
|
| 27 |
faces = face_app.get(np_img)
|
| 28 |
if not faces:
|
| 29 |
-
return {"error": "No face detected"}
|
| 30 |
|
| 31 |
-
# Extract aligned face from insightface
|
| 32 |
face = faces[0]
|
| 33 |
aligned = Image.fromarray(face.normed)
|
| 34 |
|
|
@@ -39,10 +42,10 @@ async def process_img(file: UploadFile = File(...)):
|
|
| 39 |
white = Image.new("RGB", no_bg.size, (255, 255, 255))
|
| 40 |
white.paste(no_bg, mask=no_bg.split()[3])
|
| 41 |
|
| 42 |
-
# Resize
|
| 43 |
final_img = resize_to_4x6(white)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
buf = io.BytesIO()
|
| 47 |
final_img.save(buf, format="JPEG")
|
| 48 |
buf.seek(0)
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import Response, JSONResponse
|
| 3 |
from insightface.app import FaceAnalysis
|
| 4 |
from rembg import remove
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
import io
|
| 8 |
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Simple homepage (fixes 404 on HF Spaces)
|
| 12 |
+
@app.get("/")
|
| 13 |
+
def home():
|
| 14 |
+
return {"message": "ID Photo API is running. Use POST /process"}
|
| 15 |
+
|
| 16 |
+
# Load face detection model
|
| 17 |
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 18 |
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 19 |
|
|
|
|
| 30 |
|
| 31 |
faces = face_app.get(np_img)
|
| 32 |
if not faces:
|
| 33 |
+
return JSONResponse({"error": "No face detected"}, status_code=400)
|
| 34 |
|
|
|
|
| 35 |
face = faces[0]
|
| 36 |
aligned = Image.fromarray(face.normed)
|
| 37 |
|
|
|
|
| 42 |
white = Image.new("RGB", no_bg.size, (255, 255, 255))
|
| 43 |
white.paste(no_bg, mask=no_bg.split()[3])
|
| 44 |
|
| 45 |
+
# Resize
|
| 46 |
final_img = resize_to_4x6(white)
|
| 47 |
|
| 48 |
+
# Output
|
| 49 |
buf = io.BytesIO()
|
| 50 |
final_img.save(buf, format="JPEG")
|
| 51 |
buf.seek(0)
|