Spaces:
Sleeping
Sleeping
File size: 1,397 Bytes
1ac58d1 02af144 |
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 |
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "loretmar_ResNet50.keras"
model = tf.keras.models.load_model(model_path)
def predict_snake(image):
# Preprocess image
print(type(image))
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
image = image.resize((200, 200))
image = np.array(image)
image = np.expand_dims(image, axis=0) # same as image[None, ...]
prediction = model.predict(image)
# No need to apply sigmoid, as the output layer already uses softmax
# Convert the probabilities to rounded values
prediction = np.round(prediction, 2)
# Separate the probabilities for each class
p_1 = prediction[0][0]
p_2 = prediction[0][1]
p_3 = prediction[0][2]
p_4 = prediction[0][3]
return {'Agkistrodon contortrix (venomous)': p_1, 'Agkistrodon piscivorus (venomous)': p_2, 'Ahaetulla nasuta (not venomous)': p_3, 'Ahaetulla prasina (not venomous)': p_4}
input_image = gr.Image()
iface = gr.Interface(
fn=predict_snake,
inputs=input_image,
outputs=gr.Label(),
examples=["train/18/0bd1af4119054513917caa7944efd082.jpg", "train/20/0b103c4331bb47f4890d6e0ec96bf9bf.jpg", "train/25/0b4ef7c358044df1b734ea188811e684.jpg", "train/26/0f13f1c263c94602b0462353ff3d188b.jpg"],
description="TEST.")
iface.launch() |