python

Serve images on the fly in a binary form with Flask

In this post I am going to demonstrate how to serve images from a REST endpoint for a situation when the images are not stored as static files, but generated on the fly given some original form. This use case will be described with application of Flask, OpenCV and NumPy. A situation when this functionality is needed is when the original images are too large, and their downscaled representations are sometimes required, or when the original format of the images is too heavy to be served via a web service (like BMP).

Optimization with Python

Mathematical optimization is a pervasive task in numerous engineering applications. It is concerned with algorithmic search for a value (usually a vector) that minimizes or maximizes a predefined objective function. In machine learning (ML), for instance, a search for the best configuration of model parameters is performed with an optimization algorithm minimizing the objective function that measures misfit between the known response values and those predicted by the ML model.

Position independent code

In this blog post, I am going to provide a short overview of the concept of position independent code and its specification in CMake, particularly in projects using pybind11. Some time ago, when I was working on the FxIS project, which was developed in C++ with the additional Python extension realized with pybind11, I encountered a linking error, which included a line similar to this: /usr/bin/ld: libcore.a(sum.cpp.o): relocation R_X86_64_PC32 against symbol .

My favorite IPython Magic commands

Jupyter Notebook is a great tool for data science prototyping, visualization and sharing. The first code cell of every notebook I work on contains the following commands: %matplotlib inline %load_ext autoreload %autoreload 2 These are some of the IPython Magic commands – handy enhancements added on top of the standard Python syntax to solve various tasks specific to interactive computing. The first line in the snippet above configures the notebook session to visualize and store all the Matplotlib figures inside the notebook.

Exposing C++ array data in Python with the Pybind11 buffer protocol

Python works great together with native code. The success of the Python data science stack is largely attributed to the capability of building native extensions. It is no secret, however, that the out-of-the-box C-based facilities are rather cumbersome to use. Luckily, there are a bunch of alternatives out there. The one I liked the most is Pybind11, a fork of Boost.Python with focus on the modern C++ standards. It is distributed as a lightweight header-only library that can be bundled with your project.

Building OpenCV with Conda on Linux

When it comes to building and installing OpenCV with Python support on *nix platforms, the collection of tutorials by Adrian Rosebrock is the best. He provides detailed description of the required steps, as well as motivation for better development practices. In particular, Adrian creates a dedicated virtualenv environment, installs NumPy in it, and builds the whole thing having this environment activated. Such a solution worked pretty well for me, both on Linux and macOS.