lukelike1001's picture
updated title and description
7f946a3
raw
history blame
1.95 kB
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow import keras
# load the pre-trained model from the appropriate file path
def predict_plant(input_image):
model = tf.keras.models.load_model('lukelike1001/my_model')
# redefine values from the model
img_height = img_width = 180
class_names = ['bear_oak', 'boxelder', 'eastern_poison_ivy',
'eastern_poison_oak', 'fragrant_sumac',
'jack_in_the_pulpit', 'poison_sumac',
'virginia_creeper', 'western_poison_ivy',
'western_poison_oak']
# load the image into a variable
img = tf.keras.utils.load_img(
input_image, target_size=(img_height, img_width)
)
# convert the image into a tensor and create a batch for testing
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
# find the top three likeliest plants based on probabilities
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
top_three = np.array(score).argsort()[-3:][::-1]
numpy_array = score.numpy()
# convert the folder names into English words then return the three likeliest probabilities
output = []
for i in top_three:
words = class_names[i].split("_")
name = " ".join([word.capitalize() for word in words])
output.append(
"This image likely belongs to {} with {:.2f}% confidence."
.format(name, 100 * numpy_array[i])
)
return "\n".join(output)
# describe the model
title = "Leaftracker Interactive Model"
description = "Image Classification Model For Identifying Toxic Plants from their Non-Toxic Look-Alikes"
# launch the app
app = gr.Interface(
fn=predict_plant,
inputs=gr.Image(type="filepath"),
outputs="text",
flagging_options=["incorrect", "other"],
title=title,
description=description
)
app.launch()