File size: 2,839 Bytes
f1d45e3
 
90a3647
f1d45e3
 
 
99786e5
90a3647
f1d45e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee777bb
13a0e35
ffd45c2
f1d45e3
 
 
 
 
 
 
ffd45c2
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import tensorflow as tf
import gradio as gr
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt
import os 

# Load the trained TensorFlow model using the Keras API
model = tf.keras.models.load_model("potatoesV3.h5")

# Define the class names for classification
CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]


# Define the function for making predictions
def classify_image(image):
    # Open the image using Pillow
    img = Image.fromarray(image.astype('uint8'), 'RGB')
    new_size = (256, 256)
    img_resized = img.resize(new_size)
    #img_array = np.array(img_resized)
    #img_scaled = img_array / 255.0
    img_array = np.expand_dims(img_resized, axis=0)
    pred = model.predict(img_array)

    predicted_class = CLASS_NAMES[np.argmax(pred[0])]
    confidence = float(np.max(pred[0]))

    # Return the predicted class and confidence score
    return {"class": predicted_class, "confidence": confidence,"predict":pred[0]}
    # Resize the image to the desired size
    # new_size = (256, 256)
    # img_resized = image.resize(new_size)
    
    # # Convert the image to a numpy array
    # img_array = np.array(img_resized)
    
    # # Rescale the image pixel values to [0, 1]
    # img_scaled = img_array / 255.0

    # #img_array = np.array(img_scaled)
    # img_array = np.expand_dims(img_scaled, axis=0)
    # pred = model.predict(img_array)
    
    # # Display the rescaled image
    # #plt.imshow(img_array)
    
    # # Format the predicted class and confidence score
    # predicted_class = CLASS_NAMES[np.argmax(pred[0])]
    # confidence = float(np.max(pred[0]))

    # # Return the predicted class and confidence score
    # return {"class": predicted_class, "confidence": confidence,"predict":pred[0]}
    # img = (BytesIO(image))
    # img = img.resize((256, 256))
    # img_array = np.array(img)
    # img_array = np.expand_dims(img_array, axis=0)
    # pred = model.predict(img_array)
    # return 
examples=['7227b3db-c212-4370-8b42-443eea1577aa___RS_Early.B 7306.JPG','7456db33-766c-4a68-b924-ddf69d579981___RS_Early.B 6723.JPG','7486e823-64f7-4e43-ab51-26261b077fc2___RS_Early.B 6785.JPG','8829e413-5a7a-4680-b873-e71dfa9dbfe4___RS_LB 3974.JPG','9001b18c-b659-4c56-9dfb-0d0bf64a7b4a___RS_LB 4987.JPG','9009c86e-1205-4694-b0bb-ef7cf78dd104___RS_LB 3995.JPG','Potato_healthy-76-_0_2420.jpg','Potato_healthy-76-_0_6833.jpg','Potato_healthy-76-_0_7539.jpg']

# Define the Gradio interface for image input
input_interface = gr.inputs.Image()

# Define the Gradio interface for displaying the predicted class and confidence score
output_interface = gr.Textbox()

# Launch the Gradio interface with the classify_image function as the prediction function
gr.Interface(fn=classify_image, inputs=input_interface, outputs=output_interface,Examples=examples).launch()