Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 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')
|
|
@@ -12,7 +14,14 @@ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
| 14 |
# Load your pre-trained model
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
vegetables = [
|
| 17 |
"banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
|
| 18 |
"bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
|
|
@@ -39,25 +48,46 @@ def predict():
|
|
| 39 |
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 40 |
|
| 41 |
# Convert to RGB format
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 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)
|
|
|
|
| 1 |
import base64
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
import pytesseract
|
| 5 |
from flask import Flask, request, jsonify, render_template
|
| 6 |
from tensorflow import keras
|
| 7 |
import sys
|
| 8 |
import io
|
| 9 |
+
from inference_sdk import InferenceHTTPClient
|
| 10 |
|
| 11 |
# Set the default encoding to utf-8
|
| 12 |
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
|
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
| 16 |
# Load your pre-trained model
|
| 17 |
+
fresh_model = keras.models.load_model('fresh_model.keras')
|
| 18 |
+
|
| 19 |
+
# Initialize the InferenceHTTPClient for the external model API
|
| 20 |
+
CLIENT = InferenceHTTPClient(
|
| 21 |
+
api_url="https://detect.roboflow.com",
|
| 22 |
+
api_key="QDeIvOVhC1LVQhp1wRti"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
vegetables = [
|
| 26 |
"banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
|
| 27 |
"bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
|
|
|
|
| 48 |
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 49 |
|
| 50 |
# Convert to RGB format
|
| 51 |
+
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 52 |
+
|
| 53 |
+
# Detect text using Tesseract OCR
|
| 54 |
+
text = pytesseract.image_to_string(rgb_image)
|
| 55 |
+
|
| 56 |
+
# Check if any text is detected
|
| 57 |
+
if text.strip():
|
| 58 |
+
print(f"Detected text: {text}")
|
| 59 |
+
# Use the fresh_model for prediction if text is detected
|
| 60 |
+
|
| 61 |
+
# Resize the image to the expected input size of the model (e.g., 225x225)
|
| 62 |
+
image_resized = cv2.resize(rgb_image, (225, 225))
|
| 63 |
+
|
| 64 |
+
# Normalize the image
|
| 65 |
+
image_resized = image_resized.astype('float32') / 255.0 # Normalize to [0, 1]
|
| 66 |
+
image_resized = np.expand_dims(image_resized, axis=0) # Add batch dimension
|
| 67 |
+
|
| 68 |
+
# Make a prediction using the model
|
| 69 |
+
predictions = fresh_model.predict(image_resized)
|
| 70 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 71 |
|
| 72 |
+
# Map the class index to the vegetable label
|
| 73 |
+
prediction_label = vegetables[predicted_class]
|
| 74 |
|
| 75 |
+
return jsonify({'prediction': prediction_label})
|
| 76 |
+
else:
|
| 77 |
+
print("No text detected. Using external model inference.")
|
| 78 |
+
# Convert the image back to bytes for API use
|
| 79 |
+
_, buffer = cv2.imencode('.jpg', rgb_image)
|
| 80 |
+
image_bytes = buffer.tobytes()
|
| 81 |
|
| 82 |
+
# Use the InferenceHTTPClient to send the image to the external API
|
| 83 |
+
result = CLIENT.infer(image_bytes, model_id="zydus-wellness-acjbd/2")
|
|
|
|
| 84 |
|
| 85 |
+
# Process the result and extract necessary information
|
| 86 |
+
prediction_label = result.get('predictions', [{}])[0].get('label', 'Unknown')
|
| 87 |
+
return jsonify({'prediction': prediction_label})
|
| 88 |
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
return jsonify({'error': str(e)}), 500
|
| 91 |
|
| 92 |
if __name__ == '__main__':
|
| 93 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|