aryan365 commited on
Commit
ed112f9
·
verified ·
1 Parent(s): bd1a1d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -63
app.py CHANGED
@@ -1,63 +1,63 @@
1
- import base64
2
- import cv2
3
- import numpy as np
4
- from flask import Flask, request, jsonify, render_template
5
- import keras._tf_keras.keras as keras
6
- import sys
7
- import io
8
-
9
- # Set the default encoding to utf-8
10
- sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
11
-
12
- app = Flask(__name__)
13
-
14
- # Load your pre-trained model
15
- model = keras.models.load_model(r'model\fresh_model.keras')
16
- vegetables = [
17
- "banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
18
- "bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
19
- "capsicum green", "carrot", "cauliflower", "chilli green", "colocasia arvi",
20
- "corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger",
21
- "lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini"
22
- ]
23
-
24
- @app.route('/')
25
- def index():
26
- return render_template('index.html')
27
-
28
- @app.route('/predict', methods=['POST'])
29
- def predict():
30
- try:
31
- # Extract and decode image data
32
- data = request.json
33
- image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
34
-
35
- # Decode the base64 string into a NumPy array
36
- nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
37
-
38
- # Convert the NumPy array into an OpenCV image (BGR format)
39
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
40
-
41
- # Convert to RGB format
42
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
43
-
44
- # Resize the image to the expected input size of the model (e.g., 225x225)
45
- image = cv2.resize(image, (225, 225))
46
-
47
- # Normalize the image
48
- image = image.astype('float32') / 255.0 # Normalize to [0, 1]
49
- image = np.expand_dims(image, axis=0) # Add batch dimension
50
-
51
- # Make a prediction using the model
52
- predictions = model.predict(image)
53
- predicted_class = np.argmax(predictions, axis=1)[0]
54
-
55
- # Map the class index to the vegetable label
56
- prediction_label = vegetables[predicted_class]
57
-
58
- return jsonify({'prediction': prediction_label})
59
- except Exception as e:
60
- return jsonify({'error': str(e)}), 500
61
-
62
- if __name__ == '__main__':
63
- app.run(debug=True)
 
1
+ import base64
2
+ import cv2
3
+ import numpy as np
4
+ from flask import Flask, request, jsonify, render_template
5
+ import keras._tf_keras.keras as keras
6
+ import sys
7
+ import io
8
+
9
+ # Set the default encoding to utf-8
10
+ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
11
+
12
+ app = Flask(__name__)
13
+
14
+ # Load your pre-trained model
15
+ model = keras.models.load_model(r'model\fresh_model.keras')
16
+ vegetables = [
17
+ "banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
18
+ "bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
19
+ "capsicum green", "carrot", "cauliflower", "chilli green", "colocasia arvi",
20
+ "corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger",
21
+ "lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini"
22
+ ]
23
+
24
+ @app.route('/')
25
+ def index():
26
+ return render_template('index.html')
27
+
28
+ @app.route('/predict', methods=['POST'])
29
+ def predict():
30
+ try:
31
+ # Extract and decode image data
32
+ data = request.json
33
+ image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
34
+
35
+ # Decode the base64 string into a NumPy array
36
+ nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
37
+
38
+ # Convert the NumPy array into an OpenCV image (BGR format)
39
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
40
+
41
+ # Convert to RGB format
42
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
43
+
44
+ # Resize the image to the expected input size of the model (e.g., 225x225)
45
+ image = cv2.resize(image, (225, 225))
46
+
47
+ # Normalize the image
48
+ image = image.astype('float32') / 255.0 # Normalize to [0, 1]
49
+ image = np.expand_dims(image, axis=0) # Add batch dimension
50
+
51
+ # Make a prediction using the model
52
+ predictions = model.predict(image)
53
+ predicted_class = np.argmax(predictions, axis=1)[0]
54
+
55
+ # Map the class index to the vegetable label
56
+ prediction_label = vegetables[predicted_class]
57
+
58
+ return jsonify({'prediction': prediction_label})
59
+ except Exception as e:
60
+ return jsonify({'error': str(e)}), 500
61
+
62
+ if __name__ == '__main__':
63
+ app.run(host='0.0.0.0', port=5000, debug=True)