File size: 8,096 Bytes
80eac1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
import pandas as pd
import PIL.Image as Image
import gradio as gr
import numpy as np
import math
from pathlib import Path
from ultralytics import ASSETS, YOLO

DIR_NAME = Path(os.path.dirname(__file__))
DETECTION_MODEL_n = os.path.join(DIR_NAME, 'models', 'YOLOv8-N_CNO_Detection.pt')
DETECTION_MODEL_s = os.path.join(DIR_NAME, 'models', 'YOLOv8-S_CNO_Detection.pt')
DETECTION_MODEL_m = os.path.join(DIR_NAME, 'models', 'YOLOv8-M_CNO_Detection.pt')
DETECTION_MODEL_l = os.path.join(DIR_NAME, 'models', 'YOLOv8-L_CNO_Detection.pt')
DETECTION_MODEL_x = os.path.join(DIR_NAME, 'models', 'YOLOv8-X_CNO_Detection.pt')

# MODEL = os.path.join(DIR_NAME, 'models', 'YOLOv8-M_CNO_Detection.pt')
# model = YOLO(MODEL)
# cno_df = pd.DataFrame()

def predict_image(name, model, img, conf_threshold, iou_threshold):
    """Predicts and plots labeled objects in an image using YOLOv8 model with adjustable confidence and IOU thresholds."""
    gr.Info("Starting process")
    # gr.Warning("Name is empty")
    if name == "":
        gr.Warning("Name is empty")

    if model == 'YOLOv8-N':
        CNO_model = YOLO(DETECTION_MODEL_n)
    elif model == 'YOLOv8-S':
        CNO_model = YOLO(DETECTION_MODEL_s)
    elif model == 'YOLOv8-M':
        CNO_model = YOLO(DETECTION_MODEL_m)
    elif model == 'YOLOv8-L':
        CNO_model = YOLO(DETECTION_MODEL_l)
    else:
        CNO_model = YOLO(DETECTION_MODEL_x)

    results = CNO_model.predict(
        source=img,
        conf=conf_threshold,
        iou=iou_threshold,
        show_labels=False,
        show_conf=False,
        imgsz=512,
        max_det=1200
    )

    cno_count = []
    cno_image = []
    file_name = []

    # print("deb", img)

    for idx, result in enumerate(results):
        cno = len(result.boxes)
        cno_coor = np.empty([cno, 2], dtype=int)
        file_label = img[idx].split(os.sep)[-1]
        for j in range(cno):
            # w = r.boxes.xywh[j][2]
            # h = r.boxes.xywh[j][3]
            # area = (math.pi * w * h / 4) * 20 * 20 / (512 * 512)
            # total_area += area
            # bbox_img = r.orig_img
            x = round(result.boxes.xywh[j][0].item())
            y = round(result.boxes.xywh[j][1].item())

            x1 = round(result.boxes.xyxy[j][0].item())
            y1 = round(result.boxes.xyxy[j][1].item())
            x2 = round(result.boxes.xyxy[j][2].item())
            y2 = round(result.boxes.xyxy[j][3].item())

            cno_coor[j] = [x, y]
            cv2.rectangle(result.orig_img, (x1, y1), (x2, y2), (0, 255, 0), 1)
        im_array = result.orig_img
        cno_image.append([Image.fromarray(im_array[..., ::-1]), file_label])
        cno_count.append(cno)
        file_name.append(file_label)

    """
    for r in results:
        CNO = len(r.boxes)
        CNO_coor = np.empty([CNO, 2], dtype=int)
        for j in range(CNO):
            # w = r.boxes.xywh[j][2]
            # h = r.boxes.xywh[j][3]
            # area = (math.pi * w * h / 4) * 20 * 20 / (512 * 512)
            # total_area += area
            # bbox_img = r.orig_img
            x = round(r.boxes.xywh[j][0].item())
            y = round(r.boxes.xywh[j][1].item())

            x1 = round(r.boxes.xyxy[j][0].item())
            y1 = round(r.boxes.xyxy[j][1].item())
            x2 = round(r.boxes.xyxy[j][2].item())
            y2 = round(r.boxes.xyxy[j][3].item())

            CNO_coor[j] = [x, y]
            cv2.rectangle(r.orig_img, (x1, y1), (x2, y2), (0, 255, 0), 1)
        im_array = r.orig_img
        im = Image.fromarray(im_array[..., ::-1])

    CNO_count = "CNO Count: " + str(CNO)
    
    test = []
    for i in range(len(cno_image)):
        test.append([cno_image[0], f"label {i}"])
    """
    data = {
        "Files": file_name,
        "CNO Count": cno_count,
    }

    # load data into a DataFrame object:
    cno_df = pd.DataFrame(data)

    return cno_df, cno_image

def highlight_max(s, props=''):
    return np.where(s == np.nanmax(s.values), props, '')

def highlight_df(df, data: gr.SelectData):

    styler = df.style.apply(lambda x: ['background: lightgreen'
                               if x.Files == data.value["caption"]
                               else None for i in x], axis=1)

    # print("selected", data.value["caption"])
    return data.value["caption"], styler

def reset():
    name_textbox = ""
    gender_radio = None
    age_slider = 0
    fitzpatrick = 1
    history = []
    model_radio = "YOLOv8-M"
    input_files = []
    conf_slider = 0.2
    iou_slider = 0.5
    analysis_results = []
    cno_gallery = []
    test_label = ""

    return name_textbox, gender_radio, age_slider, fitzpatrick, history, model_radio, input_files, conf_slider, iou_slider, analysis_results, cno_gallery, test_label


with gr.Blocks(title="AFM AI Analysis", theme="default") as app:
    with gr.Row():
        with gr.Column():
            # gr.Markdown("User Information")
            with gr.Accordion("User Information", open=True):
                name_textbox = gr.Textbox(label="Name")
                with gr.Row():
                    gender_radio = gr.Radio(["Male", "Female"], label="Gender", interactive=True, scale=1)
                    age_slider = gr.Slider(minimum=0, maximum=100, step=1, value=0, label="Age", interactive=True, scale=2)
                with gr.Group():
                    fitzpatrick = gr.Slider(minimum=1, maximum=6, step=1, value=1, label="Fitzpatrick", interactive=True)
                history = gr.Checkboxgroup(["Familial Disease", "Allergic Rhinitis", "Asthma"], label="Medical History", interactive=True)

            input_files = gr.File(file_types=["image"], file_count="multiple", label="Upload Image")
            # gr.Markdown("Model Configuration")
            with gr.Accordion("Model Configuration", open=False):
                model_radio = gr.Radio(["YOLOv8-N", "YOLOv8-S", "YOLOv8-M", "YOLOv8-L", "YOLOv8-X"], label="Model Selection", value="YOLOv8-M")
                conf_slider = gr.Slider(minimum=0, maximum=1, value=0.2, label="Confidence threshold")
                iou_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="IoU threshold")
            with gr.Row():
                analyze_btn = gr.Button("Analyze")
                clear_btn = gr.Button("Reset")
        with gr.Column():
            analysis_results = gr.Dataframe(headers=["Files", "CNO Count"], interactive=False)
            # cno_label = gr.Label(label="Analysis Results")
            cno_gallery = gr.Gallery(label="Result", show_label=True, columns=3, object_fit="contain")
            test_label = gr.Label(label="Analysis Results")
            # cno_img = gr.Image(type="pil", label="Result")

    analyze_btn.click(
        fn=predict_image,
        inputs=[name_textbox, model_radio, input_files, conf_slider, iou_slider],
        outputs=[analysis_results, cno_gallery]
    )

    clear_btn.click(reset, outputs=[name_textbox, gender_radio, age_slider, fitzpatrick, history, model_radio,
                                    input_files, conf_slider, iou_slider, analysis_results, cno_gallery, test_label])

    cno_gallery.select(highlight_df, inputs=analysis_results, outputs=[test_label, analysis_results])


"""
iface = gr.Interface(
    fn=predict_image,
    inputs=[
        gr.Textbox(label="User Name"),
        gr.Radio(["YOLOv8-N", "YOLOv8-S", "YOLOv8-M", "YOLOv8-L", "YOLOv8-X"], value="YOLOv8-M"),
        # gr.Image(type="filepath", label="Upload Image"),
        gr.File(file_types=["image"], file_count="multiple", label="Upload Image"),
        gr.Slider(minimum=0, maximum=1, value=0.2, label="Confidence threshold"),
        gr.Slider(minimum=0, maximum=1, value=0.5, label="IoU threshold")
    ],
    outputs=[gr.Label(label="Analysis Results"), gr.Image(type="pil", label="Result")],
    title="AFM AI Analysis",
    description="Upload images for inference. The YOLOv8-M model is used by default.",
    theme=gr.themes.Default()
)
"""

if __name__ == '__main__':
    # iface.launch()
    app.launch(auth=('user', 'admin'), auth_message="Enter your username and password")