project21 commited on
Commit
7aa37f3
·
verified ·
1 Parent(s): 6f62252

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from keras.preprocessing import image
3
+ from keras.preprocessing.image import img_to_array
4
+ from keras.models import load_model
5
+ import numpy as np
6
+
7
+ # Load the pre-trained model from the local path
8
+ model_path = 'Mango.h5'
9
+ model = load_model(model_path)
10
+
11
+ def predict_disease(image_file, model, all_labels):
12
+ """
13
+ Predict the disease from an image using the trained model.
14
+
15
+ Parameters:
16
+ - image_file: image, input image file
17
+ - model: Keras model, trained convolutional neural network
18
+ - all_labels: list, list of class labels
19
+
20
+ Returns:
21
+ - str, predicted class label
22
+ """
23
+ try:
24
+ # Load and preprocess the image
25
+ img = image.load_img(image_file, target_size=(256, 256))
26
+ img_array = img_to_array(img)
27
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
28
+ img_array = img_array / 255.0 # Normalize the image
29
+
30
+ # Predict the class
31
+ predictions = model.predict(img_array)
32
+ predicted_class = np.argmax(predictions[0])
33
+
34
+ # Return the class label
35
+ return all_labels[predicted_class]
36
+
37
+ except Exception as e:
38
+ print(f"Error: {e}")
39
+ return None
40
+
41
+ # List of class labels
42
+ all_labels = ['Mango Anthracrose','Mango Bacterial Cancker','Mango Cutting weevil','Mango Die Back','Mango Gall Midge ','Mango Healthy','Mango powdery mildew','Mango Sooty Mould']
43
+
44
+ # Define the Gradio interface
45
+ def gradio_predict(image_file):
46
+ return predict_disease(image_file, model, all_labels)
47
+
48
+ # Create a Gradio interface
49
+ gr_interface = gr.Interface(
50
+ fn=gradio_predict, # Function to call for predictions
51
+ inputs=gr.Image(type="filepath"), # Upload image as file path
52
+ outputs="text", # Output will be the class label as text
53
+ title="Plant Disease Predictor",
54
+ description="Upload an image of a plant to predict the disease.",
55
+ )
56
+
57
+ # Launch the Gradio app
58
+ gr_interface.launch()