File size: 45,079 Bytes
c6baa40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f62c8b
c6baa40
 
 
 
 
 
 
 
 
 
 
6f62c8b
c6baa40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
import sys          # System package for path dependencies
sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/Interface_Dependencies')
sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/Interface')
sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/yolov7-main')
sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface')
sys.path.append('Interface_Dependencies')
sys.path.append('Interface')

from run_methods import run_all, correct_video
import gradio as gr # Gradio package for interface

# Gradio Interface Code
def build_detect_interface():
    with gr.Blocks(title="yolov7 Interface",theme=gr.themes.Base()) as demo:
        gr.Markdown(
        """
        # Image & Video Interface for yolov7 Model
        ### Upload your own image or video and watch yolov7 try to guess what it is!
        """)
        # Row for for input & output settings
        with gr.Row() as file_settings:
            # Allows the user to toggle between the advanced and simple interfaces
            skill_type = gr.Radio(label="Skill Type",info="Choose 'Advanced' if you would like to use the advanced interface, Choose 'Simple' if you would like that version",
                                choices=['Advanced','Simple', 'Help'],value='Simple',show_label=True,interactive=True,visible=True)
            # Allows choice for uploading image or video [for all]
            file_type = gr.Radio(label="File Type",info="Choose 'Image' if you are uploading an image, Choose 'Video' if you are uploading a video",
                                choices=['Image','Video'],value='Image',show_label=True,interactive=True,visible=True)
            # Allows choice of source, from computer or webcam [for all]
            source_type = gr.Radio(label="Source Type",info="Choose 'Computer' if you are uploading from your computer, Choose 'Webcam' if you would like to use your webcam",
                                choices=['Computer','Webcam'],value='Computer',show_label=True,interactive=True,visible=True)
            # Allows choice of which convolutional layer to show (1-17) [only for images]
            conv_layer = gr.Slider(label="Convolution Layer",info="Choose a whole number from 1 to 17 to see the corresponding convolutional layer",
                                minimum=1,maximum=17,value=1,interactive=True,step=1,show_label=True)
            # Allows choice if video from webcam is streaming or uploaded [only for webcam videos]
            video_stream = gr.Checkbox(label="Stream from webcam?",info="Check this box if you would like to stream from your webcam",value=False,show_label=True,interactive=True,visible=False)
            # Allows choice of which smooth gradient output to show (1-3) [only for images]
            output_map = gr.Slider(label="Map Output Number",info="Choose a whole number from 1 to 3 to see the corresponding attribution map",
                                minimum=1,maximum=3,value=1,interactive=True,step=1,show_label=True)

        # Row for all inputs & outputs
        with gr.Row() as inputs_outputs:
            # Default input image: Visible, Upload from computer
            input_im = gr.Image(source="upload",type='filepath',label="Input Image",
                                show_download_button=True,show_share_button=True,interactive=True,visible=False)
            # Default Boxed output image: Visible
            output_box_im = gr.Image(type='filepath',label="Output Image",
                                show_download_button=True,show_share_button=True,interactive=False,visible=False)
            # Defualt Convolutional output image: Visible
            output_conv_im = gr.Image(type='filepath',label="Output Convolution",
                                    show_download_button=True,show_share_button=True,interactive=False,visible=False)
            # Default Gradient output image: Visible
            output_grad_im = gr.Image(type='filepath',label="Output Smooth Gradient",
                                    show_download_button=True,show_share_button=True,interactive=False,visible=False)
            # Default label output textbox: Visible
            labels = gr.Textbox(label='Top Predictions:', value = "")
            # Default plaus output textbox: Visible
            plaus = gr.Textbox(label = "Plausibility Score:", value="")
            # Default time output textbox: Visible
            formatted_time = gr.Textbox(label = 'Total and Detection Time in Seconds:', value = "",scale=6)
            
            # Default input video: Not visible, Upload from computer
            input_vid =  gr.Video(source="upload",label="Input Video",
                                show_share_button=True,interactive=True,visible=False)
            # Default Boxed output video: Not visible
            output_box_vid = gr.Video(label="Output Video",show_share_button=True,visible=False)
            #An option to bring up a help menu
            help_box = gr.Textbox(lines=5, label="Help Box", visible=False)
        
        # List of components for clearing
        clear_comp_list = [input_im, output_box_im, output_conv_im, output_grad_im, labels, plaus, formatted_time, input_vid, output_box_vid]
        
        # Row for start, clear and demo buttons
        with gr.Row() as buttons:
            start_but = gr.Button(value="Start")
            demo1_but = gr.Button(value="ImageDemo")
            demo2_but = gr.Button(value="VideoDemo")
            clear_but = gr.ClearButton(value='Clear All',components=clear_comp_list,
                    interactive=True,visible=True)

        # Row for model settings
        with gr.Row() as model_settings:
            # Pixel size of the inference [Possibly useless, may remove]
            inf_size = gr.Number(label='Inference Size (pixels)',value=640,precision=0,visible=False)
            # Object confidence threshold
            obj_conf_thr = gr.Number(label='Object Confidence Threshold',value=0.25,visible=False)
            # Intersection of union threshold
            iou_thr = gr.Number(label='IOU threshold for NMS',value=0.45,visible=False)
            # Agnostic NMS boolean
            agnostic_nms = gr.Checkbox(label='Agnostic NMS',value=True, visible=False)
            # Normailze gradient boolean
            norm = gr.Checkbox(label='Normalize Gradient',value=False,visible=False)
            # Weights File Upload
            weights = gr.File(label='Weights File',type='file',file_count='single',file_types=["pt"],value="weights/yolov7.pt",visible=False)   

        #Consider making this its own file
        def change_file_type(file, source, is_stream, skill_type):
            """
            Changes the visible components of the gradio interface

            Args:
                file (str): Type of the file (image or video)
                source (str): If the file is uploaded or from webcam
                is_stream (bool): If the video is streaming or uploaded

            Returns:
                Dictionary: Each component of the interface that needs to be updated.
            """
            help_string = ("Variables within the interface: \n \
                            Simple Mode: \n \
                            Skill Type - The mode the interface will be set to. Simple is recommended for new users! \n \
                            File Type - Set the interface for image or video input \n \
                            Source Type - Set the input for media to computer(will allow uploads) or webcam(will use your devices webcam) \n \
                            stream from webcam? - Toggle the ability to livestream video from webcam \n \
                            Start - Run the interface with the set parameters and media \n \
                            ImageDemo - Run a demo to test the interface with a stock image \n \
                            VideoDemo - Run a demo to test the interface with a stock video \n \
                            ClearAll - Clear all of the input/output figures on the interface \n \
                            Advanced Mode: \n \
                            Convolution Layer - The convolutional layor extracted from the neural network. There are 17 of these to scroll between. \n \
                            Map Output Number - The attribution map. 1 is bounding box, 2 is object, and 3 is class. Currently, this is from the model not loss. \n \
                            Top Predictions - A list of the predictions along with their confidence percentage \n \
                            Plausibility Score - The measurement of how much the predicted bounding boxes overlap with the attributions generated (bounding box model output) \n \
                            Inference Size - The resolution of the image that will be interpreted (default is 640 by 640 pixels) \n \
                            Object Confidence Threshold - The minimum confidence required to display a bounding box \n \
                            IOU Threshold for NMS - The minimum confidence required for a bounding box to surpass another in cases of multiple predictions \n \
                            Agnostic NMS - Enable non max suppression to use the bounding boxes with the highest confidence in prediction \n \
                            Normalize Gradient - Scale the gradient vector to have a magnitude of 1 \n \
                            Weights File - A .pt file that contains the weight values that make the filter. This contains the classes and will greatly affect how an image or video is classified")

            if file == "Image":
                if source == "Computer":
                    if skill_type == "Advanced":
                        return {
                            conv_layer: gr.Slider(visible=True),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=True),
                            input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                    show_download_button=True,show_share_button=True,interactive=True,visible=True,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=True,scale=3),
                            output_conv_im: gr.Image(visible=True,scale=3),
                            output_grad_im: gr.Image(visible=True,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=True),
                            labels: gr.Textbox(visible=True,scale=1),
                            plaus: gr.Textbox(visible=True,scale=1),
                            formatted_time: gr.Textbox(visible=True,scale=1),
                            help_box: gr.Textbox(visible=False, value=help_string),
                            inf_size: gr.Number(visible=True),
                            obj_conf_thr: gr.Number(visible=True),
                            iou_thr: gr.Number(visible=True),
                            agnostic_nms: gr.Checkbox(visible=True),
                            weights: gr.File(visible=True)

                    }
                    elif skill_type == "Simple":
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                    show_download_button=True,show_share_button=True,interactive=True,visible=True,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=True,scale=3),
                            output_conv_im: gr.Image(visible=False,scale=3),
                            output_grad_im: gr.Image(visible=False,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=False),
                            labels: gr.Textbox(visible=False,scale=1),
                            plaus: gr.Textbox(visible=False,scale=1),
                            formatted_time: gr.Textbox(visible=False,scale=1),
                            help_box: gr.Textbox(visible=False, value=help_string),
                            inf_size: gr.Number(visible=False),
                            obj_conf_thr: gr.Number(visible=False),
                            iou_thr: gr.Number(visible=False),
                            agnostic_nms: gr.Checkbox(visible=False),
                            weights: gr.File(visible=False)
                    }
                    # Else its help
                    else:
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                    show_download_button=True,show_share_button=True,interactive=True,visible=False,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=False,scale=3),
                            output_conv_im: gr.Image(visible=False,scale=3),
                            output_grad_im: gr.Image(visible=False,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=False),
                            labels: gr.Textbox(visible=False,scale=1),
                            plaus: gr.Textbox(visible=False,scale=1),
                            formatted_time: gr.Textbox(visible=False,scale=1),
                            help_box: gr.Textbox(visible=True, value=help_string,scale=10),
                            inf_size: gr.Number(visible=False),
                            obj_conf_thr: gr.Number(visible=False),
                            iou_thr: gr.Number(visible=False),
                            agnostic_nms: gr.Checkbox(visible=False),
                            weights: gr.File(visible=False)
                        }
                elif source == "Webcam":
                    if skill_type == "Advanced":
                        return {
                            conv_layer: gr.Slider(visible=True),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=True),
                            input_im: gr.Image(type='pil',source="webcam",label="Input Image",
                                            visible=True,interactive=True,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=True,scale=3),
                            output_conv_im: gr.Image(visible=True,scale=3),
                            output_grad_im: gr.Image(visible=True,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=True),
                            labels: gr.Textbox(visible=True),
                            plaus: gr.Textbox(visible=True),
                            formatted_time: gr.Textbox(visible=True),
                            help_box: gr.Textbox(visible=False, value=help_string),
                            inf_size: gr.Number(visible=True),
                            obj_conf_thr: gr.Number(visible=True),
                            iou_thr: gr.Number(visible=True),
                            agnostic_nms: gr.Checkbox(visible=True),
                            weights: gr.File(visible=True)
                    }
                    elif skill_type == "Simple":
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(type='pil',source="webcam",label="Input Image",
                                            visible=True,interactive=True,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=True,scale=3),
                            output_conv_im: gr.Image(visible=False,scale=3),
                            output_grad_im: gr.Image(visible=False,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=False),
                            labels: gr.Textbox(visible=False),
                            plaus: gr.Textbox(visible=False),
                            formatted_time: gr.Textbox(visible=False),
                            help_box: gr.Textbox(visible=False, value=help_string),
                            inf_size: gr.Number(visible=False),
                            obj_conf_thr: gr.Number(visible=False),
                            iou_thr: gr.Number(visible=False),
                            agnostic_nms: gr.Checkbox(visible=False),
                            weights: gr.File(visible=False)
                    }
                    else:
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                    show_download_button=True,show_share_button=True,interactive=True,visible=False,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=False,scale=3),
                            output_conv_im: gr.Image(visible=False,scale=3),
                            output_grad_im: gr.Image(visible=False,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=False),
                            labels: gr.Textbox(visible=False,scale=1),
                            plaus: gr.Textbox(visible=False,scale=1),
                            formatted_time: gr.Textbox(visible=False,scale=1),
                            help_box: gr.Textbox(visible=True, value=help_string,scale=10),
                            inf_size: gr.Number(visible=False),
                            obj_conf_thr: gr.Number(visible=False),
                            iou_thr: gr.Number(visible=False),
                            agnostic_nms: gr.Checkbox(visible=False),
                            weights: gr.File(visible=False)
                        }
            elif file == "Video":
                if source == "Computer":
                    if skill_type == "Advanced":
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(visible=False,streaming=False),
                            output_box_im: gr.Image(visible=False),
                            output_conv_im: gr.Image(visible=False),
                            output_grad_im: gr.Image(visible=False),
                            input_vid: gr.Video(source="upload",label="Input Video",
                                    show_share_button=True,interactive=True,visible=True,scale=6),
                            output_box_vid: gr.Video(label="Output Video",show_share_button=True,visible=True,scale=6),
                            norm: gr.Checkbox(visible=True),
                            labels: gr.Textbox(visible=False),
                            plaus: gr.Textbox(visible=False),
                            formatted_time: gr.Textbox(visible=True,scale=1),
                            help_box: gr.Textbox(visible=False, value=help_string,scale=10),
                            inf_size: gr.Number(visible=True),
                            obj_conf_thr: gr.Number(visible=True),
                            iou_thr: gr.Number(visible=True),
                            agnostic_nms: gr.Checkbox(visible=True),
                            weights: gr.File(visible=True)
                    }
                    elif skill_type == "Simple":
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(visible=False,streaming=False),
                            output_box_im: gr.Image(visible=False),
                            output_conv_im: gr.Image(visible=False),
                            output_grad_im: gr.Image(visible=False),
                            input_vid: gr.Video(source="upload",label="Input Video",
                                    show_share_button=True,interactive=True,visible=True,scale=6),
                            output_box_vid: gr.Video(label="Output Video",show_share_button=True,visible=True,scale=6),
                            norm: gr.Checkbox(visible=False),
                            labels: gr.Textbox(visible=False),
                            plaus: gr.Textbox(visible=False),
                            formatted_time: gr.Textbox(visible=False),
                            help_box: gr.Textbox(visible=False, value=help_string),
                            inf_size: gr.Number(visible=False),
                            obj_conf_thr: gr.Number(visible=False),
                            iou_thr: gr.Number(visible=False),
                            agnostic_nms: gr.Checkbox(visible=False),
                            weights: gr.File(visible=False)
                    }
                    else:
                        return {
                            conv_layer: gr.Slider(visible=False),
                            video_stream: gr.Checkbox(visible=False, value=False),
                            output_map: gr.Slider(visible=False),
                            input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                    show_download_button=True,show_share_button=True,interactive=True,visible=False,streaming=False,scale=3),
                            output_box_im: gr.Image(visible=False,scale=3),
                            output_conv_im: gr.Image(visible=False,scale=3),
                            output_grad_im: gr.Image(visible=False,scale=3),
                            input_vid: gr.Video(visible=False),
                            output_box_vid: gr.Video(visible=False),
                            norm: gr.Checkbox(visible=False),
                            labels: gr.Textbox(visible=False,scale=1),
                            plaus: gr.Textbox(visible=False,scale=1),
                            formatted_time: gr.Textbox(visible=False,scale=1),
                            help_box: gr.Textbox(visible=True, value=help_string,scale=10),
                            inf_size: gr.Number(visible=False),
                            obj_conf_thr: gr.Number(visible=False),
                            iou_thr: gr.Number(visible=False),
                            agnostic_nms: gr.Checkbox(visible=False),
                            weights: gr.File(visible=False)
                        }

                #TODO-clinic: Fix possible streaming issues
                elif source == "Webcam":
                    if is_stream:
                        if skill_type == "Advanced":
                            return {
                                conv_layer: gr.Slider(visible=False),
                                video_stream: gr.Checkbox(visible=True),
                                output_map: gr.Slider(visible=False),
                                input_im: gr.Image(type='pil',source="webcam",label="Input Image",
                                                streaming=True,visible=True,interactive=True,scale=6),
                                output_box_im: gr.Image(visible=True,scale=6),
                                output_conv_im: gr.Image(visible=False), #TODO-clinic: We may not want to hide all of these
                                output_grad_im: gr.Image(visible=False),
                                input_vid: gr.Video(visible=False),
                                output_box_vid: gr.Video(visible=False),
                                norm: gr.Checkbox(visible=True),
                                labels: gr.Textbox(visible=False),
                                plaus: gr.Textbox(visible=False),
                                formatted_time: gr.Textbox(visible=True,scale=1),
                                help_box: gr.Textbox(visible=False, value=help_string),
                                inf_size: gr.Number(visible=True),
                                obj_conf_thr: gr.Number(visible=True),
                                iou_thr: gr.Number(visible=True),
                                agnostic_nms: gr.Checkbox(visible=True),
                                weights: gr.File(visible=True)
                            }
                        elif skill_type == "Simple":
                            return {
                                conv_layer: gr.Slider(visible=False),
                                video_stream: gr.Checkbox(visible=True),
                                output_map: gr.Slider(visible=False),
                                input_im: gr.Image(type='pil',source="webcam",label="Input Image",
                                                streaming=True,visible=True,interactive=True,scale=6),
                                output_box_im: gr.Image(visible=True,scale=6),
                                output_conv_im: gr.Image(visible=False),
                                output_grad_im: gr.Image(visible=False),
                                input_vid: gr.Video(visible=False),
                                output_box_vid: gr.Video(visible=False),
                                norm: gr.Checkbox(visible=False),
                                labels: gr.Textbox(visible=False),
                                plaus: gr.Textbox(visible=False),
                                formatted_time: gr.Textbox(visible=False),
                                help_box: gr.Textbox(visible=False, value=help_string),
                                inf_size: gr.Number(visible=False),
                                obj_conf_thr: gr.Number(visible=False),
                                iou_thr: gr.Number(visible=False),
                                agnostic_nms: gr.Checkbox(visible=False),
                                weights: gr.File(visible=False)
                            }
                        else:
                            return {
                                conv_layer: gr.Slider(visible=False),
                                video_stream: gr.Checkbox(visible=False, value=False),
                                output_map: gr.Slider(visible=False),
                                input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                        show_download_button=True,show_share_button=True,interactive=True,visible=False,streaming=False,scale=3),
                                output_box_im: gr.Image(visible=False,scale=3),
                                output_conv_im: gr.Image(visible=False,scale=3),
                                output_grad_im: gr.Image(visible=False,scale=3),
                                input_vid: gr.Video(visible=False),
                                output_box_vid: gr.Video(visible=False),
                                norm: gr.Checkbox(visible=False),
                                labels: gr.Textbox(visible=False,scale=1),
                                plaus: gr.Textbox(visible=False,scale=1),
                                formatted_time: gr.Textbox(visible=False,scale=1),
                                help_box: gr.Textbox(visible=True, value=help_string,scale=10),
                                inf_size: gr.Number(visible=False),
                                obj_conf_thr: gr.Number(visible=False),
                                iou_thr: gr.Number(visible=False),
                                agnostic_nms: gr.Checkbox(visible=False),
                                weights: gr.File(visible=False)
                        }
                    elif not is_stream:
                        if skill_type == "Advanced":
                            return {
                                #TODO-clinic: We may not want to hide all of these
                                conv_layer: gr.Slider(visible=False),
                                video_stream: gr.Checkbox(visible=True, value=False),
                                output_map: gr.Slider(visible=False),
                                input_im: gr.Image(visible=False,streaming=False),
                                output_box_im: gr.Image(visible=False),
                                output_conv_im: gr.Image(visible=False),
                                output_grad_im: gr.Image(visible=False),
                                input_vid: gr.Video(label="Input Video",source="webcam",
                                                    show_share_button=True,interactive=True,visible=True,scale=6),
                                output_box_vid: gr.Video(label="Output Video",show_share_button=True,visible=True,scale=6),
                                norm: gr.Checkbox(visible=True),
                                labels: gr.Textbox(visible=False),
                                plaus: gr.Textbox(visible=False),
                                formatted_time: gr.Textbox(visible=True,scale=1),
                                help_box: gr.Textbox(visible=False, value=help_string),
                                inf_size: gr.Number(visible=True),
                                obj_conf_thr: gr.Number(visible=True),
                                iou_thr: gr.Number(visible=True),
                                agnostic_nms: gr.Checkbox(visible=True),
                                weights: gr.File(visible=True)
                            }
                        elif skill_type == "Simple":
                            #TODO-clinic: Update for simple
                            return {
                                conv_layer: gr.Slider(visible=False),
                                video_stream: gr.Checkbox(visible=True, value=False),
                                output_map: gr.Slider(visible=False),
                                input_im: gr.Image(visible=False,streaming=False),
                                output_box_im: gr.Image(visible=False),
                                output_conv_im: gr.Image(visible=False),
                                output_grad_im: gr.Image(visible=False),
                                input_vid: gr.Video(label="Input Video",source="webcam",
                                                    show_share_button=True,interactive=True,visible=True,scale=6),
                                output_box_vid: gr.Video(label="Output Video",show_share_button=True,visible=True,scale=6),
                                norm: gr.Checkbox(visible=False),
                                labels: gr.Textbox(visible=False),
                                plaus: gr.Textbox(visible=False),
                                formatted_time: gr.Textbox(visible=False),
                                help_box: gr.Textbox(visible=False, value=help_string),
                                inf_size: gr.Number(visible=False),
                                obj_conf_thr: gr.Number(visible=False),
                                iou_thr: gr.Number(visible=False),
                                agnostic_nms: gr.Checkbox(visible=False),
                                weights: gr.File(visible=False)
                            }
                        else:
                            return {
                                conv_layer: gr.Slider(visible=False),
                                video_stream: gr.Checkbox(visible=False, value=False),
                                output_map: gr.Slider(visible=False),
                                input_im: gr.Image(source="upload",type='filepath',label="Input Image",
                                        show_download_button=True,show_share_button=True,interactive=True,visible=False,streaming=False,scale=3),
                                output_box_im: gr.Image(visible=False,scale=3),
                                output_conv_im: gr.Image(visible=False,scale=3),
                                output_grad_im: gr.Image(visible=False,scale=3),
                                input_vid: gr.Video(visible=False),
                                output_box_vid: gr.Video(visible=False),
                                norm: gr.Checkbox(visible=False),
                                labels: gr.Textbox(visible=False,scale=1),
                                plaus: gr.Textbox(visible=False,scale=1),
                                formatted_time: gr.Textbox(visible=False,scale=1),
                                help_box: gr.Textbox(visible=True, value=help_string,scale=10),
                                inf_size: gr.Number(visible=False),
                                obj_conf_thr: gr.Number(visible=False),
                                iou_thr: gr.Number(visible=False),
                                agnostic_nms: gr.Checkbox(visible=False),
                                weights: gr.File(visible=False)
                        }
                    
        def change_conv_layer(layer):
            """
            Changes the shown convolutional output layer based on gradio slider

            Args:
                layer (int): The layer to show

            Returns:
                str: The file path of the output image
            """
            return "outputs/runs/detect/exp/layers/layer" + str(int(int(layer) - 1)) + '.jpg'
        
        def change_output_num(number):
            """
            Changes the shown gradient map based on gradio slider

            Args:
                number (int): The gradient map to show

            Returns:
                str: The file path of the output image
            """
            return "outputs/runs/detect/exp/smoothGrad" + str(int(int(number) -1)) + '.jpg'
        
        def demo1():
            """
            Run a test image file as a demo 
            """
            #TODO-clinic - There may be a better way to do this (move to another file)
            file_type = gr.Radio(label="File Type",info="Choose 'Image' if you are uploading an image, Choose 'Video' if you are uploading a video",
                                choices=['Image','Video'],value='Image',show_label=True,interactive=True,visible=False)
            input_im = gr.Image(type='filepath',label="Input Image",value= "references/inference/images/bus.jpg",
                                show_download_button=True,show_share_button=True,interactive=True,visible=False)
            input_vid =  gr.Video(source="upload",label="Input Video",
                                show_share_button=True,interactive=True,visible=False)
            source_type = gr.Radio(label="Source Type",info="Choose 'Computer' if you are uploading from your computer, Choose 'Webcam' if you would like to use your webcam",
                                choices=['Computer','Webcam'],value='Computer',show_label=True,interactive=True,visible=False)
            inf_size = gr.Number(label='Inference Size (pixels)',value=640,precision=0,visible=False)
            obj_conf_thr = gr.Number(label='Object Confidence Threshold',value=0.25,visible=False)
            iou_thr = gr.Number(label='IOU threshold for NMS',value=0.45,visible=False)
            conv_layer = gr.Slider(label="Convolution Layer",info="Choose a whole number from 1 to 17 to see the corresponding convolutional layer",
                                minimum=1,maximum=17,value=1,interactive=True,step=1,show_label=True,visible=False)
            agnostic_nms = gr.Checkbox(label='Agnostic NMS',value=True,visible=False)
            output_map = gr.Slider(label="Map Output Number",info="Choose a whole number from 1 to 3 to see the corresponding attribution map",
                                minimum=1,maximum=3,value=1,interactive=True,step=1,show_label=True,visible=False)
            video_stream = gr.Checkbox(label="Stream from webcam?",info="Check this box if you would like to stream from your webcam",value=False,show_label=True,interactive=True,visible=False)
            norm = gr.Checkbox(label='Normalize Gradient',value=False,visible=False)
            weights = gr.File(label='Weights File',type='file',file_count='single',file_types=["pt"],value="weights/yolov7.pt",visible=False)
            skill_type = gr.Radio(label="Skill Type",info="Choose 'Advanced' if you would like to use the advanced interface, 'Simple' if you would like that version, or help for more info",
                                choices=['Advanced','Simple'],value='Advanced',show_label=True,interactive=True,visible=False)

            demo1_inputs = [file_type, input_im, input_vid, source_type, 
                    inf_size, obj_conf_thr, iou_thr, conv_layer, 
                    agnostic_nms, output_map, video_stream, norm, weights,skill_type]
            
            return demo1_inputs
        
        def demo2():
            """
            Run a test video file as a demo 
            """
            #TODO-clinic - There may be a better way to do this (move to another file)
            file_type = gr.Radio(label="File Type",info="Choose 'Image' if you are uploading an image, Choose 'Video' if you are uploading a video",
                                choices=['Image','Video'],value='Video',show_label=True,interactive=True,visible=False)
            input_im = gr.Image(type='filepath',label="Input Image",
                                show_download_button=True,show_share_button=True,interactive=True,visible=False)
            input_vid =  gr.Video(source="upload",label="Input Video",
                                show_share_button=True,interactive=True,visible=False,value= "references/inference/videos/ducks.mp4")
            source_type = gr.Radio(label="Source Type",info="Choose 'Computer' if you are uploading from your computer, Choose 'Webcam' if you would like to use your webcam",
                                choices=['Computer','Webcam'],value='Computer',show_label=True,interactive=True,visible=False)
            inf_size = gr.Number(label='Inference Size (pixels)',value=640,precision=0,visible=False)
            obj_conf_thr = gr.Number(label='Object Confidence Threshold',value=0.25,visible=False)
            iou_thr = gr.Number(label='IOU threshold for NMS',value=0.45,visible=False)
            conv_layer = gr.Slider(label="Convolution Layer",info="Choose a whole number from 1 to 17 to see the corresponding convolutional layer",
                                minimum=1,maximum=17,value=1,interactive=True,step=1,show_label=True,visible=False)
            agnostic_nms = gr.Checkbox(label='Agnostic NMS',value=True,visible=False)
            output_map = gr.Slider(label="Map Output Number",info="Choose a whole number from 1 to 3 to see the corresponding attribution map",
                                minimum=1,maximum=3,value=1,interactive=True,step=1,show_label=True,visible=False)
            video_stream = gr.Checkbox(label="Stream from webcam?",info="Check this box if you would like to stream from your webcam",value=False,show_label=True,interactive=True,visible=False)
            norm = gr.Checkbox(label='Normalize Gradient',value=False,visible=False)
            weights = gr.File(label='Weights File',type='file',file_count='single',file_types=["pt"],value="weights/yolov7.pt",visible=False)
            skill_type = gr.Radio(label="Skill Type",info="Choose 'Advanced' if you would like to use the advanced interface, Choose 'Simple' if you would like that version",
                                choices=['Advanced','Simple'],value='Advanced',show_label=True,interactive=True,visible=False)

            demo2_inputs = [file_type, input_im, input_vid, source_type, 
                    inf_size, obj_conf_thr, iou_thr, conv_layer, 
                    agnostic_nms, output_map, video_stream, norm, weights,skill_type]
            
            return demo2_inputs
    
        # List of gradio components that change during method "change_file_type"
        change_comp_list = [conv_layer, video_stream, output_map, 
                            input_im, output_box_im, output_conv_im, output_grad_im,
                            input_vid, output_box_vid, norm, labels, plaus, formatted_time, 
                            help_box, inf_size, obj_conf_thr, iou_thr, agnostic_nms, weights]
        # List of gradio components that are input into the run_all method (when start button is clicked)
        run_inputs = [file_type, input_im, input_vid, source_type, 
                    inf_size, obj_conf_thr, iou_thr, conv_layer, 
                    agnostic_nms, output_map, video_stream, norm, weights, skill_type] #TODO-clinic: Skill_type might be able to be removed here
        
        # List of gradio components that are output from the run_all method (when start button is clicked)
        run_outputs = [output_box_im, output_conv_im, output_grad_im, labels, plaus, formatted_time, output_box_vid, input_im, input_vid]
        
        # When these settings are changed, the change_file_type method is called
        file_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream, skill_type], outputs=change_comp_list)
        source_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream, skill_type], outputs=change_comp_list)
        video_stream.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream, skill_type], outputs=change_comp_list)
        skill_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream, skill_type], outputs=change_comp_list)
        # When start button is clicked, the run_all method is called
        start_but.click(run_all, inputs=run_inputs, outputs=run_outputs)
        # When demo1 button is clicked, run the function with stored params
        demo1_but.click(run_all, inputs=demo1(), outputs=run_outputs)
        # # When demo2 button is clicked, run the function with stored params
        demo2_but.click(run_all, inputs=demo2(), outputs=run_outputs)
        # When video is uploaded, the correct_video method is called
        input_vid.upload(correct_video, inputs=[input_vid], outputs=[input_vid])
        # When the convolutional layer setting is changed, the change_conv_layer method is called
        conv_layer.input(change_conv_layer, conv_layer, output_conv_im)
        # When the stream setting is true, run the stream
        input_im.stream(run_all, inputs=run_inputs, outputs=run_outputs)
        # When the gradient number is changed, the change_output_num method is called
        output_map.input(change_output_num, output_map, output_grad_im)
        # When the demo is first started, run the change_file_type method to ensure default settings
        demo.load(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream, skill_type], outputs=change_comp_list)
    return demo

# #Used for debugging
# if __name__== "__main__" :
#     # If True, it launches Gradio interface
#     # If False, it runs without the interface (for better debugging)
#     if True:
#         # demo.queue().launch(share=True)
#         demo.queue().launch()
#     else:
#         #Test with yolo (weights and normal photo)
#         run_all('Image','references\\inference\\images\\bus.jpg', 'None', 'Computer', 640,0.25,0.45,1,True,1,False,False,"yolov7.pt", "Advanced")
#         #Test drone (weights and drone photo) TODO: Weight set may not be included in the repo
#         #run_all('Image','references\\inference\\images\\V_DRONE_113_34.jpg', 'None', 'Computer', 640,0.25,0.45,1,True,1,False,False,"drone_63.pt", "Advanced")