Emeritus-21 commited on
Commit
a0d48e7
·
verified ·
1 Parent(s): 358a9a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -1,26 +1,32 @@
1
- from flask import Flask, render_template, request, jsonify
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
- # Load trained VGG16 model
9
- model = load_model("best_model.h5")
10
 
11
- # Class names from your training dataset
12
- class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
13
 
14
- app = Flask(__name__)
 
 
 
 
15
 
16
- def preprocess_image(img_data):
17
- """Decode base64, resize to (228, 228), rescale 0-1, add batch dim"""
18
- img_data = img_data.split(',')[1]
19
- img_bytes = base64.b64decode(img_data)
 
20
  img = Image.open(BytesIO(img_bytes)).convert('RGB')
21
- img = img.resize((228, 228))
22
- img_array = np.array(img, dtype=np.float32) / 255.0 # rescale
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
- image_data = data['image']
34
- img_array = preprocess_image(image_data)
35
-
36
- predictions = model.predict(img_array)
37
- pred_idx = int(np.argmax(predictions[0]))
38
- pred_label = class_names[pred_idx]
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="0.0.0.0", port=7860, debug=False)
 
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)