project21 commited on
Commit
08ef074
·
verified ·
1 Parent(s): 622d3b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -71
app.py CHANGED
@@ -1,93 +1,87 @@
 
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 = 'peach.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=='Peach Healthy':
29
- predicted_label = predicted_label = """<h3 align="center">Peach Healthy</h3><br><br>
30
- <center>No need use Pesticides</center>"""
31
- elif predicted_label=='Peach Bacterial Spot':
32
- predicted_label = """
33
- <style>
34
- li{
35
- font-size: 15px;
36
- margin-left: 90px;
37
- margin-top: 15px;
38
- margin-bottom: 15px;
39
- }
40
- h4{
41
- font-size: 17px;
42
- margin-top: 15px;
43
- }
44
- h4:hover{
45
- cursor: pointer;
46
- }
47
-
48
- h3:hover{
49
- cursor: pointer;
50
- color: blue;
51
- transform: scale(1.3);
52
- }
53
- .note{
54
- text-align: center;
55
- font-size: 16px;
56
- }
57
- p{
58
- font-size: 13px;
59
- text-align: center;
60
- }
61
-
62
- </style>
63
- <h3><center><b>Peach Bacterial Spot</b></center></h3>
64
- <h4>PESTICIDES TO BE USED:</h4>
65
- <ul>
66
  <li>1. Copper oxychloride (Kocide)</li>
67
  <li>2. Streptomycin (Streptomycin sulfate)</li>
68
  <li>3. Tetracycline (Agrimycin)</li>
69
  <li>4. Oxytetracycline (Terramycin)</li>
70
-
71
-
72
- </ul>
73
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
74
- <p>Be sure to follow local regulations and guidelines for application</p>
75
-
76
-
77
  """
78
-
79
-
80
-
 
 
 
 
81
  else:
82
- predicted_label = """<h3 align="center">Choose Correct image</h3><br><br>
83
- """
 
84
 
85
- return predicted_label
 
 
86
 
 
 
 
 
 
 
87
 
88
  except Exception as e:
89
- print(f"Error: {e}")
90
- return None
91
 
92
  # List of class labels
93
  all_labels = [
@@ -95,18 +89,42 @@ all_labels = [
95
  'Peach Bacterial Spot'
96
  ]
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  # Define the Gradio interface
99
- def gradio_predict(image_file):
100
- return predict_disease(image_file, model, all_labels) # Pass the model to the function
 
 
101
 
102
- # Create a Gradio interface
103
  gr_interface = gr.Interface(
104
- fn=gradio_predict, # Function to call for predictions
105
- inputs=gr.Image(type="filepath"), # Upload image as file path
106
- outputs="html", # Output will be the class label as text
 
 
 
107
  title="Peach Disease Predictor",
108
- description="Upload an image of a plant to predict the disease.",
109
  )
110
 
111
  # Launch the Gradio app
112
- gr_interface.launch(debug=True)
 
1
+ import h5py
2
  import gradio as gr
3
+ from tensorflow.keras.utils import img_to_array, load_img
4
  from keras.models import load_model
5
  import numpy as np
6
+ from deep_translator import GoogleTranslator
7
 
8
  # Load the pre-trained model from the local path
9
  model_path = 'peach.h5'
 
 
 
10
 
11
+ # Check if the model is loading correctly
12
+ try:
13
+ with h5py.File(model_path, 'r+') as f:
14
+ if 'groups' in f.attrs['model_config']:
15
+ model_config_string = f.attrs['model_config']
16
+ model_config_string = model_config_string.replace('"groups": 1,', '')
17
+ model_config_string = model_config_string.replace('"groups": 1}', '}')
18
+ f.attrs['model_config'] = model_config_string.encode('utf-8')
19
+
20
+ model = load_model(model_path)
21
+ print("Model loaded successfully.")
22
+ except Exception as e:
23
+ print(f"Error loading model: {e}")
24
+
25
+ def predict_disease(image_file, model, all_labels, target_language):
26
  try:
27
  # Load and preprocess the image
28
+ print(f"Received image file: {image_file}")
29
+ img = load_img(image_file, target_size=(224, 224)) # Ensure image size matches model input
30
  img_array = img_to_array(img)
31
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
32
  img_array = img_array / 255.0 # Normalize the image
33
 
34
  # Predict the class
35
+ predictions = model.predict(img_array)
36
  predicted_class = np.argmax(predictions[0])
37
+
38
  # Get the predicted class label
39
  predicted_label = all_labels[predicted_class]
40
+
41
+ # Translate the predicted label to the selected language
42
+ translated_label = GoogleTranslator(source='en', target=target_language).translate(predicted_label)
43
+
44
+ # Provide pesticide information based on the predicted label
45
+ if predicted_label == 'Peach Bacterial Spot':
46
+ pesticide_info = """
47
+ <h2><center><b>Peach Bacterial Spot</b></center></h2>
48
+ <h4>PESTICIDES TO BE USED:</h4><br>
49
+
50
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  <li>1. Copper oxychloride (Kocide)</li>
52
  <li>2. Streptomycin (Streptomycin sulfate)</li>
53
  <li>3. Tetracycline (Agrimycin)</li>
54
  <li>4. Oxytetracycline (Terramycin)</li>
55
+ </ul><br>
56
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
57
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
 
 
 
58
  """
59
+
60
+
61
+
62
+ elif predicted_label == 'Peach Healthy':
63
+ pesticide_info = """<h2><center><b>Peach Healthy</b></center></h2>
64
+ <h5> No pesticides needed"""
65
+
66
  else:
67
+ pesticide_info = 'No pesticide information available.'
68
+
69
+ print(f"Pesticide Info (Before Translation): {pesticide_info}")
70
 
71
+ # Translate the pesticide information to the selected language
72
+ translated_pesticide_info = GoogleTranslator(source='en', target=target_language).translate(pesticide_info)
73
+ print(f"Translated Pesticide Info: {translated_pesticide_info}")
74
 
75
+ # Return translated label and pesticide information with associated styling
76
+ predicted_label_html = f"""
77
+
78
+ {translated_pesticide_info}
79
+ """
80
+ return predicted_label_html
81
 
82
  except Exception as e:
83
+ print(f"Error during prediction: {e}")
84
+ return f"<h3>Error: {e}</h3>"
85
 
86
  # List of class labels
87
  all_labels = [
 
89
  'Peach Bacterial Spot'
90
  ]
91
 
92
+ # Language codes and their full names (display full names in dropdown)
93
+ language_choices = {
94
+ 'hi': 'Hindi',
95
+ 'te': 'Telugu',
96
+ 'en': 'English',
97
+ 'ml': 'Malayalam',
98
+ 'ta': 'Tamil',
99
+ 'bn': 'Bengali',
100
+ 'gu': 'Gujarati',
101
+ 'kn': 'Kannada',
102
+ 'mr': 'Marathi'
103
+ }
104
+
105
+ # Mapping full names back to their corresponding language code
106
+ full_to_code = {value: key for key, value in language_choices.items()}
107
+
108
+ # Create a dropdown of full language names, using the full name in the UI
109
+ languages = list(language_choices.values()) # List of full language names
110
+
111
  # Define the Gradio interface
112
+ def gradio_predict(image_file, target_language):
113
+ # Map full name back to language code for translation
114
+ language_code = full_to_code.get(target_language, 'en')
115
+ return predict_disease(image_file, model, all_labels, language_code)
116
 
117
+ # Create the Gradio interface
118
  gr_interface = gr.Interface(
119
+ fn=gradio_predict,
120
+ inputs=[
121
+ gr.Image(type="filepath"), # Image input for disease prediction
122
+ gr.Dropdown(label="Select language", choices=languages, value='English') # Language selection dropdown with full names
123
+ ],
124
+ outputs="html", # Output will be in HTML (translated text)
125
  title="Peach Disease Predictor",
126
+ description="Upload an image of a plant to predict the disease and get the translated label and pesticide information in the selected language."
127
  )
128
 
129
  # Launch the Gradio app
130
+ gr_interface.launch()