Skip to content

Instantly share code, notes, and snippets.

@simon-mo
Last active October 4, 2019 18:44
Show Gist options
  • Select an option

  • Save simon-mo/24ae9da9739f6b8d5d66fb0ab9a85992 to your computer and use it in GitHub Desktop.

Select an option

Save simon-mo/24ae9da9739f6b8d5d66fb0ab9a85992 to your computer and use it in GitHub Desktop.
@app.route('/imageclassifier/predict/', methods=['POST'])
def image_classifier():
# Decoding and pre-processing base64 image
img = image.img_to_array(
image.load_img(
# Request from base 64!
BytesIO(base64.b64decode(request.form['b64'])),
# Need to resize it and normalize to 0-1
target_size=(224, 224))) / 255.
# Type casting!
img = img.astype('float32')
# Image preprocessing
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
img = normalize(img)
# perform the inference
pred = my_fancy_model(img)
# Returning JSON response to the frontend
# Multiple nested wrangling required
return jsonify(np.array(pred['predictions'])[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment