Kresna's picture
Update app.py
d5bd88f
# app.py
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Load the model
model = tf.keras.models.load_model('Adam_8_1000_Acc 0.88_Nutrient-Model.h5')
# Define the class names
class_names = ['Calcium','Magnesium','Nitrogen','Phosphorus','Potassium','Sulfur']
# Function to classify the image
def classify_image(image):
# Convert the numpy array to a PIL Image object
pil_image = Image.fromarray(np.uint8(image)).convert('RGB')
# Resize the image
pil_image = pil_image.resize((224, 224))
# Convert the PIL Image object to a numpy array
image_array = np.array(pil_image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 255.0)
# Reshape the image
data = normalized_image_array.reshape((1, 224, 224, 3))
# Make the prediction
prediction = model.predict(data)[0]
# Get the predicted class name
predicted_class = class_names[np.argmax(prediction)]
# Get the confidence score for the predicted class
confidence_score = np.max(prediction)
# Return the predicted class and confidence score
return f"{predicted_class} ({confidence_score*100:.2f}%)"
# Define the Gradio interface
inputs = gr.inputs.Image()
outputs = gr.outputs.Textbox()
interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title="Image Classification", description="Classify an image into one of six classes: Phosphorus, Magnesium, Nitrogen,Potassium, Calcium, Sulfur.")
# Launch the interface
interface.launch()