File size: 2,035 Bytes
c7700ba
85afaa8
c7700ba
 
85afaa8
c7700ba
fb90311
49030c2
c7700ba
 
69f1901
 
f9ad6b8
69f1901
 
f22c6d8
 
 
 
 
 
c7700ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524689b
 
 
 
c7700ba
524689b
c7700ba
 
 
524689b
 
c7700ba
4f39314
 
c7700ba
 
524689b
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
import gradio as gr
import tensorflow as tf
from tensorflow.keras import models, layers


# Define model architecture
image_size=256
channels=3
input_shape = (None, image_size, image_size, channels)
n_classes = 3
#preprocessing during model creation RESCALING & RESIZING
resize_and_rescale = tf.keras.Sequential([
    layers.Resizing(image_size, image_size),
    layers.Rescaling(1.0/255)
])

#DATA AUGMENTATION
data_augmentation = tf.keras.Sequential([
    layers.RandomFlip("horizontal_and_vertical"),
    layers.RandomRotation(0.2)
])
model = models.Sequential([
    resize_and_rescale,
    data_augmentation,
    layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=input_shape),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(n_classes, activation='softmax')
])

# Load pre-trained weights
model.load_weights('model911.h5')

def classify_image(image):
    # Preprocess image (if needed)
    image = tf.image.resize(image, (image_size, image_size))  # Resize to expected shape
    image = tf.cast(image, dtype=tf.float32) / 255.0  # Rescale
    
    # Make prediction
    prediction = model.predict(tf.expand_dims(image, axis=0))
    classes = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
    return {classes[i]: float(prediction[0][i]) for i in range(len(classes))}

# Input component (No need for `shape` here)
inputs = gr.Image()  

# Output component (Use directly)
outputs = gr.Label(num_top_classes=3)

# Create Gradio interface
gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title='Potato Plant Diseases Classifier').launch()