|
|
|
|
|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from tensorflow.keras.applications.resnet50 import preprocess_input |
|
from tensorflow.keras.utils import load_img, img_to_array |
|
|
|
|
|
model = tf.keras.models.load_model("denis_mnist_cnn_model_resnet50_v1.h5") |
|
|
|
|
|
def preprocess_image(image): |
|
""" |
|
Preprocesses the uploaded image for prediction. |
|
""" |
|
image = image.resize((128, 128)) |
|
image = img_to_array(image) |
|
image = preprocess_input(image) |
|
image = np.expand_dims(image, axis=0) |
|
return image |
|
|
|
|
|
def predict(image): |
|
""" |
|
Accepts an image, preprocesses it, and returns the predicted label. |
|
""" |
|
processed_image = preprocess_image(image) |
|
predictions = model.predict(processed_image) |
|
predicted_class = np.argmax(predictions, axis=-1)[0] |
|
confidence = np.max(predictions) |
|
|
|
return {"prediction": int(predicted_class)} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="pil", label="Upload an Image"), |
|
outputs=gr.Textbox(label="Prediction"), |
|
title="MNIST ResNet50 Classifier", |
|
description="Upload an image to classify it using the trained ResNet50 model.", |
|
examples=[ |
|
["example_images/example1.png"], |
|
["example_images/example2.png"] |
|
], |
|
) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch(share=True) |