aryan365 commited on
Commit
1c90932
·
verified ·
1 Parent(s): aac5e8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -1,18 +1,25 @@
1
  import base64
2
- import numpy as np
3
  import cv2
4
- from flask import Flask, request, jsonify, render_template
 
5
  from tensorflow import keras
6
- import torch
7
- from ultralytics import YOLO
8
 
9
- app = Flask(__name__)
 
10
 
11
- # Load the Keras model
12
- model_keras = keras.models.load_model('fresh_model.keras')
13
 
14
- # Load the YOLO model
15
- model_yolo = YOLO('another_model.pt')
 
 
 
 
 
 
 
16
 
17
  @app.route('/')
18
  def index():
@@ -27,27 +34,30 @@ def predict():
27
 
28
  # Decode the base64 string into a NumPy array
29
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
 
 
30
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
31
 
32
- # Prepare image for Keras model
33
- image_keras = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
34
- image_keras = cv2.resize(image_keras, (225, 225))
35
- image_keras = image_keras.astype('float32') / 255.0
36
- image_keras = np.expand_dims(image_keras, axis=0)
 
 
 
 
37
 
38
- # Make prediction with Keras model
39
- keras_predictions = model_keras.predict(image_keras)
40
- predicted_class = np.argmax(keras_predictions, axis=1)[0]
41
 
42
- # Prepare image for YOLO model
43
- yolo_predictions = model_yolo.predict(image) # YOLO prediction
44
 
45
- return jsonify({
46
- 'keras-prediction': predicted_class,
47
- 'yolo-prediction': yolo_predictions
48
- })
49
  except Exception as e:
50
  return jsonify({'error': str(e)}), 500
51
 
52
  if __name__ == '__main__':
53
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
  import base64
 
2
  import cv2
3
+ import numpy as np
4
+ from flask import Flask, request, jsonify, render_template
5
  from tensorflow import 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('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():
 
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=7860, debug=True)