-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
65 lines (48 loc) · 1.54 KB
/
server.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Authors: klin
Email: [email protected]
Date: 2020/3/3
"""
import base64
import time
from sanic import Sanic
from sanic.log import logger
from sanic.response import json
import tensorflow as tf
import numpy as np
app = Sanic()
# model settings
model = None
IMG_HEIGHT = IMG_WIDTH = 150
def load_model(model_path):
global model
# Recreate the exact same model
model = tf.keras.models.load_model(model_path)
print("Load model success...")
def preprocess_image(image):
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [IMG_HEIGHT, IMG_WIDTH])
image /= 255.0 # normalize to [0,1] range
image = np.expand_dims(image, axis=0)
return image
def predict(bs64_img):
imgdata = base64.decodebytes(bs64_img.encode('utf8'))
image = preprocess_image(imgdata)
predictions = model.predict([image])
return predictions[0][0]
@app.route('/')
async def default_page(request):
return json({'hello': 'world'})
@app.post('/predict')
async def foo(request):
img_str = request.json.get('bs64_img')
# logger.info("image size: {0:.1f} KB".format(len(img_str.encode()) / 1024))
start = time.time()
prediction = float(predict(img_str))
logger.info("prediction: {0:.4f}, cost: {1:.4f}".format(prediction, time.time()-start))
return json({"prediction": prediction})
if __name__ == '__main__':
load_model('./model/2') # load model from path, the path which you save model after train
app.run(host='0.0.0.0', port=8007)