Jeff28 commited on
Commit
29a3fa4
·
verified ·
1 Parent(s): 66b6d6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -43
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import os
2
- import numpy as np
 
3
  from tensorflow.keras.models import load_model
4
- from tensorflow.keras.preprocessing import image as keras_image
5
- from flask import Flask, request, jsonify
6
- from PIL import Image
7
- import io
8
-
9
- app = Flask(__name__)
10
 
11
  # Ensure the environment is set up correctly (we assume CPU-based execution here)
12
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU to avoid GPU-related warnings
13
 
14
  # Load the model
15
  def load_trained_model():
@@ -24,47 +21,40 @@ def load_trained_model():
24
 
25
  model = load_trained_model()
26
 
27
- # Define preprocessing function to resize and normalize images
28
- def preprocess_image(image, target_size=(224, 224)):
29
- img = Image.open(io.BytesIO(image))
30
- img = img.resize(target_size)
31
- img_array = np.array(img) / 255.0
32
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
33
- return img_array
34
-
35
- # Define a route to classify the uploaded image
36
- @app.route('/predict', methods=['POST'])
37
- def predict():
38
- if 'image' not in request.files:
39
- return jsonify({"error": "No image provided"}), 400
40
-
41
- image = request.files['image']
42
- if not image:
43
- return jsonify({"error": "No image content"}), 400
44
-
45
  try:
46
- # Preprocess the image for prediction
47
- image_data = preprocess_image(image.read())
 
 
 
48
 
49
  # Predict the class of the tomato leaf disease
50
- predictions = model.predict(image_data)
 
 
51
  predicted_class = np.argmax(predictions, axis=1)[0]
52
 
53
- # Return the prediction result
54
- disease_labels = {
55
- 0: "Tomato Bacterial Spot",
56
- 1: "Tomato Early Blight",
57
- 2: "Tomato Late Blight",
58
- 3: "Tomato Mosaic Virus",
59
- 4: "Tomato Yellow Leaf Curl Virus"
60
- }
61
 
62
- prediction_result = disease_labels.get(predicted_class, "Unknown disease")
63
-
64
- return jsonify({"prediction": prediction_result}), 200
65
  except Exception as e:
66
  print(f"Error during prediction: {e}")
67
- return jsonify({"error": "Prediction failed"}), 500
68
-
 
 
 
 
 
 
 
 
 
 
69
  if __name__ == '__main__':
70
- app.run(debug=True)
 
1
  import os
2
+ import gradio as gr
3
+ import tensorflow as tf
4
  from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.preprocessing import image
6
+ import numpy as np
 
 
 
 
7
 
8
  # Ensure the environment is set up correctly (we assume CPU-based execution here)
9
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU to avoid the GPU-related warnings
10
 
11
  # Load the model
12
  def load_trained_model():
 
21
 
22
  model = load_trained_model()
23
 
24
+ # Define the function to process the image and predict the disease
25
+ def predict_disease(image_file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  try:
27
+ # Preprocess the image
28
+ img = image.load_img(image_file, target_size=(224, 224)) # Adjust size based on model input
29
+ img_array = image.img_to_array(img)
30
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
31
+ img_array /= 255.0 # Normalize the image if required by the model
32
 
33
  # Predict the class of the tomato leaf disease
34
+ predictions = model.predict(img_array)
35
+
36
+ # Assuming the model returns probabilities, get the class with the highest probability
37
  predicted_class = np.argmax(predictions, axis=1)[0]
38
 
39
+ # You can map the class index to the disease name if required
40
+ disease_classes = ['Bacterial_spot', 'Early_blight', 'Late_blight', 'Tomato_mosaic_virus', 'Tomato_Yellow_Leaf_Curl_Virus']
41
+ predicted_disease = disease_classes[predicted_class]
 
 
 
 
 
42
 
43
+ return predicted_disease
44
+
 
45
  except Exception as e:
46
  print(f"Error during prediction: {e}")
47
+ return "Error during prediction"
48
+
49
+ # Define Gradio interface
50
+ iface = gr.Interface(
51
+ fn=predict_disease,
52
+ inputs=gr.inputs.Image(type="file"),
53
+ outputs="text",
54
+ title="Tomato Leaf Disease Detection",
55
+ description="Upload an image of a tomato leaf, and the model will predict the disease."
56
+ )
57
+
58
+ # Launch the Gradio interface
59
  if __name__ == '__main__':
60
+ iface.launch()