skibi11 commited on
Commit
6c7560e
·
verified ·
1 Parent(s): 65ede81

add the Base64-encoded images to the final JSON response

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -79,28 +79,41 @@ def run_leukocoria_prediction(iris_crop):
79
  # --- 3. FastAPI Application ---
80
  app = FastAPI()
81
 
 
 
82
  @app.post("/detect/")
83
  async def full_detection_pipeline(image: UploadFile = File(...)):
84
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
85
  contents = await image.read()
86
  tmp.write(contents)
87
  temp_image_path = tmp.name
88
-
89
  try:
 
 
 
 
90
  if not detect_faces_roboflow(temp_image_path):
91
  return JSONResponse(status_code=400, content={"error": "No face detected."})
92
 
93
- raw_image = cv2.imread(temp_image_path)
94
  eye_crops = detect_eyes_roboflow(temp_image_path, raw_image)
95
-
96
  if len(eye_crops) != 2:
97
- return JSONResponse(status_code=400, content={"error": "Exactly two eyes not detected."})
98
-
99
  eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
100
 
 
101
  flags = {}
 
 
102
  for i, eye_crop in enumerate(eye_crops):
103
  side = "left" if i == 0 else "right"
 
 
 
 
 
 
104
  pred = get_largest_iris_prediction(eye_crop)
105
  if pred:
106
  x1, y1 = int(pred['x'] - pred['width'] / 2), int(pred['y'] - pred['height'] / 2)
@@ -110,8 +123,13 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
110
  flags[side] = has_leuko
111
  else:
112
  flags[side] = None
113
-
114
- return JSONResponse(content={"leukocoria": flags, "warnings": []})
 
 
 
 
 
115
 
116
  finally:
117
  os.remove(temp_image_path)
 
79
  # --- 3. FastAPI Application ---
80
  app = FastAPI()
81
 
82
+ # In app.py - an updated full_detection_pipeline function
83
+
84
  @app.post("/detect/")
85
  async def full_detection_pipeline(image: UploadFile = File(...)):
86
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
87
  contents = await image.read()
88
  tmp.write(contents)
89
  temp_image_path = tmp.name
90
+
91
  try:
92
+ raw_image = cv2.imread(temp_image_path)
93
+ if raw_image is None:
94
+ return JSONResponse(status_code=400, content={"error": "Could not read uploaded image."})
95
+
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
+ return JSONResponse(status_code=200, content={"warnings": ["Exactly two eyes not detected."]})
102
+
103
  eye_crops.sort(key=lambda c: cv2.boundingRect(cv2.cvtColor(c, cv2.COLOR_BGR2GRAY))[0])
104
 
105
+ # Prepare to store all our results
106
  flags = {}
107
+ eye_images_b64 = {}
108
+
109
  for i, eye_crop in enumerate(eye_crops):
110
  side = "left" if i == 0 else "right"
111
+
112
+ # --- NEW: Encode the cropped eye image to Base64 ---
113
+ is_success, buffer = cv2.imencode(".jpg", eye_crop)
114
+ if is_success:
115
+ eye_images_b64[side] = "data:image/jpeg;base64," + base64.b64encode(buffer).decode("utf-8")
116
+
117
  pred = get_largest_iris_prediction(eye_crop)
118
  if pred:
119
  x1, y1 = int(pred['x'] - pred['width'] / 2), int(pred['y'] - pred['height'] / 2)
 
123
  flags[side] = has_leuko
124
  else:
125
  flags[side] = None
126
+
127
+ # --- NEW: Include the images in the final response ---
128
+ return JSONResponse(content={
129
+ "leukocoria": flags,
130
+ "warnings": [],
131
+ "two_eyes": eye_images_b64 # Add the eye images here
132
+ })
133
 
134
  finally:
135
  os.remove(temp_image_path)