|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
model_path = "Fruits_fruits.keras" |
|
model = tf.keras.models.load_model(model_path) |
|
|
|
|
|
def predict_fruit(image): |
|
|
|
image = Image.fromarray(image.astype('uint8')) |
|
image = image.resize((150, 150)) |
|
image = np.array(image) |
|
image = np.expand_dims(image, axis=0) |
|
image = image / 255.0 |
|
|
|
|
|
prediction = model.predict(image) |
|
|
|
|
|
probabilities = tf.nn.softmax(prediction) |
|
|
|
|
|
fruit_classes = ['Apple', 'Lemon', 'Strawberry'] |
|
probabilities_dict = {fruit_class: round(float(probability), 2) for fruit_class, probability in zip(fruit_classes, probabilities[0])} |
|
|
|
return probabilities_dict |
|
|
|
|
|
input_image = gr.inputs.Image(shape=(150, 150), label="Upload a Fruit Image") |
|
output_label = gr.outputs.Label(num_top_classes=3, label="Prediction") |
|
|
|
iface = gr.Interface( |
|
fn=predict_fruit, |
|
inputs=input_image, |
|
outputs=output_label, |
|
live=True, |
|
title="Fruit Classification", |
|
description="Upload an image of an Apple, Lemon, or Strawberry to get the predicted class. The model uses a CNN trained on the Fruits-360 dataset.", |
|
examples=["images/01.jpg", "images/02.jpg", "images/03.jpg"], |
|
theme="dark" |
|
) |
|
|
|
iface.launch() |
|
|