Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,228 +0,0 @@
|
|
| 1 |
-
import gradio as gr # Gradio package for interface
|
| 2 |
-
import sys # System package for path dependencies
|
| 3 |
-
sys.path.append('Interface_Dependencies')
|
| 4 |
-
sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/Interface_Dependencies')
|
| 5 |
-
sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/yolov7-main')
|
| 6 |
-
sys.path.append('./') # to run '$ python *.py' files in subdirectories
|
| 7 |
-
|
| 8 |
-
from run_methods import run_all, correct_video
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Gradio Interface Code
|
| 12 |
-
with gr.Blocks(title="YOLO7 Interface",theme=gr.themes.Base()) as demo:
|
| 13 |
-
gr.Markdown(
|
| 14 |
-
"""
|
| 15 |
-
# Image & Video Interface for YOLO7 Model
|
| 16 |
-
Upload your own image or video and watch YOLO7 try to guess what it is!
|
| 17 |
-
""")
|
| 18 |
-
# For for input & output settings
|
| 19 |
-
with gr.Row() as file_settings:
|
| 20 |
-
# Allows choice for uploading image or video [for all]
|
| 21 |
-
file_type = gr.Radio(label="File Type",info="Choose 'Image' if you are uploading an image, Choose 'Video' if you are uploading a video",
|
| 22 |
-
choices=['Image','Video'],value='Image',show_label=True,interactive=True,visible=True)
|
| 23 |
-
# Allows choice of source, from computer or webcam [for all]
|
| 24 |
-
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",
|
| 25 |
-
choices=['Computer','Webcam'],value='Computer',show_label=True,interactive=True,visible=True)
|
| 26 |
-
# Allows choice of which convolutional layer to show (1-17) [only for images]
|
| 27 |
-
conv_layer = gr.Slider(label="Convolution Layer",info="Choose a whole number from 1 to 17 to see the corresponding convolutional layer",
|
| 28 |
-
minimum=1,maximum=17,value=1,interactive=True,step=1,show_label=True)
|
| 29 |
-
# Allows choice if video from webcam is streaming or uploaded [only for webcam videos]
|
| 30 |
-
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)
|
| 31 |
-
# Allows choice of which smooth gradient output to show (1-3) [only for images]
|
| 32 |
-
output_map = gr.Slider(label="Map Output Number",info="Choose a whole number from 1 to 3 to see the corresponding attribution map",
|
| 33 |
-
minimum=1,maximum=3,value=1,interactive=True,step=1,show_label=True)
|
| 34 |
-
# For all inputs & outputs
|
| 35 |
-
with gr.Row() as inputs_outputs:
|
| 36 |
-
# Default input image: Visible, Upload from computer
|
| 37 |
-
input_im = gr.Image(source="upload",type='filepath',label="Input Image",
|
| 38 |
-
show_download_button=True,show_share_button=True,interactive=True,visible=True)
|
| 39 |
-
# Default Boxed output image: Visible
|
| 40 |
-
output_box_im = gr.Image(type='filepath',label="Output Image",
|
| 41 |
-
show_download_button=True,show_share_button=True,interactive=False,visible=True)
|
| 42 |
-
# Defualt Convolutional output image: Visible
|
| 43 |
-
output_conv_im = gr.Image(type='filepath',label="Output Convolution",
|
| 44 |
-
show_download_button=True,show_share_button=True,interactive=False,visible=True)
|
| 45 |
-
# Default Gradient output image: Visible
|
| 46 |
-
output_grad_im = gr.Image(type='filepath',label="Output Smooth Gradient",
|
| 47 |
-
show_download_button=True,show_share_button=True,interactive=False,visible=True)
|
| 48 |
-
# Default label output textbox: Visible
|
| 49 |
-
labels = gr.Textbox(label='Top Predictions', value = "")
|
| 50 |
-
# Default time output textbox: Visible
|
| 51 |
-
formatted_time = gr.Textbox(label = 'Time to Run in Seconds:', value = "")
|
| 52 |
-
|
| 53 |
-
# Default input video: Not visible, Upload from computer
|
| 54 |
-
input_vid = gr.Video(source="upload",label="Input Video",
|
| 55 |
-
show_share_button=True,interactive=True,visible=False)
|
| 56 |
-
# Default Boxed output video: Not visible
|
| 57 |
-
output_box_vid = gr.Video(label="Output Video",show_share_button=True,visible=False)
|
| 58 |
-
|
| 59 |
-
# List of components for clearing
|
| 60 |
-
clear_comp_list = [input_im, output_box_im, output_conv_im, output_grad_im, labels, formatted_time, input_vid, output_box_vid]
|
| 61 |
-
|
| 62 |
-
# For start & clear buttons
|
| 63 |
-
with gr.Row() as buttons:
|
| 64 |
-
start_but = gr.Button(label="Start")
|
| 65 |
-
clear_but = gr.ClearButton(value='Clear All',components=clear_comp_list,
|
| 66 |
-
interactive=True,visible=True)
|
| 67 |
-
|
| 68 |
-
# For model settings
|
| 69 |
-
with gr.Row() as model_settings:
|
| 70 |
-
# Pixel size of the inference [Possibly useless, may remove]
|
| 71 |
-
inf_size = gr.Number(label='Inference Size (pixels)',value=640,precision=0)
|
| 72 |
-
# Object confidence threshold
|
| 73 |
-
obj_conf_thr = gr.Number(label='Object Confidence Threshold',value=0.25)
|
| 74 |
-
# Intersection of union threshold
|
| 75 |
-
iou_thr = gr.Number(label='IOU threshold for NMS',value=0.45)
|
| 76 |
-
# Agnostic NMS boolean
|
| 77 |
-
agnostic_nms = gr.Checkbox(label='Agnostic NMS',value=True)
|
| 78 |
-
# Normailze gradient boolean
|
| 79 |
-
norm = gr.Checkbox(label='Normalize Gradient',value=False,visible=True)
|
| 80 |
-
|
| 81 |
-
def change_file_type(file, source, is_stream):
|
| 82 |
-
"""
|
| 83 |
-
Changes the visible components of the gradio interface
|
| 84 |
-
|
| 85 |
-
Args:
|
| 86 |
-
file (str): Type of the file (image or video)
|
| 87 |
-
source (str): If the file is uploaded or from webcam
|
| 88 |
-
is_stream (bool): If the video is streaming or uploaded
|
| 89 |
-
|
| 90 |
-
Returns:
|
| 91 |
-
Dictionary: Each component of the interface that needs to be updated.
|
| 92 |
-
"""
|
| 93 |
-
if file == "Image":
|
| 94 |
-
if source == "Computer":
|
| 95 |
-
return {
|
| 96 |
-
conv_layer: gr.Slider(visible=True),
|
| 97 |
-
video_stream: gr.Checkbox(visible=False, value=False),
|
| 98 |
-
output_map: gr.Slider(visible=True),
|
| 99 |
-
input_im: gr.Image(source="upload",type='filepath',label="Input Image",
|
| 100 |
-
show_download_button=True,show_share_button=True,interactive=True,visible=True,streaming=False),
|
| 101 |
-
output_box_im: gr.Image(visible=True),
|
| 102 |
-
output_conv_im: gr.Image(visible=True),
|
| 103 |
-
output_grad_im: gr.Image(visible=True),
|
| 104 |
-
input_vid: gr.Video(visible=False),
|
| 105 |
-
output_box_vid: gr.Video(visible=False),
|
| 106 |
-
norm: gr.Checkbox(visible=True),
|
| 107 |
-
labels: gr.Textbox(visible=True),
|
| 108 |
-
formatted_time: gr.Textbox(visible=True)
|
| 109 |
-
}
|
| 110 |
-
elif source == "Webcam":
|
| 111 |
-
return {
|
| 112 |
-
conv_layer: gr.Slider(visible=True),
|
| 113 |
-
video_stream: gr.Checkbox(visible=False, value=False),
|
| 114 |
-
output_map: gr.Slider(visible=True),
|
| 115 |
-
input_im: gr.Image(type='pil',source="webcam",label="Input Image",
|
| 116 |
-
visible=True,interactive=True,streaming=False),
|
| 117 |
-
output_box_im: gr.Image(visible=True),
|
| 118 |
-
output_conv_im: gr.Image(visible=True),
|
| 119 |
-
output_grad_im: gr.Image(visible=True),
|
| 120 |
-
input_vid: gr.Video(visible=False),
|
| 121 |
-
output_box_vid: gr.Video(visible=False),
|
| 122 |
-
norm: gr.Checkbox(visible=True),
|
| 123 |
-
labels: gr.Textbox(visible=True),
|
| 124 |
-
formatted_time: gr.Textbox(visible=True)
|
| 125 |
-
}
|
| 126 |
-
elif file == "Video":
|
| 127 |
-
if source == "Computer":
|
| 128 |
-
return {
|
| 129 |
-
conv_layer: gr.Slider(visible=False),
|
| 130 |
-
video_stream: gr.Checkbox(visible=False, value=False),
|
| 131 |
-
output_map: gr.Slider(visible=False),
|
| 132 |
-
input_im: gr.Image(visible=False,streaming=False),
|
| 133 |
-
output_box_im: gr.Image(visible=False),
|
| 134 |
-
output_conv_im: gr.Image(visible=False),
|
| 135 |
-
output_grad_im: gr.Image(visible=False),
|
| 136 |
-
input_vid: gr.Video(source="upload",label="Input Video",
|
| 137 |
-
show_share_button=True,interactive=True,visible=True),
|
| 138 |
-
output_box_vid: gr.Video(label="Output Video",show_share_button=True,visible=True),
|
| 139 |
-
norm: gr.Checkbox(visible=False),
|
| 140 |
-
labels: gr.Textbox(visible=False),
|
| 141 |
-
formatted_time: gr.Textbox(visible=False)
|
| 142 |
-
}
|
| 143 |
-
elif source == "Webcam":
|
| 144 |
-
if is_stream:
|
| 145 |
-
return {
|
| 146 |
-
conv_layer: gr.Slider(visible=False),
|
| 147 |
-
video_stream: gr.Checkbox(visible=True),
|
| 148 |
-
output_map: gr.Slider(visible=False),
|
| 149 |
-
input_im: gr.Image(type='pil',source="webcam",label="Input Image",
|
| 150 |
-
streaming=True,visible=True,interactive=True),
|
| 151 |
-
output_box_im: gr.Image(visible=True),
|
| 152 |
-
output_conv_im: gr.Image(visible=False),
|
| 153 |
-
output_grad_im: gr.Image(visible=False),
|
| 154 |
-
input_vid: gr.Video(visible=False),
|
| 155 |
-
output_box_vid: gr.Video(visible=False),
|
| 156 |
-
norm: gr.Checkbox(visible=False),
|
| 157 |
-
labels: gr.Textbox(visible=False),
|
| 158 |
-
formatted_time: gr.Textbox(visible=False)
|
| 159 |
-
}
|
| 160 |
-
elif not is_stream:
|
| 161 |
-
return {
|
| 162 |
-
conv_layer: gr.Slider(visible=False),
|
| 163 |
-
video_stream: gr.Checkbox(visible=True, value=False),
|
| 164 |
-
output_map: gr.Slider(visible=False),
|
| 165 |
-
input_im: gr.Image(visible=False,streaming=False),
|
| 166 |
-
output_box_im: gr.Image(visible=False),
|
| 167 |
-
output_conv_im: gr.Image(visible=False),
|
| 168 |
-
output_grad_im: gr.Image(visible=False),
|
| 169 |
-
input_vid: gr.Video(label="Input Video",source="webcam",
|
| 170 |
-
show_share_button=True,interactive=True,visible=True),
|
| 171 |
-
output_box_vid: gr.Video(label="Output Video",show_share_button=True,visible=True),
|
| 172 |
-
norm: gr.Checkbox(visible=False),
|
| 173 |
-
labels: gr.Textbox(visible=False),
|
| 174 |
-
formatted_time: gr.Textbox(visible=False)
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
def change_conv_layer(layer):
|
| 178 |
-
"""
|
| 179 |
-
Changes the shown convolutional output layer based on gradio slider
|
| 180 |
-
|
| 181 |
-
Args:
|
| 182 |
-
layer (int): The layer to show
|
| 183 |
-
|
| 184 |
-
Returns:
|
| 185 |
-
str: The file path of the output image
|
| 186 |
-
"""
|
| 187 |
-
return "outputs\\runs\\detect\\exp\\layers\\layer" + str(int(int(layer) - 1)) + '.jpg'
|
| 188 |
-
|
| 189 |
-
def change_output_num(number):
|
| 190 |
-
return "outputs\\runs\\detect\\exp\\smoothGrad" + str(int(int(number) -1)) + '.jpg'
|
| 191 |
-
|
| 192 |
-
# List of gradio components that change during method "change_file_type"
|
| 193 |
-
change_comp_list = [conv_layer, video_stream, output_map,
|
| 194 |
-
input_im, output_box_im, output_conv_im, output_grad_im,
|
| 195 |
-
input_vid, output_box_vid, norm, labels, formatted_time]
|
| 196 |
-
# List of gradio components that are input into the run_all method (when start button is clicked)
|
| 197 |
-
run_inputs = [file_type, input_im, input_vid, source_type,
|
| 198 |
-
inf_size, obj_conf_thr, iou_thr, conv_layer,
|
| 199 |
-
agnostic_nms, output_map, video_stream, norm]
|
| 200 |
-
# List of gradio components that are output from the run_all method (when start button is clicked)
|
| 201 |
-
run_outputs = [output_box_im, output_conv_im, output_grad_im, labels, formatted_time, output_box_vid]
|
| 202 |
-
|
| 203 |
-
# When these settings are changed, the change_file_type method is called
|
| 204 |
-
file_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream], outputs=change_comp_list)
|
| 205 |
-
source_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream], outputs=change_comp_list)
|
| 206 |
-
video_stream.input(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream], outputs=change_comp_list)
|
| 207 |
-
# When start button is clicked, the run_all method is called
|
| 208 |
-
start_but.click(run_all, inputs=run_inputs, outputs=run_outputs)
|
| 209 |
-
# When video is uploaded, the correct_video method is called
|
| 210 |
-
input_vid.upload(correct_video, inputs=[input_vid], outputs=[input_vid])
|
| 211 |
-
# When the convolutional layer setting is changed, the change_conv_layer method is called
|
| 212 |
-
conv_layer.input(change_conv_layer, conv_layer, output_conv_im)
|
| 213 |
-
# When the stream setting is true, run the stream
|
| 214 |
-
input_im.stream(run_all, inputs=run_inputs, outputs=run_outputs)
|
| 215 |
-
# When the gradient number is changed, the change_output_num method is called
|
| 216 |
-
output_map.input(change_output_num, output_map, output_grad_im)
|
| 217 |
-
# When the demo is first started, run the change_file_type method to ensure default settings
|
| 218 |
-
demo.load(change_file_type, show_progress=True, inputs=[file_type, source_type, video_stream], outputs=change_comp_list)
|
| 219 |
-
|
| 220 |
-
if __name__== "__main__" :
|
| 221 |
-
# If True, it launches Gradio interface
|
| 222 |
-
# If False, it runs without the interface
|
| 223 |
-
if True:
|
| 224 |
-
# demo.queue().launch(share=True)
|
| 225 |
-
demo.queue().launch()
|
| 226 |
-
else:
|
| 227 |
-
# run_image("inference\\images\\bus.jpg","Computer",640,0.45,0.25,1,True)
|
| 228 |
-
run_video("0", "Webcam", 640, 0.25, 0.45, True, True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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