Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (39 loc) · 1.48 KB
/
Copy pathmain.py
File metadata and controls
58 lines (39 loc) · 1.48 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
from flask import Flask, render_template, request, jsonify
import numpy as np
import tensorflow as tf
from PIL import Image
from io import BytesIO
app = Flask(__name__)
# Load the model using joblib
model = tf.keras.models.load_model('data_recognizer.h5')
@app.route('/')
def home():
return render_template('templates/templates/index.html')
@app.route('/predict', methods=['POST'])
def predict():
try:
# Get image file from the HTML form
file = request.files['image']
img_array = preprocess_image(file.read())
# Flatten the image data
img_array_flat = img_array.flatten()
# Make predictions
predictions = model.predict(np.array([img_array_flat]))
# Print raw predictions for debugging
print("Raw Predictions:", predictions)
digit = np.argmax(predictions)
return render_template('templates/templates/index.html', digit=int(digit))
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return render_template('templates/templates/index.html', error_message=error_message)
def preprocess_image(img_data):
# Convert bytes to PIL Image
img = Image.open(BytesIO(img_data))
# Resize and normalize the image
img = img.resize((28, 28))
img_array = np.array(img.convert('L')) # Convert to grayscale
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0
return img_array
if __name__ == '__main__':
app.run(debug=True)