from flask import Flask, render_template, request, jsonify | |
import numpy as np | |
from PIL import Image | |
import io | |
import base64 | |
from tensorflow.keras.models import load_model | |
app = Flask(__name__) | |
model = load_model("letter_cnn_model.h5") | |
labels = [chr(ord('a') + i) for i in range(26)] | |
def index(): | |
return render_template("index.html") | |
def predict(): | |
data = request.json["image"] | |
image_data = base64.b64decode(data.split(",")[1]) | |
img = Image.open(io.BytesIO(image_data)).convert("L").resize((28, 28)) | |
img = 255 - np.array(img) | |
img = img / 255.0 | |
img = img.reshape(1, 28, 28, 1) | |
pred = model.predict(img) | |
index = np.argmax(pred[0]) | |
label = labels[index].upper() | |
confidence = float(pred[0][index]) | |
return jsonify({ | |
"label": label, | |
"confidence": confidence | |
}) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=7860) | |