|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
import tensorflow as tf |
|
|
|
from PIL import Image |
|
|
|
|
|
|
|
class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips'] |
|
|
|
|
|
resnet_model = tf.keras.models.load_model('./flower_image_classification_ResNet50_v1.0.h5') |
|
|
|
def preprocess_image(image): |
|
|
|
image = np.array(image) |
|
|
|
|
|
image_resized = cv2.resize(image, (img_height, img_width)) |
|
|
|
|
|
image = np.expand_dims(image_resized, axis=0) |
|
|
|
|
|
pred = resnet_model.predict(image) |
|
|
|
|
|
predicted_class = np.argmax(pred) |
|
output_class = class_names[predicted_class] |
|
|
|
|
|
confidence_level = pred[0][predicted_class] |
|
|
|
return image_resized, output_class, confidence_level |
|
|
|
def predict(image): |
|
image_resized, output_class, confidence_level = preprocess_image(image) |
|
return Image.fromarray(image_resized), output_class, str(confidence_level) |
|
|
|
|
|
inputs = gr.Image(type="pil", label="Upload Image") |
|
outputs = [ |
|
gr.Image(type="pil", label="Resized Image"), |
|
gr.Textbox(label="Predicted Class"), |
|
gr.Textbox(label="Confidence Level") |
|
] |
|
|
|
|
|
gr.Interface( |
|
fn=predict, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="Flower Classification with ResNet50", |
|
description="Upload an image of a flower to classify it into one of the five categories.", |
|
live=True |
|
).launch() |
|
|