Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,12 +27,6 @@ async def process_img(file: UploadFile = File(...)):
|
|
| 27 |
try:
|
| 28 |
img_bytes = await file.read()
|
| 29 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 30 |
-
|
| 31 |
-
# Resize large images
|
| 32 |
-
max_dim = 1500
|
| 33 |
-
if max(img.size) > max_dim:
|
| 34 |
-
img.thumbnail((max_dim, max_dim))
|
| 35 |
-
|
| 36 |
np_img = np.array(img)
|
| 37 |
|
| 38 |
faces = face_app.get(np_img)
|
|
@@ -41,29 +35,71 @@ async def process_img(file: UploadFile = File(...)):
|
|
| 41 |
|
| 42 |
face = faces[0]
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
buf = io.BytesIO()
|
| 62 |
final_img.save(buf, format="JPEG")
|
| 63 |
buf.seek(0)
|
| 64 |
-
|
| 65 |
return Response(buf.getvalue(), media_type="image/jpeg")
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
-
print("
|
| 69 |
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
|
| 27 |
try:
|
| 28 |
img_bytes = await file.read()
|
| 29 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
np_img = np.array(img)
|
| 31 |
|
| 32 |
faces = face_app.get(np_img)
|
|
|
|
| 35 |
|
| 36 |
face = faces[0]
|
| 37 |
|
| 38 |
+
# =============================
|
| 39 |
+
# 1. Extract main landmarks
|
| 40 |
+
# =============================
|
| 41 |
+
pts = face.landmark_2d_106
|
| 42 |
+
|
| 43 |
+
left_eye = pts[38]
|
| 44 |
+
right_eye = pts[88]
|
| 45 |
+
nose = pts[52]
|
| 46 |
+
|
| 47 |
+
# =============================
|
| 48 |
+
# 2. Compute roll angle
|
| 49 |
+
# =============================
|
| 50 |
+
dx = right_eye[0] - left_eye[0]
|
| 51 |
+
dy = right_eye[1] - left_eye[1]
|
| 52 |
+
roll_angle = np.degrees(np.arctan2(dy, dx))
|
| 53 |
+
|
| 54 |
+
# =============================
|
| 55 |
+
# 3. Rotate image to fix roll
|
| 56 |
+
# =============================
|
| 57 |
+
rotated = img.rotate(-roll_angle, expand=True)
|
| 58 |
+
np_rot = np.array(rotated)
|
| 59 |
+
|
| 60 |
+
# =============================
|
| 61 |
+
# 4. Detect again after rotation
|
| 62 |
+
# =============================
|
| 63 |
+
faces2 = face_app.get(np_rot)
|
| 64 |
+
if not faces2:
|
| 65 |
+
return JSONResponse({"error": "Face lost after rotation"}, status_code=500)
|
| 66 |
+
|
| 67 |
+
f = faces2[0]
|
| 68 |
+
x1, y1, x2, y2 = f.bbox.astype(int)
|
| 69 |
+
|
| 70 |
+
# =============================
|
| 71 |
+
# 5. Resize face to Abshir ratio
|
| 72 |
+
# Face height should be 70% of image
|
| 73 |
+
# =============================
|
| 74 |
+
face_h = y2 - y1
|
| 75 |
+
target_face_ratio = 0.70
|
| 76 |
+
target_h = int(face_h / target_face_ratio)
|
| 77 |
+
|
| 78 |
+
center_y = (y1 + y2) // 2
|
| 79 |
+
top = center_y - target_h // 2
|
| 80 |
+
bottom = center_y + target_h // 2
|
| 81 |
+
|
| 82 |
+
top = max(0, top)
|
| 83 |
+
bottom = min(rotated.height, bottom)
|
| 84 |
+
|
| 85 |
+
crop_img = rotated.crop((0, top, rotated.width, bottom))
|
| 86 |
+
|
| 87 |
+
# =============================
|
| 88 |
+
# 6. Apply white background
|
| 89 |
+
# =============================
|
| 90 |
+
white_bg = Image.new("RGB", crop_img.size, (255, 255, 255))
|
| 91 |
+
white_bg.paste(crop_img, (0, 0))
|
| 92 |
+
|
| 93 |
+
# =============================
|
| 94 |
+
# 7. Final export 4x6
|
| 95 |
+
# =============================
|
| 96 |
+
final_img = white_bg.resize((472, 709), Image.LANCZOS)
|
| 97 |
|
| 98 |
buf = io.BytesIO()
|
| 99 |
final_img.save(buf, format="JPEG")
|
| 100 |
buf.seek(0)
|
|
|
|
| 101 |
return Response(buf.getvalue(), media_type="image/jpeg")
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
+
print("ERROR:", e)
|
| 105 |
return JSONResponse({"error": str(e)}, status_code=500)
|