-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_api_server.py
44 lines (32 loc) · 1.23 KB
/
run_api_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
import flask
import argparse
import io
from lib import detection_grpc
from lib.detection import chainercv_parse
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--host', default='127.0.0.1', type=str)
parser.add_argument('--port', type=int, default=5000)
args = parser.parse_args()
app = flask.Flask(__name__)
object_detection = detection_grpc.MLClient('localhost:8888',
parse=chainercv_parse)
@app.route("/predict", methods=["POST"])
def predict():
# initialize the data dictionary that will be returned from the
# view
data = {"success": False}
# ensure an image was properly uploaded to our endpoint
if flask.request.method == "POST":
if flask.request.files.get("image"):
image = flask.request.files["image"].read()
# send to server from client
predictions = object_detection.predict(image)
# return result
data["predictions"] = predictions
data["success"] = True
# return the data dictionary as a JSON response
return flask.jsonify(data)
if __name__ == "__main__":
print("Run server")
app.run(host=args.host, port=args.port)