from flask import Flask, render_template, url_for, request | |
import os | |
import requests | |
import cv2 as cv | |
app = Flask(__name__) | |
eas_url = "<EAS service URL>" | |
token = "EAS service token" | |
@app.route('/') | |
def index(): | |
uploaded = False | |
return render_template("index.html", uploaded=uploaded) | |
@app.route('/', methods=["POST"]) | |
def upload_and_predict(): | |
uploaded = True | |
# Create directory to hold uploaded images | |
target = "static/img" | |
if not os.path.isdir(target): | |
os.mkdir(target) | |
# Save uploaded image | |
file = request.files.get("file") | |
filename = file.filename | |
filepath = "/".join([target, filename]) | |
file.save(filepath) | |
# Set the request data and parameters | |
img = cv.imread(filepath, 1) | |
_, img_encoded = cv.imencode('.jpg', img) | |
body = img_encoded.tostring() | |
headers = { 'Authorization' : token } | |
# Send request to EAS | |
response = requests.post(eas_url, data=body, headers=headers) | |
return render_template("index.html", uploaded=uploaded, imgpath=filepath, prediction=response.text) | |
if __name__ == "__main__": | |
app.run(port=5000, debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment