Skip to content

Instantly share code, notes, and snippets.

@vatsalsaglani
Created November 30, 2019 11:11
Show Gist options
  • Save vatsalsaglani/a5b4b6dbeba8a3f6bc99f38619c3bf79 to your computer and use it in GitHub Desktop.
Save vatsalsaglani/a5b4b6dbeba8a3f6bc99f38619c3bf79 to your computer and use it in GitHub Desktop.
import subprocess
import io
from flask_cors import CORS
from flask import Flask, request, render_template, jsonify
@app.route('/predict_api', methods=['GET', 'POST'])
def predict_classes():
if request.method == 'GET':
return render_template('home.html', value = "Image")
if request.method == 'POST':
if 'file' not in request.files:
return "Image not uploaded"
file = request.files['file'].read()
try:
img = Image.open(io.BytesIO(file))
except IOError:
return jsonify(predictions = "Not an Image, Upload a proper image file", preds_prob = "")
img = img.convert("RGB")
img.save("input.jpg", "JPEG")
p = (subprocess.check_output(["python /Users/vatsalsaglani/Desktop/njunk/personal/CelebA_API/predict.py input.jpg"], shell=True).decode("utf-8"))
pp = p.split("@")
preds = pp[0]
pred_proba = pp[1]
pred_proba = pred_proba.split("-")
return jsonify(predicitions = preds, preds_prob = pred_proba)
if __name__ == '__main__':
app.run(debug = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment