import face_recognition
from flask import Flask, request, redirect, Response
import camera
import firestore as db

# You can change     this to any folder on your system
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

app = Flask(__name__)


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


def detect_faces_in_image(name, access, file_stream):
    # Load the uploaded image filed
    img = face_recognition.load_image_file(file_stream)
    # Get face encodings for any faces in the uploaded image
    unknown_face_encodings = face_recognition.face_encodings(img)[0].tolist()

    db.add_member(name, access, unknown_face_encodings)
    return ('', 204)


@app.route('/')
def root():
    return ('', 204)


@app.route('/upload', methods=['GET', 'POST'])
def upload_image():
    db.encoding.update()
    name = request.form.get("name")
    access = request.form.get("access")
    access = int(access)
    if request.method == 'POST':
        if 'file' not in request.files:
            return redirect(request.url)

        file = request.files['file']
        if file.filename == '':
            return redirect(request.url)

        if file and allowed_file(file.filename):
            return detect_faces_in_image(name, access, file)
    return redirect('/video_feed')


@app.route('/update', methods=['GET', 'POST'])
def update():
    db.encoding.update()
    member = request.form.get("updateMember")
    changeName = request.form.get("changeName")
    changeAccess = request.form.get("changeAccess")
    if changeAccess == None:
        changeAccess = ""
    db.update_member(member, changeName, changeAccess)

    return ('', 204)


@app.route('/configuration',  methods=['GET', 'POST'])
def config():
    db.config_camera_interval(int(request.form.get('cameraDuration')))
    return('', 204)


@app.route('/members')
def members():
    print(type(db.encoding.get_names()))
    return str(db.encoding.get_names())


@app.route('/video_feed')
def video_feed():
    print('CAMERA RUN')
    return Response(camera.gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/recent_person')
def recent_person():
    return db.history_log.get_most_recent_member()


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5001, debug=True)

[((9, 6, 9, 21), 'flask.Flask', 'Flask', ({(9, 12, 9, 20): '__name__'}, {}), '(__name__)', False, 'from flask import Flask, request, redirect, Response\n'), ((19, 10, 19, 55), 'face_recognition.load_image_file', 'face_recognition.load_image_file', ({(19, 43, 19, 54): 'file_stream'}, {}), '(file_stream)', False, 'import face_recognition\n'), ((23, 4, 23, 55), 'firestore.add_member', 'db.add_member', ({(23, 18, 23, 22): 'name', (23, 24, 23, 30): 'access', (23, 32, 23, 54): 'unknown_face_encodings'}, {}), '(name, access, unknown_face_encodings)', True, 'import firestore as db\n'), ((34, 4, 34, 24), 'firestore.encoding.update', 'db.encoding.update', ({}, {}), '()', True, 'import firestore as db\n'), ((35, 11, 35, 35), 'flask.request.form.get', 'request.form.get', ({(35, 28, 35, 34): '"""name"""'}, {}), "('name')", False, 'from flask import Flask, request, redirect, Response\n'), ((36, 13, 36, 39), 'flask.request.form.get', 'request.form.get', ({(36, 30, 36, 38): '"""access"""'}, {}), "('access')", False, 'from flask import Flask, request, redirect, Response\n'), ((48, 11, 48, 34), 'flask.redirect', 'redirect', ({(48, 20, 48, 33): '"""/video_feed"""'}, {}), "('/video_feed')", False, 'from flask import Flask, request, redirect, Response\n'), ((53, 4, 53, 24), 'firestore.encoding.update', 'db.encoding.update', ({}, {}), '()', True, 'import firestore as db\n'), ((54, 13, 54, 45), 'flask.request.form.get', 'request.form.get', ({(54, 30, 54, 44): '"""updateMember"""'}, {}), "('updateMember')", False, 'from flask import Flask, request, redirect, Response\n'), ((55, 17, 55, 47), 'flask.request.form.get', 'request.form.get', ({(55, 34, 55, 46): '"""changeName"""'}, {}), "('changeName')", False, 'from flask import Flask, request, redirect, Response\n'), ((56, 19, 56, 51), 'flask.request.form.get', 'request.form.get', ({(56, 36, 56, 50): '"""changeAccess"""'}, {}), "('changeAccess')", False, 'from flask import Flask, request, redirect, Response\n'), ((59, 4, 59, 54), 'firestore.update_member', 'db.update_member', ({(59, 21, 59, 27): 'member', (59, 29, 59, 39): 'changeName', (59, 41, 59, 53): 'changeAccess'}, {}), '(member, changeName, changeAccess)', True, 'import firestore as db\n'), ((84, 11, 84, 50), 'firestore.history_log.get_most_recent_member', 'db.history_log.get_most_recent_member', ({}, {}), '()', True, 'import firestore as db\n'), ((73, 15, 73, 38), 'firestore.encoding.get_names', 'db.encoding.get_names', ({}, {}), '()', True, 'import firestore as db\n'), ((79, 20, 79, 39), 'camera.gen_frames', 'camera.gen_frames', ({}, {}), '()', False, 'import camera\n'), ((40, 19, 40, 40), 'flask.redirect', 'redirect', ({(40, 28, 40, 39): 'request.url'}, {}), '(request.url)', False, 'from flask import Flask, request, redirect, Response\n'), ((44, 19, 44, 40), 'flask.redirect', 'redirect', ({(44, 28, 44, 39): 'request.url'}, {}), '(request.url)', False, 'from flask import Flask, request, redirect, Response\n'), ((66, 34, 66, 68), 'flask.request.form.get', 'request.form.get', ({(66, 51, 66, 67): '"""cameraDuration"""'}, {}), "('cameraDuration')", False, 'from flask import Flask, request, redirect, Response\n'), ((72, 15, 72, 38), 'firestore.encoding.get_names', 'db.encoding.get_names', ({}, {}), '()', True, 'import firestore as db\n'), ((21, 29, 21, 65), 'face_recognition.face_encodings', 'face_recognition.face_encodings', ({(21, 61, 21, 64): 'img'}, {}), '(img)', False, 'import face_recognition\n')]