<reponame>Zehua-Chen/eye-axis-adjustment<filename>web-app/main.py
from flask import *
import os
import cv2
import json
import base64
import numpy as np
import shutil

from diagnostic import performDiagnostic
from util import *

app = Flask(__name__)

# Since the web app is a single page application, every sub directory
# is only pointing to the index.html file in static folder.


@app.route("/", methods=["GET"])
@app.route("/diagnostic", methods=["GET"])
@app.route("/welcome", methods=["GET"])
@app.route("/about", methods=["GET"])
def index():
    """
    Render the index page, which is the index.html file in template folder.

    Index pages serves as a welcome screen.

    Returns:
        The rendered index.html page
    """
    return app.send_static_file("index.html")


@app.route("/new_update", methods=["POST"])
def new_update():
    """
    Handle image sent from the client

    """
    if not os.path.exists(PATH_TO_RESPONSE_IMAGES):
        os.makedirs(PATH_TO_RESPONSE_IMAGES)

    data = json.loads(request.data)
    frames = []

    for eye in data["eyes"]:
        frame = jsonStringImageToCVImage(eye)
        frames.append(frame)

    responseContent = performDiagnostic(frames)
    saveToDisk(frames[0], data["id"])

    responseJson = json.dumps(responseContent)
    return Response(responseJson, mimetype="application/json")

@app.route("/response_<uuid>", methods=["GET"])
def responses(uuid):
    """
    Respond to client's request to get the processed image

    Discussion:
        Format: /response_<uuid>, uuid is a unique number generated by an instance of a client.
    """
    fileName = "{}.jpg".format(uuid)
    print(fileName)
    return send_from_directory("caches", fileName)

@app.errorhandler(404)
def notFound(e):
    """
    Handle page not found error

    Discussion:
        Since this is a single page application, index.html generated by angular will still
        be returned.
    """
    return app.send_static_file("index.html")


if __name__ == "__main__":
    
    # TODO: enable the below line and replace host with your ip address 
    # if you want to serve the website over local network
    # app.run(ssl_context=("cert.pem","key.pem"), host='10.192.142.136')
    
    # TODO: enable the below line if you are running on localhost
    app.run()

    # Clean up the cache folder
    if os.path.exists(PATH_TO_RESPONSE_IMAGES):
        print()
        print("==========")
        print("Cleaning up")

        print("1. Delete /caches folder")
        shutil.rmtree(PATH_TO_RESPONSE_IMAGES)

from flask import *
import os
import cv2
import json
import base64
import numpy as np
import shutil

from diagnostic import performDiagnostic
from util import *

app = Flask(__name__)

# Since the web app is a single page application, every sub directory
# is only pointing to the index.html file in static folder.


@app.route("/", methods=["GET"])
@app.route("/diagnostic", methods=["GET"])
@app.route("/welcome", methods=["GET"])
@app.route("/about", methods=["GET"])
def index():
    """
    Render the index page, which is the index.html file in template folder.

    Index pages serves as a welcome screen.

    Returns:
        The rendered index.html page
    """
    return app.send_static_file("index.html")


@app.route("/new_update", methods=["POST"])
def new_update():
    """
    Handle image sent from the client

    """
    if not os.path.exists(PATH_TO_RESPONSE_IMAGES):
        os.makedirs(PATH_TO_RESPONSE_IMAGES)

    data = json.loads(request.data)
    frames = []

    for eye in data["eyes"]:
        frame = jsonStringImageToCVImage(eye)
        frames.append(frame)

    responseContent = performDiagnostic(frames)
    saveToDisk(frames[0], data["id"])

    responseJson = json.dumps(responseContent)
    return Response(responseJson, mimetype="application/json")

@app.route("/response_<uuid>", methods=["GET"])
def responses(uuid):
    """
    Respond to client's request to get the processed image

    Discussion:
        Format: /response_<uuid>, uuid is a unique number generated by an instance of a client.
    """
    fileName = "{}.jpg".format(uuid)
    print(fileName)
    return send_from_directory("caches", fileName)

@app.errorhandler(404)
def notFound(e):
    """
    Handle page not found error

    Discussion:
        Since this is a single page application, index.html generated by angular will still
        be returned.
    """
    return app.send_static_file("index.html")


if __name__ == "__main__":
    
    # TODO: enable the below line and replace host with your ip address 
    # if you want to serve the website over local network
    # app.run(ssl_context=("cert.pem","key.pem"), host='10.192.142.136')
    
    # TODO: enable the below line if you are running on localhost
    app.run()

    # Clean up the cache folder
    if os.path.exists(PATH_TO_RESPONSE_IMAGES):
        print()
        print("==========")
        print("Cleaning up")

        print("1. Delete /caches folder")
        shutil.rmtree(PATH_TO_RESPONSE_IMAGES)
# Since the web app is a single page application, every sub directory # is only pointing to the index.html file in static folder. Render the index page, which is the index.html file in template folder.      Index pages serves as a welcome screen.      Returns:         The rendered index.html page Handle image sent from the client Respond to client's request to get the processed image      Discussion:         Format: /response_<uuid>, uuid is a unique number generated by an instance of a client. Handle page not found error      Discussion:         Since this is a single page application, index.html generated by angular will still         be returned. # TODO: enable the below line and replace host with your ip address # if you want to serve the website over local network # app.run(ssl_context=("cert.pem","key.pem"), host='10.192.142.136') # TODO: enable the below line if you are running on localhost # Clean up the cache folder