project21 commited on
Commit
e96e371
·
verified ·
1 Parent(s): b95af94

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from tensorflow.keras.utils import img_to_array,load_img
3
+ from keras.models import load_model
4
+ import numpy as np
5
+
6
+ # Load the pre-trained model from the local path
7
+ model_path = 'bell pepper.h5'
8
+ model = load_model(model_path) # Load the model here
9
+
10
+ def predict_disease(image_file, model, all_labels):
11
+
12
+ try:
13
+ # Load and preprocess the image
14
+ img = load_img(image_file, target_size=(224, 224)) # Use load_img from tensorflow.keras.utils
15
+ img_array = img_to_array(img)
16
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
17
+ img_array = img_array / 255.0 # Normalize the image
18
+
19
+ # Predict the class
20
+ predictions = model.predict(img_array) # Use the loaded model here
21
+ predicted_class = np.argmax(predictions[0])
22
+
23
+ # Get the predicted class label
24
+ predicted_label = all_labels[predicted_class]
25
+
26
+ # Print the predicted label to the console
27
+
28
+ if predicted_label=='Bell pepper Healthy':
29
+ predicted_label = predicted_label = """<h3 align="center">Bell Pepper Healthy</h3><br><br>
30
+ <center>No need use Pesticides</center>"""
31
+
32
+ elif predicted_label=='Bell pepper Bacterial Spot':
33
+ predicted_label = """
34
+ <style>
35
+ li{
36
+ font-size: 15px;
37
+ margin-left: 90px;
38
+ margin-top: 15px;
39
+ margin-bottom: 15px;
40
+ }
41
+ h4{
42
+ font-size: 17px;
43
+ margin-top: 15px;
44
+ }
45
+ h4:hover{
46
+ cursor: pointer;
47
+ }
48
+
49
+ h3:hover{
50
+ cursor: pointer;
51
+ color: blue;
52
+ transform: scale(1.3);
53
+ }
54
+ .note{
55
+ text-align: center;
56
+ font-size: 16px;
57
+ }
58
+ p{
59
+ font-size: 13px;
60
+ text-align: center;
61
+ }
62
+
63
+ </style>
64
+ <h3><center><b>Bell pepper Bacterial Spot</b></center></h3>
65
+ <h4>PESTICIDES TO BE USED:</h4>
66
+ <ul>
67
+ <li>1. Oxytetracycline (Terramycin)</li>
68
+ <li>2. Streptomycin (Streptomycin sulfate)</li>
69
+ <li>3. Mancozeb (Dithane)</li>
70
+ <li>4. Copper oxychloride (Kocide)</li>
71
+ <li>5. Azoxystrobin (Heritage)</li>
72
+
73
+
74
+ </ul>
75
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
76
+ <p>Be sure to follow local regulations and guidelines for application</p>
77
+
78
+
79
+ """
80
+
81
+
82
+
83
+
84
+ else:
85
+ predicted_label = """<h3 align="center">Choose Correct image</h3><br><br>
86
+ """
87
+
88
+ return predicted_label
89
+
90
+
91
+ except Exception as e:
92
+ print(f"Error: {e}")
93
+ return None
94
+
95
+ # List of class labels
96
+ all_labels = [
97
+ 'Bell pepper Healthy',
98
+ 'Bell pepper Bacterial Spot'
99
+ ]
100
+
101
+ # Define the Gradio interface
102
+ def gradio_predict(image_file):
103
+ return predict_disease(image_file, model, all_labels) # Pass the model to the function
104
+
105
+ # Create a Gradio interface
106
+ gr_interface = gr.Interface(
107
+ fn=gradio_predict, # Function to call for predictions
108
+ inputs=gr.Image(type="filepath"), # Upload image as file path
109
+ outputs="html", # Output will be the class label as text
110
+ title="Bell Pepper Disease Predictor",
111
+ description="Upload an image of a plant to predict the disease.",
112
+ )
113
+
114
+ # Launch the Gradio app
115
+ gr_interface.launch(share=True)