New XiAPI Python binding

In the newest XiAPI SDK release, there is a Python library directly binding the XIMEA dlls without the need to use OpenCV.

Dear all,

previously, there was some effort to create a decent binding to the XiAPI. For example, the binding of the pupil-labs. However, this is barely maintained. The last reasonable update was roughly two years ago.

The de facto standard binding for Python is via OpenCV as recommended by XIMEA. However, native installations of pre-compiled OpenCV do lack the XIMEA support. To help you there, I provide a pre-compiled OpenCV 3.1 available for Python 3.5.x and Python 2.7.x. In the latest OpenCV version is also a decent amount of features included that can be altered directly via OpenCV, for example the auto-exposure/auto-gain feature (AEAG). I had sometimes problems with AEAG being enabled, so it is quite nice that one can disable this feature easily.  But still, some features are not covered by the OpenCV interface, despite the fact that it is quite easy to use:

import cv2

cam = cv2.VideoCapture(0)

while True:
    flag, im = cam.read()

    if flag:
        cv2.imshow('cam stream',im)
        k = cv2.waitKey(1)

    if k == ord('q'):
        break

cv2.destroyAllWindows()
cam.release()

Advantages of XiAPI binding

The latest camera I bought was the MQ013MG-ON with a PYTHON 1300 chip, that has a decent resolution of 1280×1024 (1/2″ sensor size) and a high speed (around 210 fps at full resolution). The most interesting part is the ROI cropping, leading to speeds up to 1000 (!) fps (as described here briefly), with roughly the same field of view  when using MQ003MG-CM. You have also the same resolution (VGA) and bit depth (8 or 10 bit). Even more interesting, this camera is reasonable cheaper than the MQ003MG with CMV 300 chip. However, the pixel size is quite different (4.6 µm PYTHON 1300 to 7.4 µm CMV 300), that may lead to problems in low light conditions etc.

But, you cannot crop the camera image to a random ROI using OpenCV. The direct C++ binding used in XIMEA’s own XiViewer is able to crop to a random ROI and gives us the reported speeds as described above. Here comes the new xiApiPython binding into play. Using the direct binding, one can access this ROI cropping functionality.

Disadvantages and example

The ease of OpenCV cannot be fulfilled by the xiApiPython library. Looking even at the example code, the environment is much more complicated than the straight forward OpenCV. However, it comes with some logic.

First, you create a camera handle. Second, you create an image handle. Then you grab a frame from your camera and store it in the image handle. The raw data can be readout from the image handle (string in Python 2.7 and bytes in Python 3.5) or directly as numpy array. Finally, you can just show the image using OpenCV.

Here’s some example code that does the same as above, but using only the xiApiPython binding to access the camera:

from ximea import xiapi
import cv2

# create handle and open camera
cam = xiapi.Camera()
cam.open_device()

# create image handle
img = xiapi.Image()

# start data acquisition
cam.start_acquisition()

while True:
    # get data and pass to img
    cam.get_image(img)

    data = img.get_image_data_numpy()

    cv2.imshow("cam stream",data)   
    k = cv2.waitKey(1)

    if k == ord('q'):
        break

# clear handle and close window
cam.stop_acquisition()
cam.close_device()
cv2.destroyAllWindows()