-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (128 loc) · 5.55 KB
/
main.py
File metadata and controls
173 lines (128 loc) · 5.55 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
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from typing import List
import numpy as np
import cv2
from PIL import Image
import tflite_runtime.interpreter as tflite
model = tflite.Interpreter("static/mnist.tflite")
model.allocate_tensors()
input_details = model.get_input_details()
output_details = model.get_output_details()
class_mapping = {0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine'
}
def model_predict(images_arr):
predictions = [0] * len(images_arr)
for i, val in enumerate(predictions):
model.set_tensor(input_details[0]['index'], images_arr[i].reshape((1, 28, 28, 1)))
model.invoke()
predictions[i] = model.get_tensor(output_details[0]['index']).reshape((10,))
prediction_probabilities = np.array(predictions)
argmaxs = np.argmax(prediction_probabilities, axis=1)
return argmaxs
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
def resize(image):
return cv2.resize(image, (28, 28))
#Expects image in grey
def resize_to_contour(image, plt=None):
ret, thresh = cv2.threshold(image.copy(), 75, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
preprocessed_digits = []
for c in contours:
x, y, w, h = cv2.boundingRect(c)
# Creating a rectangle around the digit in the original image (for displaying the digits fetched via contours)
cv2.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)
# Cropping out the digit from the image corresponding to the current contours in the for loop
digit = thresh[y:y + h, x:x + w]
# Resizing that digit to (18, 18)
resized_digit = cv2.resize(digit, (18, 18))
# Padding the digit with 5 pixels of black color (zeros) in each side to finally produce the image of (28, 28)
padded_digit = np.pad(resized_digit, ((5, 5), (5, 5)), "constant", constant_values=0)
# Adding the preprocessed digit to the list of preprocessed digits
preprocessed_digits.append(padded_digit)
return preprocessed_digits[-1]
@app.post("/uploadfiles/", response_class=HTMLResponse)
async def create_upload_files(files: List[UploadFile] = File(...)):
images = []
for file in files:
f = await file.read()
images.append(f)
images = [np.frombuffer(img, np.uint8) for img in images]
images_grey = [cv2.imdecode(img, 0) for img in images]
images_resized = [resize_to_contour(img) for img in images_grey]
names = [file.filename for file in files]
for image, name in zip(images_resized, names):
pillow_image = Image.fromarray(image)
pillow_image.save('static/' + name)
image_paths = ['static/' + name for name in names]
images_arr = np.array(images_resized, dtype=np.float32)
class_indexes = model_predict(images_arr)
class_predictions = [class_mapping[x] for x in class_indexes]
column_labels = ["Image", "Prediction"]
table_html = get_html_table(image_paths, class_predictions, column_labels)
content = head_html + """
<marquee width="525" behavior="alternate"><h1 style="color:red;font-family:Arial">Here's what the computer reads!</h1></marquee>
""" + str(table_html) + '''<br><form method="post" action="/">
<button type="submit">Home</button>
</form>'''
return content
@app.post("/", response_class=HTMLResponse)
@app.get("/", response_class=HTMLResponse)
async def main():
content = head_html + """
<marquee width="725" behavior="alternate"><h1 style="color:red;font-family:Arial"> Please upload your images below</h1></marquee>
<h3 style="font-family:Arial">Let's discover what numbers the computer will read</h3><br>
"""
original_paths = [ 'zero.jpg',
'one.jpg',
'two.jpg',
'three.jpg',
'four.jpg',
'five.jpg',
'six.jpg',
'seven.jpg',
'eight.jpg',
'nine.jpg',
]
full_original_paths = ['static/original/' + x for x in original_paths]
display_names = [filename.split('.')[0] for filename in original_paths]
column_labels = []
content = content + get_html_table(full_original_paths, display_names, column_labels)
content = content + """
<br/>
<br/>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
"""
return content
head_html = """
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body style="background-color:powderblue;">
<center>
"""
def get_html_table(image_paths, names, column_labels):
s = '<table align="center">'
if column_labels:
s += '<tr><th><h4 style="font-family:Arial">' + column_labels[
0] + '</h4></th><th><h4 style="font-family:Arial">' + column_labels[1] + '</h4></th></tr>'
for name, image_path in zip(names, image_paths):
s += '<tr><td><img height="80" src="/' + image_path + '" ></td>'
s += '<td style="text-align:center">' + name + '</td></tr>'
s += '</table>'
return s