-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
192 lines (142 loc) · 6.47 KB
/
main.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import Flask, request, jsonify, render_template
from tensorflow.keras import models
from PIL import Image
import numpy as np
import base64
import io
model_digit, model_character, character_list = None, None, None
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/canvas-digit')
def canvas_digit():
return render_template('canvas_digit.html')
@app.route('/canvas-character')
def canvas_character():
return render_template('canvas_character.html')
@app.route('/submit-digit', methods=['POST'])
def submit_digit_drawing():
data = request.json.get('image', None)
if data is None:
return jsonify({"error": "No image data provided"})
try:
if ',' in data:
header, encoded = data.split(',', 1)
else:
return jsonify({'error': 'Invalid Base64 image format'}), 400
# Decode the Base 64 image
image_data = base64.b64decode(encoded)
# Preprocess the image
image = Image.open(io.BytesIO(image_data)).convert('L')
processed_image = preprocess_image(image)
# Reshape for CNN
processed_image = reshape_for_cnn(processed_image)
# Predict the image
predictions = model_digit.predict(processed_image)
# Extract the predicted class and confidence
predicted_class = int(np.argmax(predictions)) # Convert NumPy scalar to int
confidence = float(np.max(predictions)) # Convert Numpy scalar to float
return jsonify({"prediction": predicted_class,
"confidence": confidence,
"probabilities": predictions.tolist()
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/submit-character', methods=['POST'])
def submit_character_drawing():
data = request.json.get('image', None)
if data is None:
return jsonify({"error": "No image data provided"})
try:
if ',' in data:
header, encoded = data.split(',', 1)
else:
return jsonify({'error': 'Invalid Base64 image format'}), 400
# Decode the Base 64 image
image_data = base64.b64decode(encoded)
# Preprocess the image
image = Image.open(io.BytesIO(image_data)).convert('L')
processed_image = preprocess_image(image)
# Reshape for CNN
processed_image = reshape_for_cnn(processed_image)
# Predict the image
predictions = model_character.predict(processed_image)
# Extract the predicted class and confidence
predicted_class = int(np.argmax(predictions)) # Convert NumPy scalar to int
confidence = float(np.max(predictions)) # Convert Numpy scalar to float
# Split predictions into uppercase and lowercase
upper_predictions = predictions[0][:26]
lower_predictions = predictions[0][26:]
return jsonify({"prediction": character_list[predicted_class],
"confidence": confidence,
"upper_probabilities": upper_predictions.tolist(),
"lower_probabilities": lower_predictions.tolist()
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/import-digit')
def import_digit_picture():
return render_template('import_digit.html')
@app.route('/import-character')
def import_character_picture():
return render_template('import_character.html')
@app.route('/upload-digit', methods=['POST'])
def upload_digit_picture():
file = request.files['file']
if file:
# Prepare the prediction
image = Image.open(file).convert('L') # Convert to grayscale
processed_image = preprocess_image(image)
# Reshape for CNN
processed_image = reshape_for_cnn(processed_image)
# Predict the image
predictions = model_digit.predict(processed_image)
# Extract the predicted class and confidence
predicted_class = int(np.argmax(predictions)) # Convert NumPy scalar to int
confidence = float(np.max(predictions)) # Convert Numpy scalar to float
return jsonify({"prediction": predicted_class,
"confidence": confidence,
"probabilities": predictions.tolist()
})
else:
return jsonify({"error": "No file uploaded"}), 400
@app.route('/upload-character', methods=['POST'])
def upload_character_picture():
file = request.files['file']
if file:
# Prepare the prediction
image = Image.open(file).convert('L') # Convert to grayscale
processed_image = preprocess_image(image)
# Reshape for CNN
processed_image = reshape_for_cnn(processed_image)
# Predict the image
predictions = model_character.predict(processed_image)
# Extract the predicted class and confidence
predicted_class = int(np.argmax(predictions)) # Convert NumPy scalar to int
confidence = float(np.max(predictions)) # Convert Numpy scalar to float
# Split predictions into uppercase and lowercase
upper_predictions = predictions[0][:26]
lower_predictions = predictions[0][26:]
return jsonify({"prediction": character_list[predicted_class],
"confidence": confidence,
"upper_probabilities": upper_predictions.tolist(),
"lower_probabilities": lower_predictions.tolist()
})
else:
return jsonify({"error": "No file uploaded"}), 400
def reshape_for_cnn(data: np.ndarray) -> np.ndarray:
return data.reshape(data.shape + (1,))
def preprocess_image(image, target_size=(28, 28)):
"""Resize and normalize the image."""
img = image.resize(target_size) # Resize image
img_array = np.array(img) / 255.0 # Convert to numpy array and normalize
return img_array[np.newaxis, :, :] # Add bathtch dimension
if __name__ == '__main__':
model_digit = models.load_model('./tf_practice/best_model_digit.h5')
model_character = models.load_model('./tf_practice/best_model_character.h5')
character_list = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
app.run(debug=True)