-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrafficsign_prediction_api.py
58 lines (46 loc) · 1.46 KB
/
trafficsign_prediction_api.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
'''
Author: Ambareesh Ravi
Date: Jul 31, 2021
Title: trafficsign_prediction_api.py
Description:
Contains the API to classify images by uploading them
'''
from prediction import *
from PIL import Image
import numpy as np
import io
import flask
from flask import Flask
app = Flask(__name__)
@app.route("/predict", methods=["POST"])
def predict():
'''
Method to predict an image that is uploaded
Args:
-
Returns:
results as json
Exception:
-
'''
data = {"success": False}
# ensure an image was properly uploaded to our endpoint
if flask.request.method == "POST":
if flask.request.files.get("image"):
# read the image in PIL format
image = flask.request.files["image"].read()
image = Image.open(io.BytesIO(image)).convert('RGB')
# preprocess the image and prepare it for classification
im = tester.resize_im_array(np.array(image))
pred = tester.predict_im_array(im)
data["result"] = pred[-1]
data["success"] = True
# return the data dictionary as a JSON response
return flask.jsonify(data)
if __name__ == '__main__':
# Define the tester object
tester = Tester()
# Run the application
app.run(host='127.0.0.1', debug=True, port = 12345)
# To access externally
# app.run(host='<ip address>', debug=True, threaded=True, use_reloader=False, port = <port>, ssl_context='adhoc')