Commit
·
eb76dc9
1
Parent(s):
f0458f8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
|
5 |
+
# load the pre-trained model from the appropriate file path
|
6 |
+
def predict_plant(image):
|
7 |
+
model = tf.keras.models.load_model('lukelike1001/saved_model')
|
8 |
+
|
9 |
+
# redefine values from the model
|
10 |
+
img_height = img_width = 180
|
11 |
+
class_names = ['bear_oak', 'boxelder', 'eastern_poison_ivy',
|
12 |
+
'eastern_poison_oak', 'fragrant_sumac',
|
13 |
+
'jack_in_the_pulpit', 'poison_sumac',
|
14 |
+
'virginia_creeper', 'western_poison_ivy',
|
15 |
+
'western_poison_oak']
|
16 |
+
path = image
|
17 |
+
|
18 |
+
# load the image into a variable
|
19 |
+
img = tf.keras.utils.load_img(
|
20 |
+
path, target_size=(img_height, img_width)
|
21 |
+
)
|
22 |
+
|
23 |
+
# convert the image into a tensor and create a batch for testing
|
24 |
+
img_array = tf.keras.utils.img_to_array(img)
|
25 |
+
img_array = tf.expand_dims(img_array, 0)
|
26 |
+
|
27 |
+
# find the top three likeliest plants based on probabilities
|
28 |
+
predictions = model.predict(img_array)
|
29 |
+
score = tf.nn.softmax(predictions[0])
|
30 |
+
top_three = np.array(score).argsort()[-3:][::-1]
|
31 |
+
numpy_array = score.numpy()
|
32 |
+
|
33 |
+
# convert the folder names into English words then return the three likeliest probabilities
|
34 |
+
output = []
|
35 |
+
for i in top_three:
|
36 |
+
words = class_names[i].split("_")
|
37 |
+
name = " ".join([word.capitalize() for word in words])
|
38 |
+
output.append(
|
39 |
+
"This image likely belongs to {} with {:.2f}% confidence."
|
40 |
+
.format(name, 100 * numpy_array[i])
|
41 |
+
)
|
42 |
+
return "\n".join(output)
|
43 |
+
|
44 |
+
# give the model a name
|
45 |
+
title = "Leaftracker Demonstration"
|
46 |
+
|
47 |
+
app = gr.Interface(
|
48 |
+
fn=predict_plant,
|
49 |
+
inputs=gr.Image(type="pil"),
|
50 |
+
outputs="text",
|
51 |
+
flagging_options=["incorrect", "other"],
|
52 |
+
)
|
53 |
+
app.launch()
|