Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
4cd3714
1
Parent(s):
ec67873
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import sqlite3
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
# Load YOLOv10n model
|
10 |
+
model = YOLO("best.pt")
|
11 |
+
|
12 |
+
# Define label mappings
|
13 |
+
label_mapping = {0: 'immature', 1: 'mature', 2: 'normal'}
|
14 |
+
inverse_label_mapping = {'immature': 0, 'mature': 1, 'normal': 2}
|
15 |
+
|
16 |
+
# Function to perform prediction
|
17 |
+
def predict_image(input_image, name, patient_id):
|
18 |
+
if input_image is None:
|
19 |
+
return None, "Please Input The Image"
|
20 |
+
|
21 |
+
# Convert Gradio input image (PIL Image) to numpy array
|
22 |
+
image_np = np.array(input_image)
|
23 |
+
|
24 |
+
# Ensure the image is in the correct format
|
25 |
+
if len(image_np.shape) == 2: # grayscale to RGB
|
26 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
27 |
+
elif image_np.shape[2] == 4: # RGBA to RGB
|
28 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
|
29 |
+
|
30 |
+
# Perform inference with YOLOv10n model
|
31 |
+
results = model(image_np)
|
32 |
+
|
33 |
+
# Draw bounding boxes on the image
|
34 |
+
image_with_boxes = image_np.copy()
|
35 |
+
raw_predictions = []
|
36 |
+
|
37 |
+
if results[0].boxes:
|
38 |
+
# Iterate through each detected object
|
39 |
+
for i in range(len(results[0].boxes)):
|
40 |
+
box = results[0].boxes[i]
|
41 |
+
predicted_class = int(box.cls.item())
|
42 |
+
confidence = box.conf.item()
|
43 |
+
|
44 |
+
# Apply confidence threshold
|
45 |
+
if confidence >= 0.5:
|
46 |
+
# Map the predicted class to the label
|
47 |
+
label = label_mapping[predicted_class]
|
48 |
+
|
49 |
+
# Get the bounding box coordinates
|
50 |
+
xmin, ymin, xmax, ymax = map(int, box.xyxy[0])
|
51 |
+
|
52 |
+
# Assign color for the label
|
53 |
+
color = (0, 255, 0) if label == 'normal' else (0, 255, 255) if label == 'immature' else (255, 0, 0)
|
54 |
+
|
55 |
+
# Draw the bounding box
|
56 |
+
cv2.rectangle(image_with_boxes, (xmin, ymin), (xmax, ymax), color, 2)
|
57 |
+
|
58 |
+
# Draw the label with confidence
|
59 |
+
cv2.putText(image_with_boxes, f'{label} {confidence:.2f}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
60 |
+
|
61 |
+
raw_predictions.append(f"Label: {label}, Confidence: {confidence:.2f}, Box: [{xmin}, {ymin}, {xmax}, {ymax}]")
|
62 |
+
|
63 |
+
# Convert to PIL image for Gradio output
|
64 |
+
pil_image_with_boxes = Image.fromarray(image_with_boxes)
|
65 |
+
|
66 |
+
return pil_image_with_boxes, "\n".join(raw_predictions)
|
67 |
+
|
68 |
+
# Gradio Interface
|
69 |
+
def interface(name, patient_id, input_image):
|
70 |
+
if input_image is None:
|
71 |
+
return "Please upload an image."
|
72 |
+
|
73 |
+
# Run prediction
|
74 |
+
output_image, raw_result = predict_image(input_image, name, patient_id)
|
75 |
+
|
76 |
+
return output_image, raw_result
|
77 |
+
|
78 |
+
# Gradio Blocks
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
with gr.Column():
|
81 |
+
gr.Markdown("# Cataract Detection System")
|
82 |
+
gr.Markdown("Upload an image to detect cataract and add patient details.")
|
83 |
+
|
84 |
+
with gr.Column():
|
85 |
+
name = gr.Textbox(label="Name")
|
86 |
+
patient_id = gr.Textbox(label="Patient ID")
|
87 |
+
input_image = gr.Image(type="pil", label="Upload an Image", image_mode="RGB")
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
submit_btn = gr.Button("Submit")
|
91 |
+
output_image = gr.Image(type="pil", label="Predicted Image")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
raw_result = gr.Textbox(label="Raw Result", lines=5)
|
95 |
+
|
96 |
+
submit_btn.click(fn=interface, inputs=[name, patient_id, input_image], outputs=[output_image, raw_result])
|
97 |
+
|
98 |
+
# Launch the Gradio app
|
99 |
+
demo.launch()
|