File size: 9,358 Bytes
4cd3714
 
 
 
 
 
dc36253
4cd3714
 
dc36253
4cd3714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc36253
4cd3714
 
 
 
 
dc36253
4cd3714
 
dc36253
 
 
4cd3714
 
dc36253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cd3714
 
dc36253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cd3714
 
 
 
 
 
 
dc36253
 
 
 
 
 
 
 
 
 
 
 
 
 
64a8807
dc36253
64a8807
4cd3714
64a8807
 
dc36253
64a8807
 
dc36253
64a8807
 
 
 
 
dc36253
64a8807
 
 
4cd3714
64a8807
 
 
 
4cd3714
64a8807
 
4cd3714
64a8807
 
 
 
4cd3714
64a8807
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
251
252
253
254
255
256
257
258
259
260
261
import gradio as gr
from ultralytics import YOLO
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import sqlite3
import tempfile
import pandas as pd

# Load YOLOv8 model
model = YOLO("best.pt")

# Function to perform prediction
def predict_image(input_image, name, patient_id):
    if input_image is None:
        return None, "Please Input The Image"

    # Convert Gradio input image (PIL Image) to numpy array
    image_np = np.array(input_image)

    # Ensure the image is in the correct format
    if len(image_np.shape) == 2:  # grayscale to RGB
        image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
    elif image_np.shape[2] == 4:  # RGBA to RGB
        image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)

    # Perform prediction
    results = model(image_np)

    # Draw bounding boxes on the image
    image_with_boxes = image_np.copy()
    raw_predictions = []
    label = "Unknown"  # Default label if no detection

    if results[0].boxes:
        for box in results[0].boxes:
            # Get class index and confidence for each detection
            class_index = box.cls.item()
            confidence = box.conf.item()

            # Determine the label based on the class index
            if class_index == 0:
                label = "Mature"
                color = (255, 0, 0)  # Red for Mature
            elif class_index == 1:
                label = "Immature"
                color = (0, 255, 255)  # Yellow for Immature
            else:
                label = "Normal"
                color = (0, 255, 0)  # Green for Normal

            xmin, ymin, xmax, ymax = map(int, box.xyxy[0])

            # Draw the bounding box
            cv2.rectangle(image_with_boxes, (xmin, ymin), (xmax, ymax), color, 2)

            # Enlarge font scale and thickness
            font_scale = 1.0
            thickness = 2

            # Calculate label background size
            (text_width, text_height), baseline = cv2.getTextSize(f'{label} {confidence:.2f}', cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness)
            cv2.rectangle(image_with_boxes, (xmin, ymin - text_height - baseline), (xmin + text_width, ymin), (0, 0, 0), cv2.FILLED)

            # Put the label text with black background
            cv2.putText(image_with_boxes, f'{label} {confidence:.2f}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), thickness)

            raw_predictions.append(f"Label: {label}, Confidence: {confidence:.2f}, Box: [{xmin}, {ymin}, {xmax}, {ymax}]")

    raw_predictions_str = "\n".join(raw_predictions)

    # Convert to PIL image for further processing
    pil_image_with_boxes = Image.fromarray(image_with_boxes)

    # Add text and watermark
    pil_image_with_boxes = add_text_and_watermark(pil_image_with_boxes, name, patient_id, label)

    return pil_image_with_boxes, raw_predictions_str

# Function to add watermark
def add_watermark(image):
    try:
        logo = Image.open('image-logo.png').convert("RGBA")
        image = image.convert("RGBA")

        # Resize logo
        basewidth = 100
        wpercent = (basewidth / float(logo.size[0]))
        hsize = int((float(wpercent) * logo.size[1]))
        logo = logo.resize((basewidth, hsize), Image.LANCZOS)

        # Position logo
        position = (image.width - logo.width - 10, image.height - logo.height - 10)

        # Composite image
        transparent = Image.new('RGBA', (image.width, image.height), (0, 0, 0, 0))
        transparent.paste(image, (0, 0))
        transparent.paste(logo, position, mask=logo)

        return transparent.convert("RGB")
    except Exception as e:
        print(f"Error adding watermark: {e}")
        return image

# Function to add text and watermark
def add_text_and_watermark(image, name, patient_id, label):
    draw = ImageDraw.Draw(image)

    # Load a larger font (adjust the size as needed)
    font_size = 48  # Example font size
    try:
        font = ImageFont.truetype("font.ttf", size=font_size)
    except IOError:
        font = ImageFont.load_default()
        print("Error: cannot open resource, using default font.")

    text = f"Name: {name}, ID: {patient_id}, Result: {label}"

    # Calculate text bounding box
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
    text_x = 20
    text_y = 40
    padding = 10

    # Draw a filled rectangle for the background
    draw.rectangle(
        [text_x - padding, text_y - padding, text_x + text_width + padding, text_y + text_height + padding],
        fill="black"
    )

    # Draw text on top of the rectangle
    draw.text((text_x, text_y), text, fill=(255, 255, 255, 255), font=font)

    # Add watermark to the image
    image_with_watermark = add_watermark(image)

    return image_with_watermark

# Function to initialize the database
def init_db():
    conn = sqlite3.connect('results.db')
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS results
                 (id INTEGER PRIMARY KEY, name TEXT, patient_id TEXT, input_image BLOB, predicted_image BLOB, result TEXT)''')
    conn.commit()
    conn.close()

# Function to submit result to the database
def submit_result(name, patient_id, input_image, predicted_image, result):
    conn = sqlite3.connect('results.db')
    c = conn.cursor()

    input_image_np = np.array(input_image)
    _, input_buffer = cv2.imencode('.png', cv2.cvtColor(input_image_np, cv2.COLOR_RGB2BGR))
    input_image_bytes = input_buffer.tobytes()

    predicted_image_np = np.array(predicted_image)
    predicted_image_rgb = cv2.cvtColor(predicted_image_np, cv2.COLOR_RGB2BGR)  # Ensure correct color conversion
    _, predicted_buffer = cv2.imencode('.png', predicted_image_rgb)
    predicted_image_bytes = predicted_buffer.tobytes()

    c.execute("INSERT INTO results (name, patient_id, input_image, predicted_image, result) VALUES (?, ?, ?, ?, ?)", 
              (name, patient_id, input_image_bytes, predicted_image_bytes, result))
    conn.commit()
    conn.close()
    return "Result submitted to database."

# Function to load and view database
def view_database():
    conn = sqlite3.connect('results.db')
    c = conn.cursor()
    c.execute("SELECT * FROM results")
    rows = c.fetchall()
    conn.close()

    # Convert to pandas DataFrame
    df = pd.DataFrame(rows, columns=["ID", "Name", "Patient ID", "Input Image", "Predicted Image", "Result"])

    return df

# Function to download database or image
def download_file(choice):
    conn = sqlite3.connect('results.db')
    c = conn.cursor()

    if choice == "Database (.db)":
        conn.close()
        return 'results.db'
    else:
        c.execute("SELECT predicted_image FROM results ORDER BY id DESC LIMIT 1")
        row = c.fetchone()
        conn.close()
        if row:
            image_bytes = row[0]
            with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
                temp_file.write(image_bytes)
                temp_file.flush()  # Ensure all data is written before closing
                return temp_file.name
        else:
            conn.close()
            raise FileNotFoundError("No images found in the database.")

# Initialize the database
init_db()

# Gradio Interface
def interface(name, patient_id, input_image):
    if input_image is None:
        return "Please upload an image."

    output_image, raw_result = predict_image(input_image, name, patient_id)
    submit_status = submit_result(name, patient_id, input_image, output_image, raw_result)

    return output_image, raw_result, submit_status

# View Database Function
def view_db_interface():
    df = view_database()
    return df

# Download Function
def download_interface(choice):
    try:
        file_path = download_file(choice)
        with open(file_path, "rb") as file:
            return file.read(), file_path
    except Exception as e:
        return f"Error: {str(e)}", None

# Build Gradio Interface
app = gr.Blocks()

with app:
    gr.Markdown("# Eye Condition Detection System")

    with gr.Row():
        with gr.Column():
            name = gr.Textbox(label="Name", placeholder="Enter patient's name")
            patient_id = gr.Textbox(label="Patient ID", placeholder="Enter patient ID")
            input_image = gr.Image(source="upload", type="pil", label="Upload an Eye Image")

            with gr.Row():
                download_choice = gr.Radio(label="Download Choice", choices=["Database (.db)", "Last Predicted Image (.png)"], value="Database (.db)")
                download_button = gr.Button("Download")

        with gr.Column():
            predicted_image = gr.Image(label="Predicted Image")
            raw_result = gr.Textbox(label="Raw Prediction Results", interactive=False)
            submit_status = gr.Textbox(label="Submission Status", interactive=False)

    predict_button = gr.Button("Predict")
    view_db_button = gr.Button("View Database")

    # Button actions
    predict_button.click(interface, inputs=[name, patient_id, input_image], outputs=[predicted_image, raw_result, submit_status])
    view_db_button.click(view_db_interface, outputs=gr.Dataframe())
    download_button.click(download_interface, inputs=[download_choice], outputs=[gr.File(), gr.Textbox()])

# Launch the Gradio app
app.launch()