-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (51 loc) · 2.15 KB
/
app.py
File metadata and controls
65 lines (51 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
from flask import Flask, request, redirect, url_for, render_template, send_from_directory,flash
from werkzeug.utils import secure_filename
from facial_emotion_recognition import EmotionRecognition
import cv2
import numpy as np
UPLOAD_FOLDER ='static/uploads/'
DOWNLOAD_FOLDER = 'static/downloads/'
ALLOWED_EXTENSIONS = {'jpg', 'png','.jpeg'}
app = Flask(__name__, static_url_path="/static")
# APP CONFIGURATIONS
app.config['SECRET_KEY'] = 'opencv'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
# limit upload size upto 6mb
app.config['MAX_CONTENT_LENGTH'] = 6 * 1024 * 1024
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file attached in request')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename))
process_file(os.path.join(UPLOAD_FOLDER, filename), filename)
data={
"processed_img":'static/downloads/'+filename,
"uploaded_img":'static/uploads/'+filename
}
return render_template("index.html",data=data)
return render_template('index.html')
def process_file(path, filename):
detect_object(path, filename)
def detect_object(path, filename):
er = EmotionRecognition(device='cpu')
image = cv2.imread(path)
frame = er.recognise_emotion(image, return_type='BGR')
cv2.imwrite(f"{DOWNLOAD_FOLDER}{filename}",frame)
# download
# @app.route('/uploads/<filename>')
# def uploaded_file(filename):
# return send_from_directory(app.config['DOWNLOAD_FOLDER'], filename, as_attachment=True)
if __name__ == '__main__':
app.run()