oleksiikondus's picture
Create app.py
636f7ab
import numpy as np
import tensorflow as tf
import cv2
import gradio as gr
from tensorflow import keras
model = keras.models.load_model('model_InceptionV3.h5')
class_mapping = {1: 'Собака', 2: 'Кінь', 3: 'Слон', 4:'Метелик',
5: 'Курка', 6: 'Кіт', 7:'Корова', 8: 'Вівця',
9: 'Павук', 10: 'Білка'
}
# Створення функції для передбачення тварини
def predict_image(image):
image = cv2.resize(image, (224, 224))
image = np.asarray(image)
image = image.astype('float32') / 255.0
predictions = model.predict(np.expand_dims(image, axis=0))[0]
prediction = {}
for index, probability in enumerate(predictions) :
prediction[class_mapping[index+1]] = float(round(probability, 3))
print(prediction)
return prediction
demo = gr.Blocks()
# Створення інтерфейсу Gradio
with demo:
gr.Markdown("What animal is in the picture")
with gr.Tab("Predict image"):
image_input = gr.Image(label="Upload image")
output = gr.Label(label="Animal predicted by neural network")
image_button = gr.Button("Predict")
image_button.click(predict_image, inputs=image_input, outputs=output)
demo.launch()