app.py 2.01 KB
import sys
import os

from flask.helpers import url_for
from face_emotion_recognition import face_recognition, video2
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', face_imgs=find_face_imgs())


@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')
def upload_video():
    f = request.files.get('video')
    f.save("./static/video/" + secure_filename(f.filename))
    return 'video uploaded successfully'


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