-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
30 lines (26 loc) · 1011 Bytes
/
main.py
File metadata and controls
30 lines (26 loc) · 1011 Bytes
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
import tensorflow as tf
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Expose-Headers": "*",
}
model = None
def hello(request):
if request.method != 'POST':
return '', 200, headers
data_url = request.get_data(as_text=True)
print(data_url)
img = tf.io.decode_base64(data_url.split(',')[-1].translate(str.maketrans({'+':'-','/':'_'})))
img = tf.image.decode_png(img, channels=3)
img = tf.image.resize(img, [60, 300]) / 255.0
batch = tf.expand_dims(img, 0)
global model
if model is None:
model = tf.keras.models.load_model('xserver_captcha.keras')
preds = model(batch)
input_len = tf.fill([tf.shape(preds)[0]], tf.shape(preds)[1])
decoded = tf.keras.backend.ctc_decode(preds, input_length=input_len, greedy=True)[0][0]
code = ''.join(str(c) for c in decoded.numpy()[0] if c >= 0)
print('iVBOR', code)
return code, 200, headers