Python Wrappers for NI Vision and IMAQdx
|Author:||Peter Johnson, FRC Team 294|
|Copyright:||Copyright © 2012 Peter Johnson|
NI Vision is a National Instruments image/vision processing library. It is available for multiple platforms including Windows and the CompactRIO. This project makes the functionality of NI Vision available in the Python programming language.
- Full wrapping of all structures, enumerations, and functions.
- All functions raise a nivision.ImaqError exception if an error occurs, rather than relying on nonzero or NULL return values.
- Output parameters are returned as a tuple (in parameter order). If the function additionally returns a value, this is returned as the first element of the tuple, followed by the output parameters.
- Count/size/length parameters that specify the length of an array are handled implicitly by using Python’s len() function. This includes return values (which are returned as a sized array).
- Additional Pythonic helper functions (see Documentation).
- Automatic memory management of Image and other data types (see Documentation).
Read a JPEG file and print its dimensions (width and height):
import nivision img = nivision.imaqCreateImage(nivision.IMAQ_IMAGE_RGB) nivision.Priv_ReadJPEGString(img, open('example.jpg', 'rb').read()) print(nivision.imaqGetImageSize(img))
An installed and licensed version of NI Vision. Only the 2011 version has been tested.
The wrappers currently require ctypes, so this library is currently not usable on platforms that don’t have ctypes. Unfortunately this includes RobotPy as of this writing, but this is being worked on.
The wrappers have only been tested on Python 3.x.
On the robot, this will be installed automatically as of RobotPy 2015.0.1. Otherwise, you can install it via pip:
pip install pynivision
Run install.bat or python setup.py install.
The “NI Vision for LabWindows/CVI Function Reference” help file (NIVisionCVI.chm) is currently the best “complete” reference. However, the Python wrappers deviate from the C library in several important ways (see Features).
A few helper functions are provided due to the use of in/out parameters.
- imaqGetCores(): Returns the number of cores available to NI Vision.
- imaqSetCores(customNumCores): Sets the number of cores available to NI Vision.
- imaqUseMaxAvailableCores(): Configures NI Vision to use the maximum number of available processor cores.
imaqDispose() is automatically called when the corresponding Python object is garbage collected for Image and all other structures/arrays returned by imaq functions. It is still ok to explicitly call imaqDispose() on these objects (but be careful, as the underlying object will be immediately freed even though the Python object is still accessible).
Most of the wrapper code is generated by the gen_wrap.py script. Hand-written Python code in ctypes_core_prefix.py and ctypes_core_suffix.py complements the automatically generated code. The gen_wrap.py script creates nivision/core.py which includes the contents of the prefix and suffix Python files.
The gen_wrap.py script reads an .ini file for Pythonic customizations to the wrappers such as automatic disposal and handling of sized values as arrays. The .ini file used is automatically determined by the nivision version in use (or can alternatively be specified on the command line).
As Priv_ReadJPEGString_C is not exported on current Windows distributions of nivissvc.dll, a custom implementation that uses GDI+ has been written in nivision/private.py and nivision/gdiplus.py.
Copyright © 2012 Peter Johnson.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.