Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import tensorflow as tf
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
|
6 |
-
model_path = "
|
7 |
-
model = tf.keras.models.load_model(model_path)
|
8 |
-
|
9 |
-
# Define the core prediction function
|
10 |
-
def predict_fruit(image):
|
11 |
-
# Preprocess image
|
12 |
-
print(type(image))
|
13 |
-
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
14 |
-
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
15 |
-
image = np.array(image)
|
16 |
-
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
17 |
-
|
18 |
-
# Predict
|
19 |
-
prediction = model.predict(image)
|
20 |
-
|
21 |
-
# No need to apply sigmoid, as the output layer already uses softmax
|
22 |
-
# Convert the probabilities to rounded values
|
23 |
-
prediction = np.round(prediction, 3)
|
24 |
-
|
25 |
-
# Separate the probabilities for each class
|
26 |
-
p_apple = prediction[0][0] # Probability for class 'articuno'
|
27 |
-
p_banana = prediction[0][1] # Probability for class 'moltres'
|
28 |
-
p_pinenapple = prediction[0][2] # Probability for class 'zapdos'
|
29 |
-
p_strawberries = prediction[0][3]
|
30 |
-
p_watermelon = prediction[0][4]
|
31 |
-
|
32 |
-
return {'apple': p_apple, 'banana': p_banana, 'pinenapple': p_pinenapple, 'strawberries': p_strawberries, 'watermelon': p_watermelon}
|
33 |
-
|
34 |
-
# Create the Gradio interface
|
35 |
-
input_image = gr.Image()
|
36 |
-
iface = gr.Interface(
|
37 |
-
fn=predict_fruit,
|
38 |
-
inputs=input_image,
|
39 |
-
outputs=gr.Label(),
|
40 |
-
examples=["
|
41 |
-
description="TEST.")
|
42 |
-
|
43 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
model_path = "Xeption_fruits.keras"
|
7 |
+
model = tf.keras.models.load_model(model_path)
|
8 |
+
|
9 |
+
# Define the core prediction function
|
10 |
+
def predict_fruit(image):
|
11 |
+
# Preprocess image
|
12 |
+
print(type(image))
|
13 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
14 |
+
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
15 |
+
image = np.array(image)
|
16 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
17 |
+
|
18 |
+
# Predict
|
19 |
+
prediction = model.predict(image)
|
20 |
+
|
21 |
+
# No need to apply sigmoid, as the output layer already uses softmax
|
22 |
+
# Convert the probabilities to rounded values
|
23 |
+
prediction = np.round(prediction, 3)
|
24 |
+
|
25 |
+
# Separate the probabilities for each class
|
26 |
+
p_apple = prediction[0][0] # Probability for class 'articuno'
|
27 |
+
p_banana = prediction[0][1] # Probability for class 'moltres'
|
28 |
+
p_pinenapple = prediction[0][2] # Probability for class 'zapdos'
|
29 |
+
p_strawberries = prediction[0][3]
|
30 |
+
p_watermelon = prediction[0][4]
|
31 |
+
|
32 |
+
return {'apple': p_apple, 'banana': p_banana, 'pinenapple': p_pinenapple, 'strawberries': p_strawberries, 'watermelon': p_watermelon}
|
33 |
+
|
34 |
+
# Create the Gradio interface
|
35 |
+
input_image = gr.Image()
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=predict_fruit,
|
38 |
+
inputs=input_image,
|
39 |
+
outputs=gr.Label(),
|
40 |
+
examples=["images/ap1.jpeg", "images/ap2.jpeg", "images/ap3.jpeg", "images/ba1.jpeg", "images/ba2.jpeg", "images/ba3.jpeg", "images/pi1.jpeg","images/pi2.jpeg","images/pi3.jpeg","images/st1.jpeg", "images/st2.jpeg", "images/st3.jpeg","images/wa1.jpeg","images/wa2.jpeg","images/wa3.jpeg"],
|
41 |
+
description="TEST.")
|
42 |
+
|
43 |
iface.launch()
|