VPI - Vision Programming Interface

1.2 Release

Python API

VPI exposes a Python API that offers a simplified way to use the library, leading to faster development cycle. Resource management is done automatically under the hood, allowing the developer to focus on the definition of the image processing pipeline.

Requirements

  • Ubuntu 16.04 or 18.04
  • python 2.7 or 3.6
  • numpy >= 1.11.0
  • pillow >= 3.1.2

Install the Python 2.7 packages using:

apt-get install python python-pil python-numpy

If using Python 3.6, install the following packages:

apt-get install python3 python3-pil python3-numpy

Writing an Image Blurring Application

Copy the code below to a file named main.py. The comments explain how it works.

30 import vpi
31 
32 import numpy as np
33 from PIL import Image
34 from argparse import ArgumentParser
35 
36 # 2. Parse command line parameters
37 # -----------------------------------------------------------------------------
38 parser = ArgumentParser()
39 parser.add_argument('input',
40  help='Image to be used as input')
41 args = parser.parse_args();
42 
43 # 3. Load input and wrap it into a VPI image.
44 # -----------------------------------------------------------------------------
45 
46 # `Image.open` returns a Pillow image that is then interpreted as
47 # a numpy array. This array is finally wrapped in a VPI image suitable
48 # for use by VPI algorithms.
49 input = vpi.asimage(np.asarray(Image.open(args.input)))
50 
51 # 4. Convert it to grayscale and blur it with a 5x5 box filter
52 # with ZERO border condition.
53 # -----------------------------------------------------------------------------
54 
55 # Enabling the CUDA backend in a python context like done below makes
56 # VPI algorithms use CUDA for execution by default. This can be overriden
57 # if needed by specifying the parameter `backend=` when calling the algorithm.
58 with vpi.Backend.CUDA:
59  # `image.convert` will return a new VPI image with the desired format, in
60  # this case U8 (grayscale, 8-bit unsigned pixels).
61  # Algorithms returning a new VPI image allows for chaining operations like
62  # done below, as the result of the conversion is then filtered.
63  # The end result is finally stored in a new `output` VPI image.
64  output = input.convert(vpi.Format.U8) \
65  .box_filter(5, border=vpi.Border.ZERO)
66 
67 # 5. Save result to disk
68 # -----------------------------------------------------------------------------
69 
70 # The read-lock context enabled below makes sure all processing is finished and
71 # results are stored in `output`.
72 with output.rlock():
73  # `output.cpu()` returns a numpy array view (not a copy) of `output`
74  # contents, accessible by host (cpu).
75  # The numpy array is then converted into a Pillo image and saved
76  # to the disk
77  Image.fromarray(output.cpu()).save('tutorial_blurred_python.png')
78 
79 # vim: ts=8:sw=4:sts=4:et:ai

Testing the Application

With everything set in place, if you're using python 2.7, execute the script as follows:

python main.py <image file name>

substituting <image file name> by some image on disk.

To use python 3.6, substitute python with python3:

python3 main.py <image file name>