HashamUllah commited on
Commit
69f9410
1 Parent(s): 129342b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -43
app.py CHANGED
@@ -1,43 +1,43 @@
1
- from flask import Flask, request, jsonify
2
- import tensorflow as tf
3
- import numpy as np
4
- from PIL import Image
5
- import io
6
- import json
7
-
8
- app = Flask(__name__)
9
-
10
- # Load the TensorFlow model
11
- model = tf.keras.models.load_model('./plant_disease_detection_saved_model')
12
-
13
- # Load categories
14
- with open('./categories.json') as f:
15
- categories = json.load(f)
16
-
17
- def preprocess_image(image):
18
- # Convert the image to a NumPy array
19
- image = Image.open(io.BytesIO(image))
20
- image = image.resize((224, 224)) # Adjust size as needed
21
- image_array = np.array(image) / 255.0 # Normalize to [0, 1]
22
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
23
- return image_array
24
-
25
- @app.route('/predict', methods=['POST'])
26
- def predict():
27
- if 'image' not in request.files:
28
- return jsonify({'error': 'No image provided'}), 400
29
-
30
- image = request.files['image'].read()
31
- image_array = preprocess_image(image)
32
-
33
- # Make prediction
34
- predictions = model.predict(image_array)
35
- predicted_class = np.argmax(predictions, axis=1)[0]
36
-
37
- # Map to category names
38
- predicted_label = categories.get(str(predicted_class), 'Unknown')
39
-
40
- return jsonify({'class': predicted_label, 'confidence': float(predictions[0][predicted_class])})
41
-
42
- if __name__ == '__main__':
43
- app.run(host='0.0.0.0', port=8080, debug=True)
 
1
+ from flask import Flask, request, jsonify
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import io
6
+ import json
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Load the TensorFlow model
11
+ model = tf.keras.models.load_model('plant_disease_detection_saved_model')
12
+
13
+ # Load categories
14
+ with open('categories.json') as f:
15
+ categories = json.load(f)
16
+
17
+ def preprocess_image(image):
18
+ # Convert the image to a NumPy array
19
+ image = Image.open(io.BytesIO(image))
20
+ image = image.resize((224, 224)) # Adjust size as needed
21
+ image_array = np.array(image) / 255.0 # Normalize to [0, 1]
22
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
23
+ return image_array
24
+
25
+ @app.route('/predict', methods=['POST'])
26
+ def predict():
27
+ if 'image' not in request.files:
28
+ return jsonify({'error': 'No image provided'}), 400
29
+
30
+ image = request.files['image'].read()
31
+ image_array = preprocess_image(image)
32
+
33
+ # Make prediction
34
+ predictions = model.predict(image_array)
35
+ predicted_class = np.argmax(predictions, axis=1)[0]
36
+
37
+ # Map to category names
38
+ predicted_label = categories.get(str(predicted_class), 'Unknown')
39
+
40
+ return jsonify({'class': predicted_label, 'confidence': float(predictions[0][predicted_class])})
41
+
42
+ if __name__ == '__main__':
43
+ app.run(host='0.0.0.0', port=8080, debug=True)