skibi11 commited on
Commit
31e7c35
·
verified ·
1 Parent(s): 4611e20
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -5,6 +5,9 @@ import cv2
5
  import tempfile
6
  import numpy as np
7
  import uvicorn
 
 
 
8
  from PIL import Image
9
  from inference_sdk import InferenceHTTPClient
10
  from fastapi import FastAPI, File, UploadFile
@@ -90,8 +93,11 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
90
  return JSONResponse(status_code=400, content={"error": "Exactly two eyes not detected."})
91
 
92
  results = {}
93
- for i, eye_crop in enumerate(eye_crops):
94
- side = f"eye_{i+1}"
 
 
 
95
  iris_crop = detect_iris_roboflow(eye_crop)
96
  if iris_crop is None:
97
  results[side] = {"status": "No iris detected", "prediction": None}
@@ -105,13 +111,11 @@ async def full_detection_pipeline(image: UploadFile = File(...)):
105
  finally:
106
  os.remove(temp_image_path)
107
 
108
- # --- 4. Create the Gradio UI for the homepage ---
109
- # This UI will call our own FastAPI endpoint, ensuring consistent logic.
110
- def gradio_wrapper(image):
111
  """A wrapper function to call our own FastAPI endpoint from the Gradio UI."""
112
  try:
113
- # Save the numpy array from Gradio to a temporary file to send to our API
114
- pil_image = Image.fromarray(image)
115
  with tempfile.NamedTemporaryFile(mode="wb", suffix=".jpg", delete=False) as tmp:
116
  pil_image.save(tmp, format="JPEG")
117
  tmp_path = tmp.name
@@ -121,27 +125,25 @@ def gradio_wrapper(image):
121
  # The API is running on the same server, so we call it locally
122
  response = requests.post("http://127.0.0.1:7860/api/detect/", files=files)
123
 
124
- os.remove(tmp_path) # Clean up the temp file
125
 
126
  if response.status_code == 200:
127
  return response.json()
128
  else:
129
  return {"error": f"API Error {response.status_code}", "details": response.text}
130
-
131
  except Exception as e:
132
  return {"error": str(e)}
133
 
134
  gradio_ui = gr.Interface(
135
  fn=gradio_wrapper,
136
- inputs=gr.Image(type="numpy", label="Upload an eye image to test"),
137
- outputs=gr.JSON(label="Prediction Results"),
138
  title="LeukoLook Eye Detector",
139
- description="A demonstration of the LeukoLook detection model. This UI calls the same API endpoint that the main application uses."
140
  )
141
 
142
- # --- 5. Mount the Gradio UI onto the FastAPI app's root ---
143
  app = gr.mount_gradio_app(app, gradio_ui, path="/")
144
 
145
- # --- 6. Run the server ---
146
  if __name__ == "__main__":
147
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
5
  import tempfile
6
  import numpy as np
7
  import uvicorn
8
+ import requests
9
+ import base64
10
+ import io
11
  from PIL import Image
12
  from inference_sdk import InferenceHTTPClient
13
  from fastapi import FastAPI, File, UploadFile
 
93
  return JSONResponse(status_code=400, content={"error": "Exactly two eyes not detected."})
94
 
95
  results = {}
96
+ # Sort eye crops by x-coordinate to have a consistent left-right order
97
+ sorted_eye_crops = sorted(eye_crops, key=lambda c: cv2.boundingRect(c)[0])
98
+
99
+ for i, eye_crop in enumerate(sorted_eye_crops):
100
+ side = "left_eye" if i == 0 else "right_eye"
101
  iris_crop = detect_iris_roboflow(eye_crop)
102
  if iris_crop is None:
103
  results[side] = {"status": "No iris detected", "prediction": None}
 
111
  finally:
112
  os.remove(temp_image_path)
113
 
114
+ # --- 4. Create and Mount the Gradio UI for a professional homepage ---
115
+ def gradio_wrapper(image_array):
 
116
  """A wrapper function to call our own FastAPI endpoint from the Gradio UI."""
117
  try:
118
+ pil_image = Image.fromarray(image_array.astype('uint8'), 'RGB')
 
119
  with tempfile.NamedTemporaryFile(mode="wb", suffix=".jpg", delete=False) as tmp:
120
  pil_image.save(tmp, format="JPEG")
121
  tmp_path = tmp.name
 
125
  # The API is running on the same server, so we call it locally
126
  response = requests.post("http://127.0.0.1:7860/api/detect/", files=files)
127
 
128
+ os.remove(tmp_path)
129
 
130
  if response.status_code == 200:
131
  return response.json()
132
  else:
133
  return {"error": f"API Error {response.status_code}", "details": response.text}
 
134
  except Exception as e:
135
  return {"error": str(e)}
136
 
137
  gradio_ui = gr.Interface(
138
  fn=gradio_wrapper,
139
+ inputs=gr.Image(type="numpy", label="Upload an eye image to test the full pipeline"),
140
+ outputs=gr.JSON(label="Analysis Results"),
141
  title="LeukoLook Eye Detector",
142
+ description="A demonstration of the LeukoLook detection model pipeline."
143
  )
144
 
 
145
  app = gr.mount_gradio_app(app, gradio_ui, path="/")
146
 
147
+ # --- 5. Run the server ---
148
  if __name__ == "__main__":
149
  uvicorn.run(app, host="0.0.0.0", port=7860)