Stop_Signs / app.py
mbrad's picture
Upload 4 files
247054e verified
raw
history blame
No virus
2.12 kB
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
NN = keras.models.load_model('/content/drive/MyDrive/Final_Project/model1.keras')
CNN = keras.models.load_model('/content/drive/MyDrive/Final_Project/model3.keras')
# Specify image module
input_module1 = gr.File(label = "Input Image File (Must be 128x128 Greyscale)")
# Specify method dropdown menu
input_module2 = gr.Dropdown(choices=['Convolutional Neural Network', 'Neural Network'], label = "Method")
# Specify output 1
output_module1 = gr.Plot(label = "Predicted Box")
def multi_inputs(input1, input2):
import numpy as np
## processing inputs
#input1= input1[:,:,0]
photo = plt.imread(input1)
Dcells = 128*128
if input2 == "Neural Network":
flattened = photo.reshape(1,Dcells)
predicted_box = NN.predict((flattened))
box_x = predicted_box[0,0]
box_y = predicted_box[0,1]
box_width = predicted_box[0,2]
box_height = predicted_box[0,3]
plt.imshow(photo, cmap='gray')
plt.gca().add_patch(matplotlib.patches.Rectangle((box_x, box_y), box_width, box_height, ec='r', fc='none'))
plt.gca().annotate("NN", xy = (0,0), xytext = (box_x, box_y+box_height+4), color='r',fontsize = 8)
else:
shaped = photo.reshape(1,128,128)
predicted_box = CNN.predict(shaped)
box_x = predicted_box[0,0]
box_y = predicted_box[0,1]
box_width = predicted_box[0,2]
box_height = predicted_box[0,3]
plt.imshow(photo, cmap='gray')
plt.gca().add_patch(matplotlib.patches.Rectangle((box_x, box_y), box_width, box_height, ec='r', fc='none'))
plt.gca().annotate("CNN", xy = (0,0), xytext = (box_x, box_y+box_height+4), color='r',fontsize = 8)
return plt
gr.Interface(fn=multi_inputs,
inputs=[input_module1, input_module2],
outputs=[output_module1]
).launch(debug = True)