skibi11 commited on
Commit
7b3ffbf
·
verified ·
1 Parent(s): 76a0766

show the exact image that the model analyzed1

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -89,8 +89,6 @@ def run_leukocoria_prediction(iris_crop):
89
  # --- 3. FastAPI Application ---
90
  app = FastAPI()
91
 
92
- # In app.py - an updated full_detection_pipeline function
93
-
94
  @app.post("/detect/")
95
  async def full_detection_pipeline(image: UploadFile = File(...)):
96
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
@@ -106,28 +104,31 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
106
  if not detect_faces_roboflow(temp_image_path):
107
  return JSONResponse(status_code=400, content={"error": "No face detected."})
108
 
109
- # --- NEW: Logic to track which image is used for analysis ---
110
- image_for_analysis = raw_image
111
-
112
- # First, try to detect eyes on the original image
 
 
113
  print("--- Attempting detection on original image... ---")
114
- eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, raw_image)
115
 
116
- # If that fails, create a mirrored version and try again
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_for_analysis = mirrored_image # Set the mirrored image as the one to use
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
- eye_crops, error_msg = detect_eyes_roboflow(temp_mirrored_image_path, mirrored_image)
 
127
  finally:
128
  os.remove(temp_mirrored_image_path)
129
 
130
- # After trying both, check if we have a valid result
131
  if error_msg or len(eye_crops) != 2:
132
  return JSONResponse(
133
  status_code=400,
@@ -154,19 +155,18 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
154
  flags[side] = has_leuko
155
  else:
156
  flags[side] = None
157
-
158
- # --- NEW: Encode the successfully analyzed image to send back ---
159
- is_success_main, buffer_main = cv2.imencode(".jpg", image_for_analysis)
160
  analyzed_image_b64 = ""
161
  if is_success_main:
162
  analyzed_image_b64 = "data:image/jpeg;base64," + base64.b64encode(buffer_main).decode("utf-8")
163
 
164
- # --- NEW: Add the analyzed_image to the final response ---
165
  return JSONResponse(content={
166
  "leukocoria": flags,
167
  "warnings": [],
168
  "two_eyes": eye_images_b64,
169
- "analyzed_image": analyzed_image_b64 # This is the new field
170
  })
171
 
172
  finally:
 
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:
 
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, robust logic ---
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 # Set the mirrored image as the one to use now
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,
 
155
  flags[side] = has_leuko
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:
163
  analyzed_image_b64 = "data:image/jpeg;base64," + base64.b64encode(buffer_main).decode("utf-8")
164
 
 
165
  return JSONResponse(content={
166
  "leukocoria": flags,
167
  "warnings": [],
168
  "two_eyes": eye_images_b64,
169
+ "analyzed_image": analyzed_image_b64
170
  })
171
 
172
  finally: