Spaces:
Running
Running
updated app.py
Browse files
app.py
CHANGED
@@ -39,15 +39,24 @@ def detect_faces_roboflow(image_path):
|
|
39 |
return CLIENT_FACE.infer(image_path, model_id="face-detector-v4liw/2").get("predictions", [])
|
40 |
|
41 |
def detect_eyes_roboflow(image_path, raw_image):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def get_largest_iris_prediction(eye_crop):
|
53 |
"Calls Roboflow to find the largest iris using a temporary file for reliability."
|
@@ -96,9 +105,9 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
96 |
if not detect_faces_roboflow(temp_image_path):
|
97 |
return JSONResponse(status_code=400, content={"error": "No face detected."})
|
98 |
|
99 |
-
eye_crops = detect_eyes_roboflow(temp_image_path, raw_image)
|
100 |
-
if len(eye_crops) != 2:
|
101 |
-
|
102 |
|
103 |
eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
|
104 |
|
|
|
39 |
return CLIENT_FACE.infer(image_path, model_id="face-detector-v4liw/2").get("predictions", [])
|
40 |
|
41 |
def detect_eyes_roboflow(image_path, raw_image):
|
42 |
+
"""Calls Roboflow to find eyes and returns cropped images of them."""
|
43 |
+
try:
|
44 |
+
resp = CLIENT_EYES.infer(image_path, model_id="eye-detection-kso3d/3")
|
45 |
+
crops = []
|
46 |
+
for p in resp.get("predictions", []):
|
47 |
+
x1 = int(p['x'] - p['width'] / 2)
|
48 |
+
y1 = int(p['y'] - p['height'] / 2)
|
49 |
+
x2 = int(p['x'] + p['width'] / 2)
|
50 |
+
y2 = int(p['y'] + p['height'] / 2)
|
51 |
+
crop = raw_image[y1:y2, x1:x2]
|
52 |
+
if crop.size > 0:
|
53 |
+
crops.append(crop)
|
54 |
+
# On success, return the crops and None for the error message
|
55 |
+
return crops, None
|
56 |
+
except Exception as e:
|
57 |
+
# If Roboflow fails, return an empty list and the error message
|
58 |
+
print(f"Error in Roboflow eye detection: {e}")
|
59 |
+
return [], str(e)
|
60 |
|
61 |
def get_largest_iris_prediction(eye_crop):
|
62 |
"Calls Roboflow to find the largest iris using a temporary file for reliability."
|
|
|
105 |
if not detect_faces_roboflow(temp_image_path):
|
106 |
return JSONResponse(status_code=400, content={"error": "No face detected."})
|
107 |
|
108 |
+
eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, raw_image)
|
109 |
+
if error_msg or len(eye_crops) != 2:
|
110 |
+
return JSONResponse(status_code=200, content={"warnings": ["Exactly two eyes not detected."]})
|
111 |
|
112 |
eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
|
113 |
|