File size: 5,260 Bytes
bc97962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a14a0f
bc97962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

from processsors import RootSegmentor
from processsors import *

from gradio_imageslider import ImageSlider

import cv2 as cv

PRELOAD_MODELS = False

if PRELOAD_MODELS:
    root_segmentor = RootSegmentor()
    
    
def process(input_img, model_type):
    
    print(model_type)
    
    if PRELOAD_MODELS:
        global root_segmentor
    else:
        root_segmentor = RootSegmentor(model_type)
        
    result = root_segmentor.predict(input_img)
    
    return result

def just_show(files, should_process, model_type):
    
    imgs = []
    
    img = merge_images(files)
    
    
    
    imgs.append(img)
    
    if should_process:    
        root_segmentor = RootSegmentor(model_type)
    
    results = []

    for file in files:
        print(type(file))
        print(file)
        img = cv.imread(file)
        img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
        #imgs.append(img)
        
        if should_process: 
        
            result = root_segmentor.predict(img)
            results.append(result)
            #imgs.append(results)
        
    if should_process: 
        img_res = merge_images(results)
        imgs.append(img_res)

    return imgs

def slider_test(img1, img2):
 
    return [img1,img2]

def download_result():
    
    #print(filepath)
    return
    

def gui():

  with gr.Blocks(title="Root analysis", theme=gr.themes.Soft()) as demo:

    big_block = gr.HTML("""

    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: white
            margin: 0;
        }

        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 5px;
            color: #fff;
        }

        hr {
            border: 1px solid #ddd;
            margin: 5px;
        }

    </style>

    <header>
        <div style="display: flex; align-items: center;">
            <div style="text-align: left;">
            <h1>Root Analysis</h1>
            <p>Root segmentation using underground root scanner images.</p>
            <h3>Tropical Forages Program</h3>
            <p><b>Authors: </b>Andres Felipe Ruiz-Hurtado, Juan Andrés Cardoso Arango</p>
            <p></p>            
        </div>
        </div>
        <div style="background-color: white; padding: 5px; border-radius: 15px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
                        <img src="https://alliancebioversityciat.org/sites/default/files/styles/1920_scale/public/images/Alliance%20Logo%20Refresh-color.jpg" alt="Logo" width="200" height="100">
                    </div>
    </header>   
    
    """)
    
    #<iframe style="height:600px;width: 100%;" src="/file=slides.html" title="description"></iframe>

    
    #<iframe style="height:600px;width: 100%;" src="https://revealjs.com/demo/?view" title="description"></iframe>

    with gr.Tab("Single Image"):
    
        model_selector = gr.Dropdown(
                ["segroot_finetuned", "segroot", "segroot_finetuned_dec", "seg_model"], label="Model"
                , info="AI model"
                ,value="segroot_finetuned"
            )

        input_img=gr.Image(render=False)
        output_img=gr.Image(render=False)

        gr.Interface(
            fn=process,
            inputs=[input_img,model_selector],
            outputs=output_img,
            examples=[["example_1.jpg"],["example_2.jpg"],["example_3.jpg"]]
        )

        #examples = gr.Examples([["Chicago"], ["Little Rock"], ["San Francisco"]], textbox)

        with gr.Row():
            img_comp = ImageSlider(label="Root Segmentation")
        with gr.Row():
            compare_button = gr.Button("Compare")
            compare_button.click(fn=slider_test, inputs=[input_img,output_img], outputs=img_comp, api_name="slider_test")
        
    with gr.Tab("Multiple Images"):

    #img_comp = ImageSlider(label="Blur image", type="pil")
    
        gallery = gr.Gallery(show_fullscreen_button=True, render=False) 
        
        gr.Interface(
            fn=just_show
            ,inputs=[gr.File(file_count="multiple"),gr.Checkbox(label="Process", info="Check if you want to process"),model_selector]
            ,outputs= gallery
            , examples=[[["example_1.jpg", "example_2.jpg", "example_3.jpg"]]]
        )

    with gr.Tab("Compare"):

        img_comp = ImageSlider(label="Root Segmentation")
        img_comp.upload(inputs=img_comp, outputs=img_comp)

    
    #d = gr.DownloadButton("Download the file")
    #d.click(download_result, gallery, None)
    
    # with gr.Row():    
    #     img1=gr.Image()
    #     img2=gr.Image()
    # with gr.Row():
    #     img_comp = ImageSlider(label="Blur image", type="pil")
    # with gr.Row():
    #     compare_button = gr.Button("Compare")
    #     compare_button.click(fn=slider_test, inputs=[img1,img2], outputs=img_comp, api_name="slider_test")
    
    # with gr.Group():
    #     img_comp = ImageSlider(label="Blur image", type="pil")
    #     #img1.upload(slider_test, inputs=[img1,img2], outputs=img_comp)
    #     gr.Interface(slider_test, inputs=[img1,img2], outputs=img_comp)
    
    demo.launch(allowed_paths=["logo.png"], share=False)

if __name__ == "__main__":
    gui()