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)

[((221, 236), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (226, 236), False, 'from flask import Flask, request, redirect, Response\n'), ((470, 515), 'face_recognition.load_image_file', 'face_recognition.load_image_file', (['file_stream'], {}), '(file_stream)\n', (502, 515), False, 'import face_recognition\n'), ((660, 711), 'firestore.add_member', 'db.add_member', (['name', 'access', 'unknown_face_encodings'], {}), '(name, access, unknown_face_encodings)\n', (673, 711), True, 'import firestore as db\n'), ((857, 877), 'firestore.encoding.update', 'db.encoding.update', ([], {}), '()\n', (875, 877), True, 'import firestore as db\n'), ((889, 913), 'flask.request.form.get', 'request.form.get', (['"""name"""'], {}), "('name')\n", (905, 913), False, 'from flask import Flask, request, redirect, Response\n'), ((927, 953), 'flask.request.form.get', 'request.form.get', (['"""access"""'], {}), "('access')\n", (943, 953), False, 'from flask import Flask, request, redirect, Response\n'), ((1326, 1349), 'flask.redirect', 'redirect', (['"""/video_feed"""'], {}), "('/video_feed')\n", (1334, 1349), False, 'from flask import Flask, request, redirect, Response\n'), ((1417, 1437), 'firestore.encoding.update', 'db.encoding.update', ([], {}), '()\n', (1435, 1437), True, 'import firestore as db\n'), ((1451, 1483), 'flask.request.form.get', 'request.form.get', (['"""updateMember"""'], {}), "('updateMember')\n", (1467, 1483), False, 'from flask import Flask, request, redirect, Response\n'), ((1501, 1531), 'flask.request.form.get', 'request.form.get', (['"""changeName"""'], {}), "('changeName')\n", (1517, 1531), False, 'from flask import Flask, request, redirect, Response\n'), ((1551, 1583), 'flask.request.form.get', 'request.form.get', (['"""changeAccess"""'], {}), "('changeAccess')\n", (1567, 1583), False, 'from flask import Flask, request, redirect, Response\n'), ((1643, 1693), 'firestore.update_member', 'db.update_member', (['member', 'changeName', 'changeAccess'], {}), '(member, changeName, changeAccess)\n', (1659, 1693), True, 'import firestore as db\n'), ((2227, 2266), 'firestore.history_log.get_most_recent_member', 'db.history_log.get_most_recent_member', ([], {}), '()\n', (2264, 2266), True, 'import firestore as db\n'), ((1974, 1997), 'firestore.encoding.get_names', 'db.encoding.get_names', ([], {}), '()\n', (1995, 1997), True, 'import firestore as db\n'), ((2089, 2108), 'camera.gen_frames', 'camera.gen_frames', ([], {}), '()\n', (2106, 2108), False, 'import camera\n'), ((1071, 1092), 'flask.redirect', 'redirect', (['request.url'], {}), '(request.url)\n', (1079, 1092), False, 'from flask import Flask, request, redirect, Response\n'), ((1182, 1203), 'flask.redirect', 'redirect', (['request.url'], {}), '(request.url)\n', (1190, 1203), False, 'from flask import Flask, request, redirect, Response\n'), ((1821, 1855), 'flask.request.form.get', 'request.form.get', (['"""cameraDuration"""'], {}), "('cameraDuration')\n", (1837, 1855), False, 'from flask import Flask, request, redirect, Response\n'), ((1933, 1956), 'firestore.encoding.get_names', 'db.encoding.get_names', ([], {}), '()\n', (1954, 1956), True, 'import firestore as db\n'), ((606, 642), 'face_recognition.face_encodings', 'face_recognition.face_encodings', (['img'], {}), '(img)\n', (637, 642), False, 'import face_recognition\n')]