Skip to main content
In our previous lesson, we encountered a 404 error when sending a request, and the root cause was not immediately obvious. In this guide, we’ll explore classic debugging techniques enhanced with generative AI insights and refactor our application step by step for better performance and security.

Initial Application Setup

Below is the initial code for creating the Flask application instance. In this block, we load the configuration, enable debug mode, and register our routes. Notice that the configuration is printed and a confirmation is logged once the routes are imported:
When we ran the application, the terminal output looked similar to the following:
The repeated 404 errors pointed to an issue with the /upload route. Let’s take a closer look at that function.

The Initial Upload Route

Initially, the upload route function manually validates the quality parameter, saves the image to a local directory, processes it with OpenCV, and returns a JSON message containing the path to the processed image:

First Improvement: Directly Returning Processed Image

To improve performance and reduce disk I/O, we leveraged generative AI insights to modify the function. Instead of writing the image to disk, we directly read the image from the request and return it using Flask’s send_file:

Final Refinement: Processing Images In-Memory

In our final version, we entirely eliminate disk I/O by processing the image in memory. We use np.frombuffer and OpenCV’s decoding and encoding functions to directly manipulate the image data, then send the processed image back as binary data:
The in-memory processing significantly improves performance by reducing disk I/O, making the application more efficient in handling image uploads.

Adopting the Application Factory Pattern

While testing, an error was encountered indicating that the app object was undefined in the routes.py file. To resolve this, we refactored the application following the application factory pattern. The __init__.py was updated as follows:
Subsequently, routes.py was updated to remove the dependency on a global app object by using a Flask Blueprint:

Application Testing

After refactoring, the application properly initialized using the Flask application factory. The Blueprint registration ensured that the upload route was correctly integrated. Running the application with:
resulted in a 200 OK response when sending a POST request to /upload. Testing various quality parameters (e.g., quality = 10 or 100) revealed clear and noticeable differences in image compression quality.

Conclusion

This debugging session demonstrates the benefits of leveraging generative AI to propose improvements, such as eliminating unnecessary disk I/O and processing images entirely in memory. Although the process required some trial and error, the final design is robust and efficient.
Consider enhancing error handling and adding additional code comments in future iterations to further improve maintainability and clarity.
Happy coding, and see you in the next lesson!

Watch Video