-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.py
36 lines (27 loc) · 978 Bytes
/
app.py
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
# -------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# -------------------------------------------------------------
import os
import io
import re
import base64
from PIL import Image
from flask import Flask, request
from tf_model_helper import TFModel
app = Flask(__name__)
# Path to signature.json and model file
ASSETS_PATH = os.path.join(".", "./model")
TF_MODEL = TFModel(ASSETS_PATH)
@app.post('/predict')
def predict_image():
req = request.get_json(force=True)
image = _process_base64(req)
return TF_MODEL.predict(image)
def _process_base64(json_data):
image_data = json_data.get("image")
image_data = re.sub(r"^data:image/.+;base64,", "", image_data)
image_base64 = bytearray(image_data, "utf8")
image = base64.decodebytes(image_base64)
return Image.open(io.BytesIO(image))
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5000, debug=True)