Spaces:
Sleeping
Sleeping
File size: 1,951 Bytes
eb76dc9 1d9b055 eb76dc9 a23f77c 1d9b055 eb76dc9 a23f77c eb76dc9 a23f77c eb76dc9 1d9b055 eb76dc9 a23f77c eb76dc9 7f946a3 eb76dc9 |
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 |
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() |