Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,25 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from fastapi.responses import Response
|
| 3 |
-
from retinaface import RetinaFace
|
| 4 |
from insightface.app import FaceAnalysis
|
| 5 |
from rembg import remove
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
import io
|
| 9 |
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 12 |
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 13 |
-
face_app.prepare(ctx_id=0)
|
|
|
|
| 14 |
|
| 15 |
def resize_to_4x6(img):
|
| 16 |
return img.resize((472, 709), Image.LANCZOS)
|
| 17 |
|
|
|
|
| 18 |
@app.post("/process")
|
| 19 |
-
async def
|
| 20 |
img_bytes = await file.read()
|
| 21 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 22 |
np_img = np.array(img)
|
|
@@ -24,18 +27,24 @@ async def process_image(file: UploadFile = File(...)):
|
|
| 24 |
faces = face_app.get(np_img)
|
| 25 |
if not faces:
|
| 26 |
return {"error": "No face detected"}
|
|
|
|
|
|
|
| 27 |
face = faces[0]
|
| 28 |
aligned = Image.fromarray(face.normed)
|
| 29 |
|
|
|
|
| 30 |
no_bg = remove(aligned)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
return Response(
|
|
|
|
| 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 |
+
# Load face detection model (SCRFD)
|
| 13 |
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 14 |
+
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 15 |
+
|
| 16 |
|
| 17 |
def resize_to_4x6(img):
|
| 18 |
return img.resize((472, 709), Image.LANCZOS)
|
| 19 |
|
| 20 |
+
|
| 21 |
@app.post("/process")
|
| 22 |
+
async def process_img(file: UploadFile = File(...)):
|
| 23 |
img_bytes = await file.read()
|
| 24 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 25 |
np_img = np.array(img)
|
|
|
|
| 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 |
|
| 35 |
+
# Remove background
|
| 36 |
no_bg = remove(aligned)
|
| 37 |
|
| 38 |
+
# Add white background
|
| 39 |
+
white = Image.new("RGB", no_bg.size, (255, 255, 255))
|
| 40 |
+
white.paste(no_bg, mask=no_bg.split()[3])
|
| 41 |
|
| 42 |
+
# Resize to official 4x6 photo size
|
| 43 |
+
final_img = resize_to_4x6(white)
|
| 44 |
|
| 45 |
+
# Return JPEG
|
| 46 |
+
buf = io.BytesIO()
|
| 47 |
+
final_img.save(buf, format="JPEG")
|
| 48 |
+
buf.seek(0)
|
| 49 |
|
| 50 |
+
return Response(buf.getvalue(), media_type="image/jpeg")
|