-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
184 lines (148 loc) · 6.09 KB
/
Copy pathutils.py
File metadata and controls
184 lines (148 loc) · 6.09 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
174
175
176
177
178
179
180
181
182
183
184
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageOps
import cv2
import os
import tensorflow as tf
import hashlib
import cloudinary
from cloudinary.uploader import upload
import io
from exceptions import DigitNotFoundError
import streamlit as st
CLOUD_UPLOAD = True
# cloudinary_auth = dict()
# cloudinary_auth['CLOUD_NAME'] = os.getenv('CLOUD_NAME')
# cloudinary_auth['API_KEY'] = os.getenv('API_KEY')
# cloudinary_auth['API_SECRET'] = os.getenv('API_SECRET')
#
# if CLOUD_UPLOAD and any([v is None for v in cloudinary_auth]):
# warnings.warn('Cloud upload was disabled because Cloudinary API keys not found')
# CLOUD_UPLOAD = False
# else:
# # Config
# ...
model = tf.keras.models.load_model('models/tuned-mnist.h5')
def save_png(img_):
img = Image.fromarray(img_, mode='RGBA')
img_hash = hashlib.md5(img.tobytes()).hexdigest()
if CLOUD_UPLOAD:
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
cloudinary.config(
cloud_name=st.secrets.cloudinary.cloud_name,
api_key=st.secrets.cloudinary.api_key,
api_secret=st.secrets.cloudinary.api_secret,
secure=True
)
upload(img_byte_arr, public_id=f"draw2text/canvas/{img_hash}")
else:
if not os.path.isdir('./img'):
os.makedirs('./img')
img.save('./img/' + img_hash + '.png')
def save_digit(img_, metadata):
img_hash = hashlib.md5(img_.tobytes()).hexdigest()
if CLOUD_UPLOAD:
img_byte_arr = io.BytesIO()
img_.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
cloudinary.config(
cloud_name=st.secrets.cloudinary.cloud_name,
api_key=st.secrets.cloudinary.api_key,
api_secret=st.secrets.cloudinary.api_secret,
secure=True
)
upload(img_byte_arr, public_id=f"draw2text/digits/{img_hash}", context=metadata)
else:
if not os.path.isdir('./img'):
os.makedirs('./img')
img_.save('./img/' + img_hash + '.png')
def plot_numbers(img_array, true_label=None, predicted_label=None, n_rows=3, n_cols=10, title=''):
"""
Display a grid of images using matplotlib.
Args:
- img_array (numpy.ndarray): An array of images.
- true_label (list, optional): A list of true labels for the images. Default is None.
- predicted_label (list, optional): A list of predicted labels for the images. Default is None.
- n_rows (int, optional): The number of rows in the grid. Default is 3.
- n_cols (int, optional): The number of columns in the grid. Default is 10.
- title (str, optional): The title of the plot. Default is an empty string.
Returns:
None.
"""
if not isinstance(img_array, np.ndarray):
raise ValueError('img_array need to be an numpy array')
# if img_array.shape[0] < n_cols:
# n_cols = img_array.shape[0]
# if img_array.shape[0] < 10:
# n_rows = 1
min_color = 0
max_color = 255
fig = plt.figure(figsize=(1.2 * n_cols, 1.2*n_rows))
if title:
plt.suptitle(title, weight='bold', size=18)
for col in range(n_cols):
for row in range(n_rows):
index = row + max(col, 0) * n_rows
if index >= img_array.shape[0]:
break
plt.subplot(n_rows, n_cols, index + 1)
plt.imshow(
img_array[index],
cmap='binary',
vmin=min_color, vmax=max_color,
interpolation="nearest")
plt.axis('off')
if predicted_label is not None:
if true_label is not None:
plt.title(f'label={true_label[index]}\npredicted={predicted_label[index]}')
else:
plt.title(f'predicted={predicted_label[index]}')
elif true_label is not None:
plt.title(f'label={true_label[index]}')
plt.tight_layout()
return fig
def resize_image(img_array, shape=(28, 28), resample=Image.BICUBIC, pad=2):
pil_image = Image.fromarray(img_array, mode='L')
drawable_shape = (shape[0] - pad * 2, shape[1] - pad * 2)
wpercent = (drawable_shape[1] / float(pil_image.size[0]))
hsize = int((float(pil_image.size[1]) * float(wpercent)))
img = pil_image.resize((drawable_shape[1], hsize), resample=resample)
img_resized = ImageOps.pad(img, size=drawable_shape, color=0, method=resample)
img_padded = ImageOps.expand(img_resized, border=pad)
return img_padded
def crop_canvas_digits(img_array):
# Apply thresholding to convert the grayscale image to a binary image
_, thresh = cv2.threshold(img_array, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# Find contours in the binary image
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filter the contours based on size and aspect ratio
digits = []
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
digit = thresh[y:y + h, x:x + w]
resized_digit = resize_image(digit, (28, 28))
digits.append(resized_digit)
return digits
def predict_drawings(img_):
if isinstance(img_, np.ndarray):
if len(img_.shape) == 3:
# Open image and convert to array ranging from 0-255
pil_image = Image.fromarray(img_, mode='RGBA')
gray_img = ImageOps.grayscale(pil_image)
img_array = np.array(gray_img)
else:
pass
else:
raise ValueError('img_ must be an array')
digits = crop_canvas_digits(img_array)
if len(digits) == 0:
raise DigitNotFoundError("Couldn't find any digit")
digits_array = [np.array(d) for d in digits]
predicted_labels = np.argmax(model.predict(np.array(digits_array)), axis=1)
return digits, predicted_labels
def get_accuracy_by_digit(y_true, y_pred):
df = pd.DataFrame({'y_pred': y_pred, 'y_true': y_true, 'right': y_pred == y_true})
return df.groupby('y_true')['right'].sum() / df.groupby('y_true')['right'].count()