app.py 2.68 KB
import sys
import os

from flask.helpers import url_for
from face_emotion_recognition import face_recognition, video4
from flask import Flask, render_template
from flask.globals import request
from werkzeug.utils import redirect, secure_filename


def find_face_imgs():
    face_imgs = []
    id = 1
    for img_name in os.listdir('static/img'):
        if(img_name.rsplit('.')[1] == 'jpg' or img_name.rsplit('.')[1] == 'png'):
            face_imgs.append(
                {
                    'id': id,
                    'name': img_name.rsplit('.')[0],
                    'imgUrl': 'img/' + img_name
                })
            id += 1
    return face_imgs


# Flask 객체 인스턴스 생성
app = Flask(__name__)


@app.route('/', methods=('GET', 'POST'))  # 접속하는 url
def index():
    if request.method == 'POST':
        return render_template('index.html', face_imgs=find_face_imgs())

    elif request.method == 'GET':
        return render_template('index.html', face_imgs=find_face_imgs())


@app.route('/goTest', methods=('GET', 'POST'))  # 접속하는 url
def test():
    if request.method == 'GET':
        return render_template('test.html')


@app.route('/uploadFace', methods=('GET', 'POST'))
def upload_face():
    if request.method == 'GET':
        return render_template('upload.html')
    elif request.method == 'POST':
        f = request.files.get('file')
        f.save("./static/img/" + secure_filename(f.filename))
        face_recognition.face_to_npy()
        return redirect(url_for('index'))


@app.route('/deleteFace/<string:face_name>')
def delete_face(face_name):
    print("request좀 보여줘", face_name)
    os.remove("./static/img/" + face_name + '.jpg')
    face_recognition.face_to_npy()
    return redirect(url_for('index'))


@app.route('/uploadVideo', methods=('GET', 'POST'))
def upload_video():
    if request.method == 'POST':
        f = request.files.get('video')
        f.save("./static/video/" + secure_filename(f.filename))
        return redirect(url_for('test'))


@app.route('/faceEmotionRecognition', methods=('GET', 'POST'))
def faceEmotionRecognition():
    if request.method == 'POST':
        f = request.files.get('video')
        f.save("./static/video/" + secure_filename(f.filename))
        seconds = float(request.form['seconds'])
        face_emotion_dict = video4.videoDetector(
            seconds, secure_filename(f.filename))
        print(face_emotion_dict)
        file_name = 'video/' + secure_filename(f.filename)
        return render_template('result.html', face_emotion_dict=face_emotion_dict, face_imgs=find_face_imgs(), file_name=file_name)
        return redirect(url_for('test'))


if __name__ == "__main__":
    app.debug = True
    app.run()