Last active
March 23, 2020 06:03
-
-
Save maxoobot/9ff00024aa517462b1e3c304dfcbfe2d 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
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