Spaces:
Running
Running
fix inconsistent labeling
Browse files
app.py
CHANGED
@@ -104,44 +104,49 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
104 |
if not detect_faces_roboflow(temp_image_path):
|
105 |
return JSONResponse(status_code=400, content={"error": "No face detected."})
|
106 |
|
107 |
-
# --- This is the corrected
|
108 |
|
109 |
-
# By default, assume we will use the original image
|
110 |
image_to_process = raw_image
|
|
|
111 |
|
112 |
-
# First, try detection on the original image
|
113 |
print("--- Attempting detection on original image... ---")
|
114 |
eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, image_to_process)
|
115 |
|
116 |
-
# If the first attempt fails, try the mirrored version
|
117 |
if len(eye_crops) != 2:
|
118 |
print("--- Original failed. Attempting detection on mirrored image... ---")
|
119 |
mirrored_image = cv2.flip(raw_image, 1)
|
120 |
-
image_to_process = mirrored_image
|
121 |
-
|
|
|
122 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_mirrored:
|
123 |
cv2.imwrite(tmp_mirrored.name, mirrored_image)
|
124 |
temp_mirrored_image_path = tmp_mirrored.name
|
125 |
try:
|
126 |
-
# Ensure we pass the mirrored image data for cropping
|
127 |
eye_crops, error_msg = detect_eyes_roboflow(temp_mirrored_image_path, image_to_process)
|
128 |
finally:
|
129 |
os.remove(temp_mirrored_image_path)
|
130 |
|
131 |
-
# Final check after both attempts
|
132 |
if error_msg or len(eye_crops) != 2:
|
133 |
return JSONResponse(
|
134 |
status_code=400,
|
135 |
content={"error": "Could not detect exactly two eyes. Please try another photo."}
|
136 |
)
|
137 |
|
|
|
138 |
eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
flags = {}
|
141 |
eye_images_b64 = {}
|
142 |
-
|
143 |
for i, eye_crop in enumerate(eye_crops):
|
|
|
144 |
side = "right" if i == 0 else "left"
|
|
|
145 |
is_success, buffer = cv2.imencode(".jpg", eye_crop)
|
146 |
if is_success:
|
147 |
eye_images_b64[side] = "data:image/jpeg;base64," + base64.b64encode(buffer).decode("utf-8")
|
@@ -156,7 +161,6 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
156 |
else:
|
157 |
flags[side] = None
|
158 |
|
159 |
-
# Encode the image that was successfully analyzed (original or mirrored)
|
160 |
is_success_main, buffer_main = cv2.imencode(".jpg", image_to_process)
|
161 |
analyzed_image_b64 = ""
|
162 |
if is_success_main:
|
|
|
104 |
if not detect_faces_roboflow(temp_image_path):
|
105 |
return JSONResponse(status_code=400, content={"error": "No face detected."})
|
106 |
|
107 |
+
# --- This is the final corrected logic ---
|
108 |
|
|
|
109 |
image_to_process = raw_image
|
110 |
+
was_mirrored = False # --- NEW: Add a flag to track if we flipped the image
|
111 |
|
|
|
112 |
print("--- Attempting detection on original image... ---")
|
113 |
eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, image_to_process)
|
114 |
|
|
|
115 |
if len(eye_crops) != 2:
|
116 |
print("--- Original failed. Attempting detection on mirrored image... ---")
|
117 |
mirrored_image = cv2.flip(raw_image, 1)
|
118 |
+
image_to_process = mirrored_image
|
119 |
+
was_mirrored = True # --- NEW: Set the flag to true
|
120 |
+
|
121 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_mirrored:
|
122 |
cv2.imwrite(tmp_mirrored.name, mirrored_image)
|
123 |
temp_mirrored_image_path = tmp_mirrored.name
|
124 |
try:
|
|
|
125 |
eye_crops, error_msg = detect_eyes_roboflow(temp_mirrored_image_path, image_to_process)
|
126 |
finally:
|
127 |
os.remove(temp_mirrored_image_path)
|
128 |
|
|
|
129 |
if error_msg or len(eye_crops) != 2:
|
130 |
return JSONResponse(
|
131 |
status_code=400,
|
132 |
content={"error": "Could not detect exactly two eyes. Please try another photo."}
|
133 |
)
|
134 |
|
135 |
+
# Sort the eyes from left to right based on their position in the image
|
136 |
eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
|
137 |
|
138 |
+
# --- NEW: If the image was mirrored, reverse the sorted list ---
|
139 |
+
# This ensures the person's right eye is always first.
|
140 |
+
if was_mirrored:
|
141 |
+
print("--- Image was mirrored, reversing eye order for correct labeling. ---")
|
142 |
+
eye_crops.reverse()
|
143 |
+
|
144 |
flags = {}
|
145 |
eye_images_b64 = {}
|
|
|
146 |
for i, eye_crop in enumerate(eye_crops):
|
147 |
+
# Now, because of the reverse, i=0 is ALWAYS the person's right eye
|
148 |
side = "right" if i == 0 else "left"
|
149 |
+
|
150 |
is_success, buffer = cv2.imencode(".jpg", eye_crop)
|
151 |
if is_success:
|
152 |
eye_images_b64[side] = "data:image/jpeg;base64," + base64.b64encode(buffer).decode("utf-8")
|
|
|
161 |
else:
|
162 |
flags[side] = None
|
163 |
|
|
|
164 |
is_success_main, buffer_main = cv2.imencode(".jpg", image_to_process)
|
165 |
analyzed_image_b64 = ""
|
166 |
if is_success_main:
|