Spaces:
Running
Running
fix labeling and results
Browse files
app.py
CHANGED
@@ -89,6 +89,8 @@ def run_leukocoria_prediction(iris_crop):
|
|
89 |
# --- 3. FastAPI Application ---
|
90 |
app = FastAPI()
|
91 |
|
|
|
|
|
92 |
@app.post("/detect/")
|
93 |
async def full_detection_pipeline(image: UploadFile = File(...)):
|
94 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
@@ -107,7 +109,7 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
107 |
# --- This is the final corrected logic ---
|
108 |
|
109 |
image_to_process = raw_image
|
110 |
-
was_mirrored = False #
|
111 |
|
112 |
print("--- Attempting detection on original image... ---")
|
113 |
eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, image_to_process)
|
@@ -116,7 +118,7 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
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 #
|
120 |
|
121 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_mirrored:
|
122 |
cv2.imwrite(tmp_mirrored.name, mirrored_image)
|
@@ -126,7 +128,6 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
126 |
finally:
|
127 |
os.remove(temp_mirrored_image_path)
|
128 |
|
129 |
-
# Final check after both attempts
|
130 |
if error_msg or len(eye_crops) != 2:
|
131 |
return JSONResponse(
|
132 |
status_code=400,
|
@@ -136,8 +137,9 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
136 |
# Sort the eyes from left to right based on their position in the image
|
137 |
eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
|
138 |
|
139 |
-
# ---
|
140 |
-
#
|
|
|
141 |
if was_mirrored:
|
142 |
print("--- Image was mirrored, reversing eye order for correct labeling. ---")
|
143 |
eye_crops.reverse()
|
@@ -145,7 +147,7 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
145 |
flags = {}
|
146 |
eye_images_b64 = {}
|
147 |
for i, eye_crop in enumerate(eye_crops):
|
148 |
-
#
|
149 |
side = "right" if i == 0 else "left"
|
150 |
|
151 |
is_success, buffer = cv2.imencode(".jpg", eye_crop)
|
@@ -176,7 +178,7 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
|
|
176 |
|
177 |
finally:
|
178 |
os.remove(temp_image_path)
|
179 |
-
|
180 |
# --- 4. Create and Mount the Gradio UI for a professional homepage ---
|
181 |
def gradio_wrapper(image_array):
|
182 |
"""A wrapper function to call our own FastAPI endpoint from the Gradio UI."""
|
|
|
89 |
# --- 3. FastAPI Application ---
|
90 |
app = FastAPI()
|
91 |
|
92 |
+
# In app.py, replace the existing function with this one
|
93 |
+
|
94 |
@app.post("/detect/")
|
95 |
async def full_detection_pipeline(image: UploadFile = File(...)):
|
96 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
|
|
109 |
# --- This is the final corrected logic ---
|
110 |
|
111 |
image_to_process = raw_image
|
112 |
+
was_mirrored = False # Add a flag to track if we flipped the image
|
113 |
|
114 |
print("--- Attempting detection on original image... ---")
|
115 |
eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, image_to_process)
|
|
|
118 |
print("--- Original failed. Attempting detection on mirrored image... ---")
|
119 |
mirrored_image = cv2.flip(raw_image, 1)
|
120 |
image_to_process = mirrored_image
|
121 |
+
was_mirrored = True # Set the flag to true
|
122 |
|
123 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_mirrored:
|
124 |
cv2.imwrite(tmp_mirrored.name, mirrored_image)
|
|
|
128 |
finally:
|
129 |
os.remove(temp_mirrored_image_path)
|
130 |
|
|
|
131 |
if error_msg or len(eye_crops) != 2:
|
132 |
return JSONResponse(
|
133 |
status_code=400,
|
|
|
137 |
# Sort the eyes from left to right based on their position in the image
|
138 |
eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
|
139 |
|
140 |
+
# --- THE CRITICAL FIX ---
|
141 |
+
# If the image was mirrored, the eye on the left of the image is the person's
|
142 |
+
# left eye. We need to reverse the list so the person's right eye is first.
|
143 |
if was_mirrored:
|
144 |
print("--- Image was mirrored, reversing eye order for correct labeling. ---")
|
145 |
eye_crops.reverse()
|
|
|
147 |
flags = {}
|
148 |
eye_images_b64 = {}
|
149 |
for i, eye_crop in enumerate(eye_crops):
|
150 |
+
# Because of the sort and potential reverse, i=0 is ALWAYS the person's right eye
|
151 |
side = "right" if i == 0 else "left"
|
152 |
|
153 |
is_success, buffer = cv2.imencode(".jpg", eye_crop)
|
|
|
178 |
|
179 |
finally:
|
180 |
os.remove(temp_image_path)
|
181 |
+
|
182 |
# --- 4. Create and Mount the Gradio UI for a professional homepage ---
|
183 |
def gradio_wrapper(image_array):
|
184 |
"""A wrapper function to call our own FastAPI endpoint from the Gradio UI."""
|