Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -97,7 +97,46 @@ num_to_char = layers.StringLookup(
|
|
97 |
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
|
98 |
)
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
def decode_batch_predictions(input_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
pred = loaded_model.predict(input_image)
|
102 |
input_len = np.ones(pred.shape[0]) * pred.shape[1]
|
103 |
# Use greedy search. For complex tasks, you can use beam search.
|
|
|
97 |
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
|
98 |
)
|
99 |
|
100 |
+
def distortion_free_resize(image, img_size):
|
101 |
+
w, h = img_size
|
102 |
+
image = tf.image.resize(image, size=(h, w), preserve_aspect_ratio=True)
|
103 |
+
|
104 |
+
# Check tha amount of padding needed to be done.
|
105 |
+
pad_height = h - tf.shape(image)[0]
|
106 |
+
pad_width = w - tf.shape(image)[1]
|
107 |
+
|
108 |
+
# only necessary if you want to do same amount of padding on both sides.
|
109 |
+
if pad_height % 2 != 0:
|
110 |
+
height = pad_height // 2
|
111 |
+
pad_height_top = height +1
|
112 |
+
pad_height_bottom = height
|
113 |
+
else:
|
114 |
+
pad_height_top = pad_height_bottom = pad_height // 2
|
115 |
+
|
116 |
+
if pad_width % 2 != 0:
|
117 |
+
width = pad_width // 2
|
118 |
+
pad_width_left = width + 1
|
119 |
+
pad_width_right = width
|
120 |
+
else:
|
121 |
+
pad_width_left = pad_width_right = pad_width // 2
|
122 |
+
|
123 |
+
image = tf.pad(
|
124 |
+
image, paddings=[
|
125 |
+
[pad_height_top, pad_height_bottom],
|
126 |
+
[pad_width_left, pad_width_right],
|
127 |
+
[0, 0],
|
128 |
+
],
|
129 |
+
image = tf.transpose(image, perm=[1,0,2])
|
130 |
+
image = tf.image.flip_left_right(image)
|
131 |
+
return image
|
132 |
+
|
133 |
def decode_batch_predictions(input_image):
|
134 |
+
|
135 |
+
width, height = input_image.size
|
136 |
+
img_size=(width, height)
|
137 |
+
input_image = distortion_free_resize(input_image, img_size)
|
138 |
+
input_image = tf.image.convert_image_dtype(input_image, tf.float32)/255.0
|
139 |
+
|
140 |
pred = loaded_model.predict(input_image)
|
141 |
input_len = np.ones(pred.shape[0]) * pred.shape[1]
|
142 |
# Use greedy search. For complex tasks, you can use beam search.
|