0

I am using flask and a deep learning model in tensorflow to display some annotated video. The module works fine but the loading of the model is lazy due to the nature of the generator. Only after I launch the url in my browser the model is loaded, and thus, there is some delay before the actual detection start to occur. I wanted to load my module before-hand to avoid this behaviour.

So, instead of using a structure like this:

def main():
    app.run(host='0.0.0.0', debug=True)


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


if __name__ == '__main__':
    main()

apply_detections() here of course is the generator which detects the objects and then yields the annotated frame. I was aiming for something like this:

def main():
    loaded_model = load_model()  # this should load the model before-hand
    loaded_tracker = load_tracker() # same here. This should load the model before-hand
    app.run(host='0.0.0.0', debug=True)


@app.route('/')
def video_feed():
    return Response(apply_detections(loaded_model, loaded_tracker), mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    main()

I know I can pass some arguments via the url -like @app.route('/<args1>')- but obviously this could not help here (I couldn't pass the whole model as text in the url) and a second option would be to define the arguments as global ones but this also has its drawbacks.

So, my question is which is the right way to accomplish this argument passing?

1 Answer 1

0

I am using the Flask app context/ app factory pattern for this:

  • You create the Flask app in a create_app() function in __init__.py, load the model there during the app creation, and append it to the app context:

    # __init__.py
    def create_app():
        app = Flask(__name__)
        with app.app_context():
            current_app.model = load_model()
    
  • Then, in your task function, you can get the app context and access the model:

    # tasks.py
    
    # Create a minimal app context
    app = create_app()
    
    def run_model():
        with app.app_context():
            apply_detections(current_app.model)
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.