skibi11 commited on
Commit
76a0766
·
verified ·
1 Parent(s): 18f7e14

show the exact image that the model analyzed

Browse files
Files changed (1) hide show
  1. app.py +20 -17
app.py CHANGED
@@ -106,45 +106,41 @@ 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
  # First, try to detect eyes on the original image
110
  print("--- Attempting detection on original image... ---")
111
  eye_crops, error_msg = detect_eyes_roboflow(temp_image_path, raw_image)
112
-
113
  # If that fails, create a mirrored version and try again
114
  if len(eye_crops) != 2:
115
  print("--- Original failed. Attempting detection on mirrored image... ---")
116
- mirrored_image = cv2.flip(raw_image, 1) # 1 means a horizontal flip
117
-
118
- # We need a new temporary file for the mirrored image to send to Roboflow
119
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_mirrored:
120
  cv2.imwrite(tmp_mirrored.name, mirrored_image)
121
  temp_mirrored_image_path = tmp_mirrored.name
122
-
123
  try:
124
- # Run detection again on the new mirrored image
125
- # NOTE: We use mirrored_image for cropping, not raw_image
126
  eye_crops, error_msg = detect_eyes_roboflow(temp_mirrored_image_path, mirrored_image)
127
  finally:
128
- # Always clean up the second temporary file
129
  os.remove(temp_mirrored_image_path)
130
-
131
- # Finally, after trying both original and mirrored, check if we have a valid result
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
- # Prepare to store all our results
 
141
  flags = {}
142
  eye_images_b64 = {}
143
 
144
  for i, eye_crop in enumerate(eye_crops):
145
  side = "right" if i == 0 else "left"
146
-
147
- # --- NEW: Encode the cropped eye image to Base64 ---
148
  is_success, buffer = cv2.imencode(".jpg", eye_crop)
149
  if is_success:
150
  eye_images_b64[side] = "data:image/jpeg;base64," + base64.b64encode(buffer).decode("utf-8")
@@ -159,11 +155,18 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
159
  else:
160
  flags[side] = None
161
 
162
- # --- NEW: Include the images in the final response ---
 
 
 
 
 
 
163
  return JSONResponse(content={
164
  "leukocoria": flags,
165
  "warnings": [],
166
- "two_eyes": eye_images_b64 # Add the eye images here
 
167
  })
168
 
169
  finally:
 
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,
134
  content={"error": "Could not detect exactly two eyes. Please try another photo."}
135
  )
 
 
136
 
137
+ eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
138
+
139
  flags = {}
140
  eye_images_b64 = {}
141
 
142
  for i, eye_crop in enumerate(eye_crops):
143
  side = "right" if i == 0 else "left"
 
 
144
  is_success, buffer = cv2.imencode(".jpg", eye_crop)
145
  if is_success:
146
  eye_images_b64[side] = "data:image/jpeg;base64," + base64.b64encode(buffer).decode("utf-8")
 
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: