-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
66 lines (57 loc) · 1.73 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
66
import re
import base64
from PIL import Image
import io
import numpy as np
import os
from flask import Flask, request, send_from_directory, send_file, json
app = Flask(__name__)
from tensorflow import keras
keras.backend.clear_session()
model = None
@app.before_first_request
def load_model():
# load the pre-trained Keras model (here we are using a model
# pre-trained on ImageNet and provided by Keras, but you can
# substitute in your own networks just as easily)
global model
model = keras.models.load_model(os.path.join(app.root_path, 'net4.h5'))
model._make_predict_function()
# Serve static files
@app.route("/public/<path:path>")
def public_files(path):
return send_from_directory('public', path)
# Main page
@app.route("/")
def index():
return send_file('public/index.html')
# Prediction endpoint
@app.route("/predict", methods=['POST'])
def predict():
classes = [
'flamingo',
'cake',
'campfire',
'angel',
'palm tree',
'remote control',
'rhinoceros',
'The Eiffel Tower',
'The Mona Lisa',
'wine bottle',
'hot air balloon',
'skateboard',
'map',
'underwear',
'roller coaster'
]
image_data_url = request.json['image']
image_string = re.search(r'base64,(.*)', image_data_url).group(1)
image_bytes = io.BytesIO(base64.b64decode(image_string))
PIL_image = Image.open(image_bytes)
image_arr = np.array(PIL_image)[:, :, 0] / 255.0
image_arr = image_arr.flatten()
image_input = np.expand_dims(image_arr, axis=0)
predictions = model.predict(image_input)
ret = json.dumps({'conf': str(predictions.max() * 100)[:4], 'class': classes[np.argmax(predictions)]})
return ret