Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,32 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
| 3 |
import base64
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
-
from tensorflow.keras.models import load_model
|
| 7 |
|
| 8 |
-
|
| 9 |
-
model = load_model("best_model.h5")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
img = Image.open(BytesIO(img_bytes)).convert('RGB')
|
| 21 |
-
img = img.resize(
|
| 22 |
-
img_array =
|
| 23 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
| 24 |
return img_array
|
| 25 |
|
| 26 |
@app.route('/')
|
|
@@ -29,19 +35,17 @@ def index():
|
|
| 29 |
|
| 30 |
@app.route('/predict', methods=['POST'])
|
| 31 |
def predict():
|
| 32 |
-
data = request.get_json()
|
| 33 |
-
|
| 34 |
-
img_array = preprocess_image(
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
confidence = float(predictions[0][pred_idx] * 100)
|
| 40 |
-
|
| 41 |
return jsonify({
|
| 42 |
'prediction': pred_label,
|
| 43 |
'confidence': confidence
|
| 44 |
})
|
| 45 |
|
| 46 |
if __name__ == '__main__':
|
| 47 |
-
app.run(host=
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
+
app = Flask(__name__)
|
|
|
|
| 10 |
|
| 11 |
+
# Load your model
|
| 12 |
+
model = load_model('./best_model.h5', compile=False)
|
| 13 |
|
| 14 |
+
# Itsekiri digit labels
|
| 15 |
+
itsekiri_labels = [
|
| 16 |
+
'Méene', 'Méji', 'Métà', 'Mérin', 'Márùn',
|
| 17 |
+
'Méfa', 'Méje', 'Méjọ', 'Méṣan', 'Méwà'
|
| 18 |
+
]
|
| 19 |
|
| 20 |
+
IMG_SIZE = (228, 228)
|
| 21 |
+
|
| 22 |
+
def preprocess_image(base64_str):
|
| 23 |
+
header, encoded = base64_str.split(',', 1)
|
| 24 |
+
img_bytes = base64.b64decode(encoded)
|
| 25 |
img = Image.open(BytesIO(img_bytes)).convert('RGB')
|
| 26 |
+
img = img.resize(IMG_SIZE)
|
| 27 |
+
img_array = image.img_to_array(img)
|
| 28 |
img_array = np.expand_dims(img_array, axis=0)
|
| 29 |
+
img_array = img_array.astype('float32') / 255.0
|
| 30 |
return img_array
|
| 31 |
|
| 32 |
@app.route('/')
|
|
|
|
| 35 |
|
| 36 |
@app.route('/predict', methods=['POST'])
|
| 37 |
def predict():
|
| 38 |
+
data = request.get_json(force=True)
|
| 39 |
+
img_data = data['image']
|
| 40 |
+
img_array = preprocess_image(img_data)
|
| 41 |
+
pred_probs = model.predict(img_array)
|
| 42 |
+
pred_index = np.argmax(pred_probs)
|
| 43 |
+
pred_label = itsekiri_labels[pred_index]
|
| 44 |
+
confidence = float(pred_probs[0][pred_index] * 100)
|
|
|
|
|
|
|
| 45 |
return jsonify({
|
| 46 |
'prediction': pred_label,
|
| 47 |
'confidence': confidence
|
| 48 |
})
|
| 49 |
|
| 50 |
if __name__ == '__main__':
|
| 51 |
+
app.run(host='0.0.0.0', port=7860)
|