-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
56 lines (40 loc) · 1.45 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from flask import Flask, request, jsonify, url_for, render_template
import tensorflow as tf
import uuid
import os
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
import base64
from io import StringIO
from io import BytesIO
import cv2
import pandas as pd
app = Flask(__name__)
model = load_model('EMNIST-Balanced-Model.h5',compile=True)
ascii_map = pd.read_csv("mapping.csv")
@app.route('/')
def index():
return render_template("main.html")
@app.route('/predict',methods=["POST"])
def get_image():
canvasdata = request.form['canvasimg']
# print(canvasdata)
encoded_data = request.form['canvasimg'].split(',')[1]
nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
print(img.shape)
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_image = cv2.resize(gray_image, (28, 28), interpolation=cv2.INTER_LINEAR)
gray_image = gray_image/255.0
gray_image = np.expand_dims(gray_image, axis=-1)
img = np.expand_dims(gray_image, axis=0)
print('Image received: {}'.format(img.shape))
prediction = model.predict(img)
cl = list(prediction[0])
print("Prediction : ",ascii_map["Character"][cl.index(max(cl))])
## INITIAL TF VERSION -> 2.3.0
# print(prediction)
return render_template("main.html", value=ascii_map["Character"][cl.index(max(cl))])
if __name__ == '__main__':
app.run(debug=True)