Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
2c1e728
1
Parent(s):
925fada
Update app.py
Browse files
app.py
CHANGED
@@ -151,15 +151,18 @@ def submit_result(name, patient_id, input_image, predicted_image, result):
|
|
151 |
conn = sqlite3.connect('results.db')
|
152 |
c = conn.cursor()
|
153 |
|
|
|
154 |
input_image_np = np.array(input_image)
|
155 |
_, input_buffer = cv2.imencode('.png', cv2.cvtColor(input_image_np, cv2.COLOR_RGB2BGR))
|
156 |
input_image_bytes = input_buffer.tobytes()
|
157 |
|
|
|
158 |
predicted_image_np = np.array(predicted_image)
|
159 |
predicted_image_rgb = cv2.cvtColor(predicted_image_np, cv2.COLOR_RGB2BGR) # Ensure correct color conversion
|
160 |
_, predicted_buffer = cv2.imencode('.png', predicted_image_rgb)
|
161 |
predicted_image_bytes = predicted_buffer.tobytes()
|
162 |
|
|
|
163 |
c.execute("INSERT INTO results (name, patient_id, input_image, predicted_image, result) VALUES (?, ?, ?, ?, ?)",
|
164 |
(name, patient_id, input_image_bytes, predicted_image_bytes, result))
|
165 |
conn.commit()
|
@@ -170,12 +173,12 @@ def submit_result(name, patient_id, input_image, predicted_image, result):
|
|
170 |
def view_database():
|
171 |
conn = sqlite3.connect('results.db')
|
172 |
c = conn.cursor()
|
173 |
-
c.execute("SELECT name, patient_id FROM results")
|
174 |
rows = c.fetchall()
|
175 |
conn.close()
|
176 |
|
177 |
# Convert to pandas DataFrame for better display in Gradio
|
178 |
-
df = pd.DataFrame(rows, columns=["Name", "Patient ID"])
|
179 |
|
180 |
return df
|
181 |
|
@@ -185,7 +188,15 @@ def download_file(choice):
|
|
185 |
# Provide the path to the database file
|
186 |
return 'results.db'
|
187 |
elif choice == "Database (.html)":
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as temp_file:
|
190 |
df.to_html(temp_file.name)
|
191 |
return temp_file.name
|
@@ -214,37 +225,58 @@ def interface(name, patient_id, input_image):
|
|
214 |
|
215 |
output_image, raw_result = predict_image(input_image, name, patient_id)
|
216 |
submit_status = submit_result(name, patient_id, input_image, output_image, raw_result)
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
)
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
)
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
conn = sqlite3.connect('results.db')
|
152 |
c = conn.cursor()
|
153 |
|
154 |
+
# Encode input image
|
155 |
input_image_np = np.array(input_image)
|
156 |
_, input_buffer = cv2.imencode('.png', cv2.cvtColor(input_image_np, cv2.COLOR_RGB2BGR))
|
157 |
input_image_bytes = input_buffer.tobytes()
|
158 |
|
159 |
+
# Encode predicted image
|
160 |
predicted_image_np = np.array(predicted_image)
|
161 |
predicted_image_rgb = cv2.cvtColor(predicted_image_np, cv2.COLOR_RGB2BGR) # Ensure correct color conversion
|
162 |
_, predicted_buffer = cv2.imencode('.png', predicted_image_rgb)
|
163 |
predicted_image_bytes = predicted_buffer.tobytes()
|
164 |
|
165 |
+
# Insert into database
|
166 |
c.execute("INSERT INTO results (name, patient_id, input_image, predicted_image, result) VALUES (?, ?, ?, ?, ?)",
|
167 |
(name, patient_id, input_image_bytes, predicted_image_bytes, result))
|
168 |
conn.commit()
|
|
|
173 |
def view_database():
|
174 |
conn = sqlite3.connect('results.db')
|
175 |
c = conn.cursor()
|
176 |
+
c.execute("SELECT name, patient_id, result FROM results")
|
177 |
rows = c.fetchall()
|
178 |
conn.close()
|
179 |
|
180 |
# Convert to pandas DataFrame for better display in Gradio
|
181 |
+
df = pd.DataFrame(rows, columns=["Name", "Patient ID", "Result"])
|
182 |
|
183 |
return df
|
184 |
|
|
|
188 |
# Provide the path to the database file
|
189 |
return 'results.db'
|
190 |
elif choice == "Database (.html)":
|
191 |
+
conn = sqlite3.connect('results.db')
|
192 |
+
c = conn.cursor()
|
193 |
+
c.execute("SELECT * FROM results")
|
194 |
+
rows = c.fetchall()
|
195 |
+
conn.close()
|
196 |
+
|
197 |
+
df = pd.DataFrame(rows, columns=["ID", "Name", "Patient ID", "Input Image", "Predicted Image", "Result"])
|
198 |
+
|
199 |
+
# Convert DataFrame to HTML
|
200 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as temp_file:
|
201 |
df.to_html(temp_file.name)
|
202 |
return temp_file.name
|
|
|
225 |
|
226 |
output_image, raw_result = predict_image(input_image, name, patient_id)
|
227 |
submit_status = submit_result(name, patient_id, input_image, output_image, raw_result)
|
228 |
+
|
229 |
+
return output_image, raw_result, submit_status
|
230 |
+
|
231 |
+
# View Database Function
|
232 |
+
def view_db_interface():
|
233 |
+
df = view_database()
|
234 |
+
return df
|
235 |
+
|
236 |
+
# Download Function
|
237 |
+
def download_interface(choice):
|
238 |
+
try:
|
239 |
+
file_path = download_file(choice)
|
240 |
+
with open(file_path, "rb") as file:
|
241 |
+
return file.read(), file_path.split('/')[-1]
|
242 |
+
except FileNotFoundError as e:
|
243 |
+
return str(e), None
|
244 |
+
|
245 |
+
# Gradio Blocks
|
246 |
+
with gr.Blocks() as demo:
|
247 |
+
with gr.Column():
|
248 |
+
gr.Markdown("# Cataract Detection System")
|
249 |
+
gr.Markdown("Upload an image to detect cataract and add patient details.")
|
250 |
+
gr.Image("PR_curve.png", label="Model PR Curve")
|
251 |
+
gr.Markdown("This application uses YOLOv8 with mAP=0.981")
|
252 |
+
|
253 |
+
with gr.Column():
|
254 |
+
name = gr.Textbox(label="Name")
|
255 |
+
patient_id = gr.Textbox(label="Patient ID")
|
256 |
+
input_image = gr.Image(type="pil", label="Upload an Image", image_mode="RGB")
|
257 |
+
|
258 |
+
with gr.Column():
|
259 |
+
submit_btn = gr.Button("Submit")
|
260 |
+
output_image = gr.Image(type="pil", label="Predicted Image")
|
261 |
+
|
262 |
+
with gr.Row():
|
263 |
+
raw_result = gr.Textbox(label="Raw Result", lines=5)
|
264 |
+
submit_status = gr.Textbox(label="Submission Status")
|
265 |
+
|
266 |
+
submit_btn.click(fn=interface, inputs=[name, patient_id, input_image], outputs=[output_image, raw_result, submit_status])
|
267 |
+
|
268 |
+
with gr.Column():
|
269 |
+
view_db_btn = gr.Button("View Database")
|
270 |
+
db_output = gr.Dataframe(label="Database Records")
|
271 |
+
|
272 |
+
view_db_btn.click(fn=view_db_interface, inputs=[], outputs=[db_output])
|
273 |
+
|
274 |
+
with gr.Column():
|
275 |
+
download_choice = gr.Radio(["Database (.db)", "Predicted Image (.png)", "Database (.html)"], label="Choose the file to download:")
|
276 |
+
download_btn = gr.Button("Download")
|
277 |
+
download_output = gr.File(label="Download File")
|
278 |
+
|
279 |
+
download_btn.click(fn=download_interface, inputs=[download_choice], outputs=[download_output])
|
280 |
+
|
281 |
+
# Launch the Gradio app
|
282 |
+
demo.launch()
|