Last active
January 24, 2019 05:54
-
-
Save rizalespe/52c81973355de3e2f3d8d2ec478e2007 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing all required library | |
import flask | |
import io | |
from flask_cors import CORS | |
from PIL import Image | |
from ResNetPredict import ResNetPredict | |
app = flask.Flask(__name__) | |
# Cross Origin Resource Sharing (CORS), making the URL accessible from cross-origin AJAX | |
CORS(app) | |
''' | |
Instantiate the defined class related to ResNet model, | |
you can add your own defined class here, for specific task. | |
''' | |
resNet_predict = ResNetPredict() | |
''' | |
Loading the model on memory, this is efficient way to store your model | |
on memory along the way, rather than load on every endpoint call | |
''' | |
resNet_predict.load_model() | |
''' | |
An endpoint for predicting an image using ResNet pre-trained model | |
you can add more endpoint. Here you will define the endpoint of URL | |
and method what you will use | |
''' | |
@app.route("/predict_resnet", methods=["POST"]) | |
def predict(): | |
data = {"success": False} | |
if flask.request.method == "POST": | |
# get paremeter named "image" as we only need one image file as parameter | |
if flask.request.files.get("image"): | |
# read the image in PIL format | |
image = flask.request.files["image"].read() | |
image = Image.open(io.BytesIO(image)) | |
image = resNet_predict.prepare_image(image, target=(224, 224)) | |
result_value = resNet_predict.predict(image) | |
data["predictions"] = [] | |
for (imagenetID, label, prob) in result_value[0]: | |
r = {"label": label, "probability": float(prob)} | |
data["predictions"].append(r) | |
data["success"] = True | |
# return the result as JSON text | |
return flask.jsonify(data) | |
if __name__ == "__main__": | |
print("loading the payload...") | |
print("starting server...") | |
app.run(host="localhost") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment