skibi11 commited on
Commit
916b4c6
·
verified ·
1 Parent(s): e0cf57f

with comments

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -1,11 +1,12 @@
1
  # app.py
2
- # Adapted to follow the logic from the provided Django api/views.py
3
  import os
4
  import cv2
5
  import tempfile
6
  import numpy as np
7
  import uvicorn
8
  import base64
 
9
  from PIL import Image
10
  from inference_sdk import InferenceHTTPClient
11
  from fastapi import FastAPI, File, UploadFile
@@ -57,7 +58,6 @@ def detect_eyes_roboflow(image_path):
57
  h, w = raw_image.shape[:2]
58
  scale = min(1.0, MAX_INFER_DIM / max(h, w))
59
 
60
- # Use a temporary file for inference if resizing is needed
61
  if scale < 1.0:
62
  small_image = cv2.resize(raw_image, (int(w*scale), int(h*scale)))
63
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
@@ -69,17 +69,14 @@ def detect_eyes_roboflow(image_path):
69
  try:
70
  resp = CLIENT_EYES.infer(infer_path, model_id="eye-detection-kso3d/3")
71
  finally:
72
- # Clean up temp file if one was created
73
  if scale < 1.0 and os.path.exists(infer_path):
74
  os.remove(infer_path)
75
 
76
  crops = []
77
  for p in resp.get("predictions", []):
78
- # Scale coordinates back to original image dimensions
79
  cx, cy = p["x"] / scale, p["y"] / scale
80
  bw, bh = p["width"] / scale, p["height"] / scale
81
 
82
- # Crop from the original raw image
83
  x1 = int(cx - bw / 2)
84
  y1 = int(cy - bh / 2)
85
  x2 = int(cx + bw / 2)
@@ -105,7 +102,6 @@ def get_largest_iris_prediction(eye_crop):
105
 
106
  def run_leukocoria_prediction(iris_crop):
107
  """Runs the loaded TensorFlow model on an iris crop."""
108
- # The logic from views.py is now directly in the TF model call
109
  enh = enhance_image_unsharp_mask(iris_crop)
110
  enh_rs = cv2.resize(enh, ENHANCED_SIZE)
111
 
@@ -127,38 +123,47 @@ app = FastAPI()
127
 
128
  @app.post("/detect/")
129
  async def full_detection_pipeline(image: UploadFile = File(...)):
 
130
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
131
  tmp.write(await image.read())
132
  temp_image_path = tmp.name
133
 
134
  try:
135
- # Step 1: Face Check
136
  if not detect_faces_roboflow(temp_image_path):
 
137
  return JSONResponse(status_code=200, content={"warnings": ["No face detected."]})
 
138
 
139
- # Step 2: Eye Detection
140
  raw_image, eye_crops = detect_eyes_roboflow(temp_image_path)
141
  if raw_image is None:
142
  return JSONResponse(status_code=400, content={"error": "Could not read uploaded image."})
143
-
 
144
  if len(eye_crops) != 2:
145
  return JSONResponse(status_code=200, content={
146
  "analyzed_image": to_base64(raw_image),
147
  "warnings": ["Exactly two eyes not detected."]
148
  })
149
 
150
- # Step 3: Process Eyes with NEW Labeling Logic
 
 
151
  sorted_eyes = sorted(eye_crops, key=lambda e: e["coords"][0])
 
 
 
152
  images_b64 = {}
153
  flags = {}
154
 
155
- # This new loop labels the left-most eye as "left" and right-most as "right"
156
  for side, eye_info in zip(("left", "right"), sorted_eyes):
 
157
  eye_img = eye_info["image"]
158
 
159
- # Iris detection and Leukocoria prediction
160
  pred = get_largest_iris_prediction(eye_img)
161
  if pred:
 
162
  cx, cy, w, h = pred["x"], pred["y"], pred["width"], pred["height"]
163
  x1, y1 = int(cx - w / 2), int(cy - h / 2)
164
  x2, y2 = int(cx + w / 2), int(cy + h / 2)
@@ -166,13 +171,15 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
166
  iris_crop = eye_img[y1:y2, x1:x2]
167
 
168
  has_leuko, confidence = run_leukocoria_prediction(iris_crop)
 
169
  flags[side] = has_leuko
170
  else:
 
171
  flags[side] = None
172
 
173
  images_b64[side] = to_base64(eye_img)
174
-
175
- # Step 4: Prepare and return the final response
176
  return JSONResponse(status_code=200, content={
177
  "analyzed_image": to_base64(raw_image),
178
  "two_eyes": images_b64,
 
1
  # app.py
2
+ # Adapted to follow the logic from the provided Django api/views.py with added logging
3
  import os
4
  import cv2
5
  import tempfile
6
  import numpy as np
7
  import uvicorn
8
  import base64
9
+ import io
10
  from PIL import Image
11
  from inference_sdk import InferenceHTTPClient
12
  from fastapi import FastAPI, File, UploadFile
 
58
  h, w = raw_image.shape[:2]
59
  scale = min(1.0, MAX_INFER_DIM / max(h, w))
60
 
 
61
  if scale < 1.0:
62
  small_image = cv2.resize(raw_image, (int(w*scale), int(h*scale)))
63
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
 
69
  try:
70
  resp = CLIENT_EYES.infer(infer_path, model_id="eye-detection-kso3d/3")
71
  finally:
 
72
  if scale < 1.0 and os.path.exists(infer_path):
73
  os.remove(infer_path)
74
 
75
  crops = []
76
  for p in resp.get("predictions", []):
 
77
  cx, cy = p["x"] / scale, p["y"] / scale
78
  bw, bh = p["width"] / scale, p["height"] / scale
79
 
 
80
  x1 = int(cx - bw / 2)
81
  y1 = int(cy - bh / 2)
82
  x2 = int(cx + bw / 2)
 
102
 
103
  def run_leukocoria_prediction(iris_crop):
104
  """Runs the loaded TensorFlow model on an iris crop."""
 
105
  enh = enhance_image_unsharp_mask(iris_crop)
106
  enh_rs = cv2.resize(enh, ENHANCED_SIZE)
107
 
 
123
 
124
  @app.post("/detect/")
125
  async def full_detection_pipeline(image: UploadFile = File(...)):
126
+ print("\n--- 1. Starting full detection pipeline. ---")
127
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
128
  tmp.write(await image.read())
129
  temp_image_path = tmp.name
130
 
131
  try:
132
+ print("--- 2. Checking for faces... ---")
133
  if not detect_faces_roboflow(temp_image_path):
134
+ print("--- 2a. No face detected. Aborting. ---")
135
  return JSONResponse(status_code=200, content={"warnings": ["No face detected."]})
136
+ print("--- 2b. Face found. Proceeding. ---")
137
 
138
+ print("--- 3. Detecting eyes... ---")
139
  raw_image, eye_crops = detect_eyes_roboflow(temp_image_path)
140
  if raw_image is None:
141
  return JSONResponse(status_code=400, content={"error": "Could not read uploaded image."})
142
+
143
+ print(f"--- 4. Found {len(eye_crops)} eyes. ---")
144
  if len(eye_crops) != 2:
145
  return JSONResponse(status_code=200, content={
146
  "analyzed_image": to_base64(raw_image),
147
  "warnings": ["Exactly two eyes not detected."]
148
  })
149
 
150
+ initial_coords = [e['coords'] for e in eye_crops]
151
+ print(f"--- 5. Initial eye coordinates: {initial_coords} ---")
152
+
153
  sorted_eyes = sorted(eye_crops, key=lambda e: e["coords"][0])
154
+ sorted_coords = [e['coords'] for e in sorted_eyes]
155
+ print(f"--- 6. Sorted eye coordinates: {sorted_coords} ---")
156
+
157
  images_b64 = {}
158
  flags = {}
159
 
 
160
  for side, eye_info in zip(("left", "right"), sorted_eyes):
161
+ print(f"--- 7. Processing side: '{side}' ---")
162
  eye_img = eye_info["image"]
163
 
 
164
  pred = get_largest_iris_prediction(eye_img)
165
  if pred:
166
+ print(f"--- 8. Iris found for '{side}' eye. Running leukocoria prediction... ---")
167
  cx, cy, w, h = pred["x"], pred["y"], pred["width"], pred["height"]
168
  x1, y1 = int(cx - w / 2), int(cy - h / 2)
169
  x2, y2 = int(cx + w / 2), int(cy + h / 2)
 
171
  iris_crop = eye_img[y1:y2, x1:x2]
172
 
173
  has_leuko, confidence = run_leukocoria_prediction(iris_crop)
174
+ print(f"--- 9. Prediction for '{side}' eye: Has Leukocoria={has_leuko}, Confidence={confidence:.4f} ---")
175
  flags[side] = has_leuko
176
  else:
177
+ print(f"--- 8a. No iris found for '{side}' eye. ---")
178
  flags[side] = None
179
 
180
  images_b64[side] = to_base64(eye_img)
181
+
182
+ print(f"--- 10. Final generated flags: {flags} ---")
183
  return JSONResponse(status_code=200, content={
184
  "analyzed_image": to_base64(raw_image),
185
  "two_eyes": images_b64,