File size: 1,508 Bytes
fed96c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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('Nutrient-Model.h5')

# Define the class names
class_names = ['Magnesium','Nitrogen','Potassium','Phosphorus','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()