-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
111 lines (80 loc) · 3.46 KB
/
application.py
File metadata and controls
111 lines (80 loc) · 3.46 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from flask import Flask, render_template, request, jsonify
import numpy as np
import tensorflow as tf
import cv2
import base64
from app.model.model import load_model
from PIL import Image, ImageOps
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'app')))
app = Flask(__name__, template_folder='app/templates', static_folder='app/static')
# Load models
digit_model = load_model('app/models/handwritten.model.keras')
alphabet_model = load_model('app/models/best_alphabet_model.keras')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/digit')
def digit():
return render_template('digit.html')
@app.route('/soon')
def soon():
return render_template('soon.html')
@app.route('/alphabet')
def alphabet():
return render_template('alphabet.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/howitworks')
def howitworks():
return render_template('howitworks.html')
# Define a route in Flask application that listens for POST requests at '/predict_digit'.
@app.route('/predict_digit', methods=['POST'])
def predict_digit():
# Get the JSON data from the incoming request.
data = request.get_json()
# Decode the base64 encoded image data from the JSON into bytes.
img_data = base64.b64decode(data['image'])
# Write the decoded image data to a file named 'digit.png' in binary mode.
with open('digit.png', 'wb') as f:
f.write(img_data)
# Read the saved image file as a grayscale image using OpenCV.
img = cv2.imread('digit.png', cv2.IMREAD_GRAYSCALE)
# Resize the image to 28x28 pixels, which is the input size expected by the digit model.
img = cv2.resize(img, (28, 28))
# Invert the image colors if the image's average brightness is higher than 127.
# This is done because some digit classifiers expect the background to be dark and the digit to be light.
if np.mean(img) > 127:
img = np.invert(img)
# Normalize the pixel values to the range [0, 1] for better model performance.
img = img / 255.0
# Reshape the image array to match the input shape expected by the model (batch_size, height, width, channels).
img = img.reshape(1, 28, 28, 1)
# Use the digit model to predict the digit in the image.
prediction = digit_model.predict(img)
# Find the digit with the highest probability from the model's predictions.
digit = np.argmax(prediction)
# Calculate the confidence level of the prediction as the maximum probability.
confidence = np.max(prediction)
# Return the predicted digit and the confidence as a JSON response.
return jsonify({'digit': int(digit), 'confidence': float(confidence)})
@app.route('/predict_alphabet', methods=['POST'])
def predict_alphabet():
data = request.get_json()
img_data = base64.b64decode(data['image'])
with open('alphabet.png', 'wb') as f:
f.write(img_data)
img = cv2.imread('alphabet.png', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (28, 28))
if np.mean(img) > 127:
img = np.invert(img)
img = img / 255.0
img = img.reshape(1, 28, 28, 1)
prediction = alphabet_model.predict(img)
alphabet = chr(np.argmax(prediction) + ord('A'))
confidence = np.max(prediction) # Calculate maximum probability
return jsonify({'letter': alphabet, 'confidence': float(confidence)})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)