Commit 48bad758 authored by Hasini Piumika Alwis's avatar Hasini Piumika Alwis

app.py file

parent abb135f6
import cv2
import os
import json
import random
import io
import numpy as np
from anaconda_navigator.exceptions import exception_handler
from flask import Flask, render_template, Response, request, send_from_directory
from flask_cors import CORS, cross_origin
from keras.models import load_model, model_from_json
from base64 import decodestring
from emotion_detection import predict_image
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
path = os.getcwd()
# file Upload
UPLOAD_FOLDER = os.path.join(path, 'uploads')
TEMP_FOLDER = os.path.join(path, 'rendered')
if not os.path.isdir(TEMP_FOLDER):
os.mkdir(TEMP_FOLDER)
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['TEMP_FOLDER'] = TEMP_FOLDER
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
@cross_origin()
def image_upload():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
# flash('No file part')
return {'data': 'No file part'}
file = request.files['file']
if file.filename == '':
# flash('No file selected for uploading')
return {'data': 'No file selected for uploading'}
if file and allowed_file(file.filename):
upl_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(upl_path)
predicted_image, result_image = predict_image(upl_path)
_hash = str(random.getrandbits(128))
cv2.imwrite('rendered/result_' + _hash + '.jpg', result_image)
renders = os.path.join(app.root_path, app.config['TEMP_FOLDER'])
# response = flask.jsonify({'some': 'data'})
# response.headers.add('Access-Control-Allow-Origin', '*')
# print(type(result_image))
# return app.make_response((result_image.blob, 200,{'Access-Control-Allow-Origin':'*', 'Content-Type':'image/jpeg'}))
# response = send_file(io.BytesIO(result_image),
# mimetype='image/jpeg',as_attachment=True,attachment_filename='%s.jpg' % 1000)
# response.headers = {'Access-Control-Allow-Origin':'*', 'type':'image/jpeg'}
# return response
# response.content_type = 'image/jpeg'
return send_from_directory(directory=renders, filename='result_' + _hash + '.jpg')
else:
# flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return {'data': 'Allowed file types are png, jpeg, jpg'}
@app.route("/test", endpoint='index()')
@exception_handler
def index():
pass
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5000, debug=True)
print("OK")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment