From 3c1ca240eb7d90e7c7e925c06712a59a4063cb7e Mon Sep 17 00:00:00 2001 From: John Sun Date: Fri, 4 Apr 2014 15:49:14 -0700 Subject: Rename directory to pymba instead of pyvimba. Fix test_systemfeatures.py --- README.md | 8 +- README.txt | 8 +- pymba/__init__.py | 4 + pymba/tests/__init__.py | 0 pymba/tests/opencv_example.py | 102 ++++++++++ pymba/tests/test_cameras.py | 70 +++++++ pymba/tests/test_installation.py | 8 + pymba/tests/test_interfaces.py | 29 +++ pymba/tests/test_systemfeature.py | 21 ++ pymba/vimba.py | 231 +++++++++++++++++++++ pymba/vimbacamera.py | 124 ++++++++++++ pymba/vimbadll.py | 387 ++++++++++++++++++++++++++++++++++++ pymba/vimbaexception.py | 63 ++++++ pymba/vimbafeature.py | 321 ++++++++++++++++++++++++++++++ pymba/vimbaframe.py | 89 +++++++++ pymba/vimbainterface.py | 44 ++++ pymba/vimbaobject.py | 213 ++++++++++++++++++++ pymba/vimbastructure.py | 99 +++++++++ pymba/vimbasystem.py | 19 ++ pyvimba/__init__.py | 4 - pyvimba/tests/__init__.py | 0 pyvimba/tests/opencv_example.py | 80 -------- pyvimba/tests/test_cameras.py | 70 ------- pyvimba/tests/test_installation.py | 8 - pyvimba/tests/test_interfaces.py | 29 --- pyvimba/tests/test_systemfeature.py | 16 -- pyvimba/vimba.py | 231 --------------------- pyvimba/vimbacamera.py | 124 ------------ pyvimba/vimbadll.py | 387 ------------------------------------ pyvimba/vimbaexception.py | 63 ------ pyvimba/vimbafeature.py | 321 ------------------------------ pyvimba/vimbaframe.py | 89 --------- pyvimba/vimbainterface.py | 44 ---- pyvimba/vimbaobject.py | 213 -------------------- pyvimba/vimbastructure.py | 99 --------- pyvimba/vimbasystem.py | 19 -- setup.py | 8 +- 37 files changed, 1836 insertions(+), 1809 deletions(-) create mode 100644 pymba/__init__.py create mode 100644 pymba/tests/__init__.py create mode 100644 pymba/tests/opencv_example.py create mode 100644 pymba/tests/test_cameras.py create mode 100644 pymba/tests/test_installation.py create mode 100644 pymba/tests/test_interfaces.py create mode 100644 pymba/tests/test_systemfeature.py create mode 100644 pymba/vimba.py create mode 100644 pymba/vimbacamera.py create mode 100644 pymba/vimbadll.py create mode 100644 pymba/vimbaexception.py create mode 100644 pymba/vimbafeature.py create mode 100644 pymba/vimbaframe.py create mode 100644 pymba/vimbainterface.py create mode 100644 pymba/vimbaobject.py create mode 100644 pymba/vimbastructure.py create mode 100644 pymba/vimbasystem.py delete mode 100644 pyvimba/__init__.py delete mode 100644 pyvimba/tests/__init__.py delete mode 100644 pyvimba/tests/opencv_example.py delete mode 100644 pyvimba/tests/test_cameras.py delete mode 100644 pyvimba/tests/test_installation.py delete mode 100644 pyvimba/tests/test_interfaces.py delete mode 100644 pyvimba/tests/test_systemfeature.py delete mode 100644 pyvimba/vimba.py delete mode 100644 pyvimba/vimbacamera.py delete mode 100644 pyvimba/vimbadll.py delete mode 100644 pyvimba/vimbaexception.py delete mode 100644 pyvimba/vimbafeature.py delete mode 100644 pyvimba/vimbaframe.py delete mode 100644 pyvimba/vimbainterface.py delete mode 100644 pyvimba/vimbaobject.py delete mode 100644 pyvimba/vimbastructure.py delete mode 100644 pyvimba/vimbasystem.py diff --git a/README.md b/README.md index 4fca83f..a6bef6d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# pyvimba +# pymba -pyvimba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API. It wraps the VimbaC.dll file included in the AVT Vimba installation to provide a simple Python interface for AVT cameras. It currently supports most of the functionality provided by VimbaC.dll. +pymba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API. It wraps the VimbaC.dll file included in the AVT Vimba installation to provide a simple Python interface for AVT cameras. It currently supports most of the functionality provided by VimbaC.dll. ## Installation @@ -8,13 +8,13 @@ Install the Vimba SDK from AVT to the default directory. Run the AVTDriverInstaller tool and install the AVT Vimba SDK drivers. -Install pyvimba. +Install pymba. ## Usage ### Testing installation -If Vimba and pyvimba are installed correctly, then the following code should give the installed Vimba version. No camera is needed. +If Vimba and pymba are installed correctly, then the following code should give the installed Vimba version. No camera is needed. from vimba import * diff --git a/README.txt b/README.txt index 49a0ed1..f7f9dda 100644 --- a/README.txt +++ b/README.txt @@ -1,8 +1,8 @@ ======= -pyvimba +pymba ======= -pyvimba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API. It wraps the VimbaC.dll file included in the AVT Vimba installation to provide a simple Python interface for AVT cameras. It currently supports most of the functionality provided by VimbaC.dll. +pymba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API. It wraps the VimbaC.dll file included in the AVT Vimba installation to provide a simple Python interface for AVT cameras. It currently supports most of the functionality provided by VimbaC.dll. Installation ============ @@ -11,7 +11,7 @@ Installation * Run the AVTDriverInstaller tool and install the AVT Vimba SDK drivers. -* Install pyvimba. +* Install pymba. Usage ===== @@ -19,7 +19,7 @@ Usage Testing installation -------------------- -If Vimba and pyvimba are installed correctly, then the following code should give the installed Vimba version. No camera is needed. +If Vimba SDK and pymba are installed correctly, then the following code should give the installed Vimba version. No camera is needed. from vimba import * diff --git a/pymba/__init__.py b/pymba/__init__.py new file mode 100644 index 0000000..96a931e --- /dev/null +++ b/pymba/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +from vimba import Vimba +from vimbaexception import VimbaException + diff --git a/pymba/tests/__init__.py b/pymba/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pymba/tests/opencv_example.py b/pymba/tests/opencv_example.py new file mode 100644 index 0000000..ddd0c8d --- /dev/null +++ b/pymba/tests/opencv_example.py @@ -0,0 +1,102 @@ +from pymba import * +import numpy as np +import cv2 +import time + +#very crude example, assumes your camera is PixelMode = BAYERRG8 + +# start Vimba +vimba = Vimba() +vimba.startup() + +# get system object +system = vimba.getSystem() + +# list available cameras (after enabling discovery for GigE cameras) +if system.GeVTLIsPresent: + print("GeVTLIsPresent") + system.runFeatureCommand("GeVDiscoveryAllOnce") + time.sleep(0.2) +else: + print ("No GeVTL") +cameraIds = vimba.getCameraIds() +for cameraId in cameraIds: + print 'Camera ID:', cameraId + +# get and open a camera +camera0 = vimba.getCamera(cameraIds[0]) +camera0.openCamera() + +# list camera features +cameraFeatureNames = camera0.getFeatureNames() +for name in cameraFeatureNames: + try: + print 'Camera feature:%s=%s' % (name, camera0.__getattr__(name)) + pass + except VimbaException: + print "%s Not yet implemented" % name + pass + +# read info of a camera feature +#featureInfo = camera0.getFeatureInfo('AcquisitionMode') +#for field in featInfo.getFieldNames(): +# print field, '--', getattr(featInfo, field) + +# get the value of a feature +print "AcquisitionMode is" +print camera0.AcquisitionMode +print camera0.ExposureMode + +# set the value of a feature +print "Setting acquisition mode" + +#camera0.AcquisitionMode = 'Continuous' +camera0.__setattr__("AcquisitionModeCCC", 'SingleFrame') +try: + camera0.AcquisitionModeJJJ = 'JJJ' + print "Got in here" +except Exception: + print "Failed to set JJJ" +print camera0.AcquisitionMode + +# create new frames for the camera + +frame0 = camera0.getFrame() # creates a frame +frame1 = camera0.getFrame() # creates a second frame + +# announce frame +frame0.announceFrame() + +# capture a camera image +count = 0 +while count < 10: + camera0.startCapture() + camera0.runFeatureCommand('AcquisitionStart') + + frame0.queueFrameCapture() + frame0.waitFrameCapture() + + camera0.runFeatureCommand('AcquisitionStop') + + # get image data... + imgData = frame0.getBufferByteData() + + moreUsefulImgData = np.ndarray(buffer = frame0.getBufferByteData(), + dtype = np.uint8, + shape = (frame0.height, + frame0.width, + 1)) + rgb = cv2.cvtColor(moreUsefulImgData, cv2.COLOR_BAYER_RG2RGB) + cv2.imwrite('foo{}.png'.format(count), rgb) + print "image {} saved".format(count) + count += 1 + camera0.endCapture() +# clean up after capture +camera0.revokeAllFrames() + +# close camera +camera0.closeCamera() + +# shutdown Vimba +vimba.shutdown() + diff --git a/pymba/tests/test_cameras.py b/pymba/tests/test_cameras.py new file mode 100644 index 0000000..527109f --- /dev/null +++ b/pymba/tests/test_cameras.py @@ -0,0 +1,70 @@ +#!/usr/bin/python + +from pymba import * +import time + +def test_cameras(): + # start Vimba + vimba = Vimba() + vimba.startup() + + # get system object + system = vimba.getSystem() + + # list available cameras (after enabling discovery for GigE cameras) + if system.GeVTLIsPresent: + system.runFeatureCommand("GeVDiscoveryAllOnce") + time.sleep(0.2) + cameraIds = vimba.getCameraIds() + for cameraId in cameraIds: + print 'Camera ID:', cameraId + + # get and open a camera + camera0 = vimba.getCamera(cameraIds[0]) + camera0.openCamera() + + # list camera features + cameraFeatureNames = camera0.getFeatureNames() + for name in cameraFeatureNames: + print 'Camera feature:', name + + # get the value of a feature + print camera0.AcquisitionMode + + # set the value of a feature + camera0.AcquisitionMode = 'SingleFrame' + + # create new frames for the camera + frame0 = camera0.getFrame() # creates a frame + frame1 = camera0.getFrame() # creates a second frame + + # announce frame + frame0.announceFrame() + + # capture a camera image + camera0.startCapture() + frame0.queueFrameCapture() + camera0.runFeatureCommand('AcquisitionStart') + camera0.runFeatureCommand('AcquisitionStop') + frame0.waitFrameCapture() + + # get image data... + imgData = frame0.getBufferByteData() + + # ...or use NumPy for fast image display (for use with OpenCV, etc) + import numpy as np + moreUsefulImgData = np.ndarray(buffer = frame0.getBufferByteData(), + dtype = np.uint8, + shape = (frame0.height, + frame0.width, + 1)) + + # clean up after capture + camera0.endCapture() + camera0.revokeAllFrames() + + # close camera + camera0.closeCamera() + + # shutdown Vimba + vimba.shutdown() diff --git a/pymba/tests/test_installation.py b/pymba/tests/test_installation.py new file mode 100644 index 0000000..782bc56 --- /dev/null +++ b/pymba/tests/test_installation.py @@ -0,0 +1,8 @@ +#!/usr/bin/python + +from pymba import Vimba + +def test_installation(): + vimba = Vimba() + version = vimba.getVersion() + assert version == '1.2.0' diff --git a/pymba/tests/test_interfaces.py b/pymba/tests/test_interfaces.py new file mode 100644 index 0000000..3294715 --- /dev/null +++ b/pymba/tests/test_interfaces.py @@ -0,0 +1,29 @@ +#!/usr/bin/python + +from pymba import * +import time + +def test_interfaces(): + # start Vimba + vimba = Vimba() + vimba.startup() + + # get list of available interfaces + interfaceIds = vimba.getInterfaceIds() + for interfaceId in interfaceIds: + print 'Interface ID:', interfaceId + + # get interface object and open it + interface0 = vimba.getInterface(interfaceIds[0]) + interface0.openInterface() + + # list interface features + interfaceFeatureNames = interface0.getFeatureNames() + for name in interfaceFeatureNames: + print 'Interface feature:', name + + # close interface + interface0.closeInterface() + + # shutdown Vimba + vimba.shutdown() diff --git a/pymba/tests/test_systemfeature.py b/pymba/tests/test_systemfeature.py new file mode 100644 index 0000000..0f926d4 --- /dev/null +++ b/pymba/tests/test_systemfeature.py @@ -0,0 +1,21 @@ +#!/usr/bin/python + +from pymba import * + +def test_systemfeature(): + # get system object + vimba = Vimba() + vimba.startup() + + system = vimba.getSystem() + + # list system features + for featureName in system.getFeatureNames(): + print 'System feature:', featureName + fInfo = system.getFeatureInfo(featureName) + for field in fInfo.getFieldNames(): + print "\t", featureName, ":", field, getattr(fInfo, field) + + # shutdown Vimba + vimba.shutdown() + diff --git a/pymba/vimba.py b/pymba/vimba.py new file mode 100644 index 0000000..0add297 --- /dev/null +++ b/pymba/vimba.py @@ -0,0 +1,231 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbadll import VimbaDLL +from vimbaexception import VimbaException +from vimbasystem import VimbaSystem +from vimbacamera import VimbaCamera +from vimbainterface import VimbaInterface +from ctypes import * + +class Vimba(object): + """ + An Allied Vision Technology Vimba API. + This API provides access to AVT cameras. + """ + + # todo - assign camera info and feature info as own object proeprties + + def __init__(self): + + # create own system singleton object + self._system = VimbaSystem() + + # lists of VimbaCameraInfo and VimbaInterfaceInfo objects + # can't be called before startup() so populate later + self._cameraInfos = None + self._interfaceInfos = None + + # dict of {camera ID : VimbaCamera object} as we don't want to forget them + self._cameras = {} + + # dict of {interface ID : VimbaInterface object} as we don't want to forget them + self._interfaces = {} + + def _getInterfaceInfos(self): + """ + Gets interface info of all available interfaces. + + :returns: list -- interface info for available interfaces. + """ + if self._interfaceInfos is None: + # args + dummyInterfaceInfo = structs.VimbaInterfaceInfo() + numFound = c_uint32(-1) + + # call once just to get the number of interfaces + # Vimba DLL will return an error code + errorCode = VimbaDLL.interfacesList(byref(dummyInterfaceInfo), + 0, + byref(numFound), + sizeof(dummyInterfaceInfo)) + if errorCode != 0: + print errorCode + raise VimbaException(errorCode) + + numInterfaces = numFound.value + + # args + interfaceInfoArray = (structs.VimbaInterfaceInfo * numInterfaces)() + + # call again to get the features + # Vimba DLL will return an error code + errorCode = VimbaDLL.interfacesList(interfaceInfoArray, + numInterfaces, + byref(numFound), + sizeof(dummyInterfaceInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + self._interfaceInfos = list(interfaceInfo for interfaceInfo in interfaceInfoArray) + return self._interfaceInfos + + def _getCameraInfos(self): + """ + Gets camera info of all attached cameras. + + :returns: list -- camera info for available cameras. + """ + if self._cameraInfos is None: + # args + dummyCameraInfo = structs.VimbaCameraInfo() + numFound = c_uint32(-1) + + # call once just to get the number of cameras + # Vimba DLL will return an error code + errorCode = VimbaDLL.camerasList(byref(dummyCameraInfo), + 0, + byref(numFound), + sizeof(dummyCameraInfo)) + if errorCode != 0: + print errorCode + raise VimbaException(errorCode) + + numCameras = numFound.value + + # args + cameraInfoArray = (structs.VimbaCameraInfo * numCameras)() + + # call again to get the features + # Vimba DLL will return an error code + errorCode = VimbaDLL.camerasList(cameraInfoArray, + numCameras, + byref(numFound), + sizeof(dummyCameraInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + self._cameraInfos = list(camInfo for camInfo in cameraInfoArray) + return self._cameraInfos + + def getSystem(self): + """ + Gets system singleton object. + + :returns: VimbaSystem object -- the system singleton object. + """ + return self._system + + def getInterfaceIds(self): + """ + Gets IDs of all available interfaces. + + :returns: list -- interface IDs for available interfaces. + """ + return list(interfaceInfo.interfaceIdString for interfaceInfo in self._getInterfaceInfos()) + + def getCameraIds(self): + """ + Gets IDs of all available cameras. + + :returns: list -- camera IDs for available cameras. + """ + return list(camInfo.cameraIdString for camInfo in self._getCameraInfos()) + + def getInterfaceInfo(self, interfaceId): + """ + Gets interface info object of specified interface. + + :param interfaceId: the ID of the interface object to get. + + :returns: VimbaInterfaceInfo object -- the interface info object specified. + """ + # don't do this live as we already have this info + # return info object if it exists + for interfaceInfo in self._getInterfaceInfos(): + if interfaceInfo.interfaceIdString == interfaceId: + return interfaceInfo + # otherwise raise error + raise VimbaException(-54) + + def getCameraInfo(self, cameraId): + """ + Gets camera info object of specified camera. + + :param cameraId: the ID of the camera object to get. + + :returns: VimbaCameraInfo object -- the camera info object specified. + """ + # don't do this live as we already have this info + # return info object if it exists + for camInfo in self._getCameraInfos(): + if camInfo.cameraIdString == cameraId: + return camInfo + # otherwise raise error + raise VimbaException(-50) + + def getInterface(self, interfaceId): + """ + Gets interface object based on interface ID string. Will not recreate + interface object if it already exists. + + :param interfaceId: the ID of the interface. + + :returns: VimbaInterface object -- the interface object specified. + """ + # check ID is valid + if interfaceId in self.getInterfaceIds(): + # create it if it doesn't exist + if interfaceId not in self._interfaces: + self._interfaces[interfaceId] = VimbaInterface(interfaceId) + return self._interfaces[interfaceId] + raise VimbaException(-54) + + def getCamera(self, cameraId): + """ + Gets camera object based on camera ID string. Will not recreate + camera object if it already exists. + + :param cameraId: the ID of the camera. + + :returns: VimbaCamera object -- the camera object specified. + """ + # check ID is valid + if cameraId in self.getCameraIds(): + # create it if it doesn't exist + if cameraId not in self._cameras: + self._cameras[cameraId] = VimbaCamera(cameraId) + return self._cameras[cameraId] + raise VimbaException(-50) + + def getVersion(self): + """ + Retrieve the version number of VimbaC. + + :returns: string - Vimba API version info. + """ + # args + versionInfo = structs.VimbaVersion() + + # Vimba DLL will return an error code + errorCode = VimbaDLL.versionQuery(versionInfo, + sizeof(versionInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + + versionStr = '.'.join([str(versionInfo.major), + str(versionInfo.minor), + str(versionInfo.patch)]) + return versionStr + + def startup(self): + """ + Initialize the VimbaC API. + """ + # Vimba DLL will return an error code + errorCode = VimbaDLL.startup() + if errorCode != 0: + raise VimbaException(errorCode) + + def shutdown(self): + """ + Perform a shutdown on the API. + """ + VimbaDLL.shutdown() diff --git a/pymba/vimbacamera.py b/pymba/vimbacamera.py new file mode 100644 index 0000000..747eb4c --- /dev/null +++ b/pymba/vimbacamera.py @@ -0,0 +1,124 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbaobject import VimbaObject +from vimbaexception import VimbaException +from vimbaframe import VimbaFrame +from vimbadll import VimbaDLL +from ctypes import * + +# camera features are automatically readable as object attributes. +class VimbaCamera(VimbaObject): + """ + A Vimba camera object. This class provides the minimal access + to Vimba functions required to control the camera. + """ + + @property + def cameraIdString(self): + return self._cameraIdString + + # own handle is inherited as self._handle + def __init__(self, cameraIdString): + + # call super constructor + super(VimbaCamera, self).__init__() + + # set ID + self._cameraIdString = cameraIdString + + # set own info + self._info = self._getInfo() + + def getInfo(self): + """ + Get info of the camera. Does not require + the camera to be opened. + + :returns: VimbaCameraInfo object -- camera information. + """ + return self._info + + def _getInfo(self): + """ + Get info of the camera. Does not require + the camera to be opened. + + :returns: VimbaCameraInfo object -- camera information. + """ + # args for Vimba call + cameraInfo = structs.VimbaCameraInfo() + + # Vimba DLL will return an error code + errorCode = VimbaDLL.cameraInfoQuery(self._cameraIdString, + byref(cameraInfo), + sizeof(cameraInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + + return cameraInfo + + def openCamera(self): + """ + Open the camera. + """ + # args for Vimba call + cameraAccessMode = 1 # full access (see VmbAccessModeType) + + errorCode = VimbaDLL.cameraOpen(self._cameraIdString, + cameraAccessMode, + byref(self._handle)) + if errorCode != 0: + raise VimbaException(errorCode) + + def closeCamera(self): + """ + Close the camera. + """ + errorCode = VimbaDLL.cameraClose(self._handle) + if errorCode != 0: + raise VimbaException(errorCode) + + def revokeAllFrames(self): + """ + Revoke all frames assigned to the camera. + """ + errorCode = VimbaDLL.frameRevokeAll(self._handle) + if errorCode != 0: + raise VimbaException(errorCode) + + def startCapture(self): + """ + Prepare the API for incoming frames. + """ + errorCode = VimbaDLL.captureStart(self._handle) + if errorCode != 0: + raise VimbaException(errorCode) + + def endCapture(self): + """ + Stop the API from being able to receive frames. + """ + errorCode = VimbaDLL.captureEnd(self._handle) + if errorCode != 0: + raise VimbaException(errorCode) + + def flushCaptureQueue(self): + """ + Flush the capture queue. + """ + errorCode = VimbaDLL.captureQueueFlush(self._handle) + if errorCode != 0: + raise VimbaException(errorCode) + + # method for easy frame creation + def getFrame(self): + """ + Creates and returns a new frame object. Multiple frames + per camera can therefore be returned. + + :returns: VimbaFrame object -- the new frame. + """ + return VimbaFrame(self) + + + diff --git a/pymba/vimbadll.py b/pymba/vimbadll.py new file mode 100644 index 0000000..f21f7f6 --- /dev/null +++ b/pymba/vimbadll.py @@ -0,0 +1,387 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbaexception import VimbaException +from sys import platform +import os +from ctypes import * + +if platform == "win32": + from ctypes.util import find_msvcrt + _cruntime = cdll.LoadLibrary(find_msvcrt()) + vimbaC_path = r'C:\Program Files\Allied Vision Technologies\AVTVimba_1.2\VimbaC\Bin\Win32\VimbaC.dll' + dll_loader = windll +else: + _cruntime = CDLL("libc.so.6") + dll_loader = cdll + assert os.environ.get("GENICAM_GENTL64_PATH"), "you need your GENICAM_GENTL64_PATH environment set. Make sure you have Vimba installed, and you have loaded the /etc/profile.d/ scripts" + vimba_dir = "/".join(os.environ.get("GENICAM_GENTL64_PATH").split("/")[1:-3]) + vimbaC_path = "/" + vimba_dir + "/VimbaC/DynamicLib/x86_64bit/libVimbaC.so" + +with open(vimbaC_path) as thefile: + pass #NJO i think this is kind of like an os.exists ? + + +class VimbaDLL(object): + """ + ctypes directives to make the wrapper class work cleanly, + talks to VimbaC.dll + """ + # a full list of Vimba API methods + # (only double dashed methods have been implemented so far) + # + # -- VmbVersionQuery() + # + # -- VmbStartup() + # -- VmbShutdown() + # + # -- VmbCamerasList() + # -- VmbCameraInfoQuery() + # -- VmbCameraOpen() + # -- VmbCameraClose() + # + # -- VmbFeaturesList() + # -- VmbFeatureInfoQuery() + # VmbFeatureListAffected() + # VmbFeatureListSelected() + # VmbFeatureAccessQuery() + # + # -- VmbFeatureIntGet() + # -- VmbFeatureIntSet() + # -- VmbFeatureIntRangeQuery() + # VmbFeatureIntIncrementQuery() + # + # -- VmbFeatureFloatGet() + # -- VmbFeatureFloatSet() + # -- VmbFeatureFloatRangeQuery() + # + # -- VmbFeatureEnumGet() + # -- VmbFeatureEnumSet() + # VmbFeatureEnumRangeQuery() + # VmbFeatureEnumIsAvailable() + # VmbFeatureEnumAsInt() + # VmbFeatureEnumAsString() + # VmbFeatureEnumEntryGet() + # + # -- VmbFeatureStringGet() + # -- VmbFeatureStringSet() + # VmbFeatureStringMaxlengthQuery() + # + # -- VmbFeatureBoolGet() + # -- VmbFeatureBoolSet() + # + # -- VmbFeatureCommandRun() + # VmbFeatureCommandIsDone() + # + # VmbFeatureRawGet() + # VmbFeatureRawSet() + # VmbFeatureRawLengthQuery() + # + # VmbFeatureInvalidationRegister() + # VmbFeatureInvalidationUnregister() + # + # -- VmbFrameAnnounce() + # -- VmbFrameRevoke() + # -- VmbFrameRevokeAll() + # -- VmbCaptureStart() + # -- VmbCaptureEnd() + # -- VmbCaptureFrameQueue() + # -- VmbCaptureFrameWait() + # -- VmbCaptureQueueFlush() + # + # -- VmbInterfacesList() + # -- VmbInterfaceOpen() + # -- VmbInterfaceClose() + # + # VmbAncillaryDataOpen() + # VmbAncillaryDataClose() + # + # VmbMemoryRead() + # VmbMemoryWrite() + # -- VmbRegistersRead() + # -- VmbRegistersWrite() + + # Vimba C API DLL + + _vimbaDLL = dll_loader.LoadLibrary(vimbaC_path) + + # version query + versionQuery = _vimbaDLL.VmbVersionQuery + versionQuery.restype = c_int32 # returned error code + versionQuery.argtypes = (POINTER(structs.VimbaVersion), # pointer to version structure + c_uint32) # version structure size + + # startup + startup = _vimbaDLL.VmbStartup + startup.restype = c_int32 # returned error code + + # shutdown + shutdown = _vimbaDLL.VmbShutdown + + # list cameras + camerasList = _vimbaDLL.VmbCamerasList + camerasList.restype = c_int32 # returned error code + camerasList.argtypes = (POINTER(structs.VimbaCameraInfo), # pointer to camera info structure + c_uint32, # length of list + POINTER(c_uint32), # pointer to number of cameras + c_uint32) # camera info structure size + + # camera info query + cameraInfoQuery = _vimbaDLL.VmbCameraInfoQuery + cameraInfoQuery.restype = c_int32 + cameraInfoQuery.argtypes = (c_char_p, # camera unique id + POINTER(structs.VimbaCameraInfo), # pointer to camera info structure + c_uint32) # size of structure + + # camera open + cameraOpen = _vimbaDLL.VmbCameraOpen + cameraOpen.restype = c_int32 # returned error code + cameraOpen.argtypes = (c_char_p, # camera unique id + c_uint32, # access mode + c_void_p) # camera handle, pointer to a pointer + + # camera close + cameraClose = _vimbaDLL.VmbCameraClose + cameraClose.restype = c_int32 # returned error code + cameraClose.argtypes = (c_void_p,) # camera handle + + # list features + featuresList = _vimbaDLL.VmbFeaturesList + featuresList.restype = c_int32 + featuresList.argtypes = (c_void_p, # handle, in this case camera handle + POINTER(structs.VimbaFeatureInfo), # pointer to feature info structure + c_uint32, # list length + POINTER(c_uint32), # pointer to num features found + c_uint32) # feature info size + + # feature info query + featureInfoQuery = _vimbaDLL.VmbFeatureInfoQuery + featureInfoQuery.restype = c_int32 + featureInfoQuery.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of feature + POINTER(structs.VimbaFeatureInfo), # pointer to feature info structure + c_uint32) # size of structure + + # get the int value of a feature + featureIntGet = _vimbaDLL.VmbFeatureIntGet + featureIntGet.restype = c_int32 + featureIntGet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + POINTER(c_int64)) # value to get + + # set the int value of a feature + featureIntSet = _vimbaDLL.VmbFeatureIntSet + featureIntSet.restype = c_int32 + featureIntSet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + c_int64) # value to set # get the value of an integer feature + + # query the range of values of the feature + featureIntRangeQuery = _vimbaDLL.VmbFeatureIntRangeQuery + featureIntRangeQuery.restype = c_int32 + featureIntRangeQuery.argtypes = (c_void_p, # handle + c_char_p, # name of the feature + POINTER(c_int64), # min range + POINTER(c_int64)) # max range + + # get the float value of a feature + featureFloatGet = _vimbaDLL.VmbFeatureFloatGet + featureFloatGet.restype = c_int32 + featureFloatGet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + POINTER(c_double)) # value to get + + # set the float value of a feature + featureFloatSet = _vimbaDLL.VmbFeatureFloatSet + featureFloatSet.restype = c_int32 + featureFloatSet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + c_double) # value to set + + # query the range of values of the feature + featureFloatRangeQuery = _vimbaDLL.VmbFeatureFloatRangeQuery + featureFloatRangeQuery.restype = c_int32 + featureFloatRangeQuery.argtypes = (c_void_p, # handle + c_char_p, # name of the feature + POINTER(c_double), # min range + POINTER(c_double)) # max range + + # get the enum value of a feature + featureEnumGet = _vimbaDLL.VmbFeatureEnumGet + featureEnumGet.restype = c_int32 + featureEnumGet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + POINTER(c_char_p)) # value to get + + # set the enum value of a feature + featureEnumSet = _vimbaDLL.VmbFeatureEnumSet + featureEnumSet.restype = c_int32 + featureEnumSet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + c_char_p) # value to set + + # get the string value of a feature + featureStringGet = _vimbaDLL.VmbFeatureStringGet + featureStringGet.restype = c_int32 + featureStringGet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + c_char_p, # string buffer to fill + c_uint32, # size of the input buffer + POINTER(c_uint32)) # string buffer to fill + + # set the string value of a feature + featureStringSet = _vimbaDLL.VmbFeatureStringSet + featureStringSet.restype = c_int32 + featureStringSet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + c_char_p) # value to set + + # get the boolean value of a feature + featureBoolGet = _vimbaDLL.VmbFeatureBoolGet + featureBoolGet.restype = c_int32 + featureBoolGet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + POINTER(c_bool)) # value to get + + # set the boolean value of a feature + featureBoolSet = _vimbaDLL.VmbFeatureBoolSet + featureBoolSet.restype = c_int32 + featureBoolSet.argtypes = (c_void_p, # handle, in this case camera handle + c_char_p, # name of the feature + c_bool) # value to set + + # run a feature command + featureCommandRun = _vimbaDLL.VmbFeatureCommandRun + featureCommandRun.restype = c_int32 + featureCommandRun.argtypes = (c_void_p, # handle for a module that exposes features + c_char_p) # name of the command feature + + # announce frames to the API that may be queued for frame capturing later + frameAnnounce = _vimbaDLL.VmbFrameAnnounce + frameAnnounce.restype = c_int32 + frameAnnounce.argtypes = (c_void_p, # camera handle + POINTER(structs.VimbaFrame), # pointer to frame + c_uint32) # size of frame + + # revoke a frame from the API + frameRevoke = _vimbaDLL.VmbFrameRevoke + frameRevoke.restype = c_int32 + frameRevoke.argtypes = (c_void_p, # camera handle + POINTER(structs.VimbaFrame)) # pointer to frame + + # revoke all frames assigned to a certain camera + frameRevokeAll = _vimbaDLL.VmbFrameRevokeAll + frameRevokeAll.restype = c_int32 + frameRevokeAll.argtypes = (c_void_p,) # camera handle + + # prepare the API for incoming frames + captureStart = _vimbaDLL.VmbCaptureStart + captureStart.restype = c_int32 + captureStart.argtypes = (c_void_p,) # camera handle + + # stop the API from being able to receive frames + captureEnd = _vimbaDLL.VmbCaptureEnd + captureEnd.restype = c_int32 + captureEnd.argtypes = (c_void_p,) # camera handle + + # queue frames that may be filled during frame capturing + captureFrameQueue = _vimbaDLL.VmbCaptureFrameQueue + captureFrameQueue.restype = c_int32 + captureFrameQueue.argtypes = (c_void_p, + POINTER(structs.VimbaFrame), + c_void_p) # callback + + # wait for a queued frame to be filled (or dequeued) + captureFrameWait = _vimbaDLL.VmbCaptureFrameWait + captureFrameWait.restype = c_int32 + captureFrameWait.argtypes = (c_void_p, # camera handle + POINTER(structs.VimbaFrame), + c_uint32) # timeout + + # flush the capture queue + captureQueueFlush = _vimbaDLL.VmbCaptureQueueFlush + captureQueueFlush.restype = c_int32 + captureQueueFlush.argtypes = (c_void_p,) # camera handle + + # list interfaces + interfacesList = _vimbaDLL.VmbInterfacesList + interfacesList.restype = c_int32 + interfacesList.argtypes = (POINTER(structs.VimbaInterfaceInfo), # pointer to interface info structure + c_uint32, # length of list + POINTER(c_uint32), # pointer to number of interfaces + c_uint32) + + # open interface + interfaceOpen = _vimbaDLL.VmbInterfaceOpen + interfaceOpen.restype = c_int32 + interfaceOpen.argtypes = (c_char_p, # unique id + c_void_p) # handle + + # close interface + interfaceClose = _vimbaDLL.VmbInterfaceClose + interfaceClose.restype = c_int32 + interfaceClose.argtypes = (c_void_p,) # handle + + # read from register + registersRead = _vimbaDLL.VmbRegistersRead + registersRead.restype = c_int32 + registersRead.argtypes = (c_void_p, # handle + c_uint32, # read count + POINTER(c_uint64), # pointer to address array + POINTER(c_uint64), # pointer to data array + POINTER(c_uint32)) # pointer to num complete reads + + # write to register + registersWrite = _vimbaDLL.VmbRegistersWrite + registersWrite.restype = c_int32 + registersWrite.argtypes = (c_void_p, # handle + c_uint32, # write count + POINTER(c_uint64), # pointer to address array + POINTER(c_uint64), # pointer to data array + POINTER(c_uint32)) # pointer to num complete write + + +class VimbaC_MemoryBlock(object): + """ + Just a memory block object for dealing + neatly with C memory allocations. + """ + + # C runtime DLL + _crtDLL = _cruntime + + @property + def block(self): + return self._block + + def __init__(self, blockSize): + + # assign memory block + malloc = self._crtDLL.malloc + malloc.argtypes = (c_size_t,) + malloc.restype = c_void_p + self._block = malloc(blockSize) # todo check for NULL on failure + + # this seems to be None if too much memory is requested + if self._block is None: + raise VimbaException(-51) + + def __del__(self): + + # free memory block + free = self._crtDLL.free + free.argtypes = (c_void_p,) + free.restype = None + free(self._block) + + + + + + + + + + + + + diff --git a/pymba/vimbaexception.py b/pymba/vimbaexception.py new file mode 100644 index 0000000..034f194 --- /dev/null +++ b/pymba/vimbaexception.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +import exceptions + +class VimbaException(Exception): + """ + An exception for the AVT Vimba API. It contains a message + property which is a string indicating what went wrong. + + :param errorCode: Error code to be used to look up error message. + """ + + @property + def message(self): + return self._errorCodes[self.errorCode] + + @property + def errorCode(self): + return self._errorCode + + _errorCodes = { # Vimba C API specific errors + 0: 'No error.', + -1: 'Unexpected fault in VimbaC or driver.', + -2: 'VmbStartup() was not called before the current command.', + -3: 'The designated instance (camera, feature etc.) cannot be found.', + -4: 'The given handle is not valid, ensure device open.', + -5: 'Device was not opened for usage.', + -6: 'Operation is invalid with the current access mode.', + -7: 'One of the parameters was invalid (usually an illegal pointer).', + -8: 'The given struct size is not valid for this version of the API.', + -9: 'More data was returned in a string/list than space was provided.', + -10: 'The feature type for this access function was wrong.', + -11: 'The value was not valid; either out of bounds or not an increment of the minimum.', + -12: 'Timeout during wait.', + -13: 'Other error.', + -14: 'Resources not available (e.g. memory).', + -15: 'Call is invalid in the current context (e.g. callback).', + -16: 'No transport layers were found.', + -17: 'API feature is not implemented.', + -18: 'API feature is not supported.', + -19: 'A multiple registers read or write was partially completed.', + + # Custom errors + -50: 'Could not find the specified camera.', + -51: 'Not enough memory to assign frame buffer.', + -52: 'Invalid input.', + -53: 'Could not find the specified feature.', + -54: 'Could not find the specified interface.', + + # Miscellaneous errors + -1000: 'Oops, unknown internal error code!', + -1001: 'Oops, this VimbaFeature function is not yet implemented in pymba!'} + + def __init__(self, errorCode): + # if error code does not match expected codes then assign invalid code + if errorCode in self._errorCodes: + self._errorCode = errorCode + else: + self._errorCode = -1000 + + + + + diff --git a/pymba/vimbafeature.py b/pymba/vimbafeature.py new file mode 100644 index 0000000..c1d7e60 --- /dev/null +++ b/pymba/vimbafeature.py @@ -0,0 +1,321 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbaexception import VimbaException +from vimbadll import VimbaDLL +from ctypes import * + +# class may extend a generic Vimba entity class one day... +class VimbaFeature(object): + """ + A feature of a Vimba object. + """ + + @property + def name(self): + return self._name + + @property + def handle(self): + return self._handle + + # lookup relevant function for feature type and pass to that function + @property + def value(self): + return self._getSetTypeFuncs[self._info.featureDataType][0]() + @value.setter + def value(self, val): + self._getSetTypeFuncs[self._info.featureDataType][1](val) + + @property + def range(self): + return self._rangeQueryTypeFuncs[self._info.featureDataType]() + + def __init__(self, name, handle): + + # set name and handle + self._name = name + self._handle = handle + + # set own info + self._info = self._getInfo() + + # type functions dict for looking up correct get/set function to use + self._getSetTypeFuncs = {0 : (self._notYetImplemented, self._notYetImplemented), # todo + 1 : (self._getIntFeature, self._setIntFeature), + 2 : (self._getFloatFeature, self._setFloatFeature), + 3 : (self._getEnumFeature, self._setEnumFeature), + 4 : (self._getStringFeature, self._setStringFeature), + 5 : (self._getBoolFeature, self._setBoolFeature), + 6 : (self._notYetImplemented, self._notYetImplemented), # todo + 7 : (self._notYetImplemented, self._notYetImplemented), # todo + 8 : (self._notYetImplemented, self._notYetImplemented)} # todo + + # type functions dict for looking up correct range function to use + self._rangeQueryTypeFuncs = {0 : self._unknownRange, + 1 : self._rangeQueryIntFeature, + 2 : self._rangeQueryFloatFeature, + 3 : self._unknownRange, + 4 : self._unknownRange, + 5 : self._unknownRange, + 6 : self._unknownRange, + 7 : self._unknownRange, + 8 : self._unknownRange} + + def getInfo(self): + """ + Get info of the feature. + + :returns: VimbaFeatureInfo object -- feature information.. + """ + return self._info + + def _getInfo(self): + """ + Get info of the feature. + + :returns: VimbaFeatureInfo object -- feature information.. + """ + # args for Vimba call + featureInfo = structs.VimbaFeatureInfo() + + # Vimba DLL will return an error code + errorCode = VimbaDLL.featureInfoQuery(self._handle, + self._name, + byref(featureInfo), + sizeof(featureInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + + return featureInfo + + def _notYetImplemented(self, val = None): + """ + Raises exception if feature value type is not yet defined. + """ + raise VimbaException(-1001) + + def _getIntFeature(self): + """ + Get the value of an integer feature. + + :returns: int -- value of the specified feature. + """ + + # create args + valueToGet = c_int64() + + errorCode = VimbaDLL.featureIntGet(self._handle, + self._name, + byref(valueToGet)) + if errorCode != 0: + raise VimbaException(errorCode) + + return valueToGet.value + + def _setIntFeature(self, valueToSet): + """ + Set the value of an integer feature. + + :param valueToSet: the int value to set for the feature. + """ + + errorCode = VimbaDLL.featureIntSet(self._handle, + self._name, + valueToSet) + if errorCode != 0: + raise VimbaException(errorCode) + + def _getFloatFeature(self): + """ + Get the value of a float feature. + + :returns: float -- value of the specified feature. + """ + + # create args + valueToGet = c_double() + + errorCode = VimbaDLL.featureFloatGet(self._handle, + self._name, + byref(valueToGet)) + if errorCode != 0: + raise VimbaException(errorCode) + + return valueToGet.value + + def _setFloatFeature(self, valueToSet): + """ + Set the value of a float feature. + + :param valueToSet: the float value to set for the feature. + """ + + errorCode = VimbaDLL.featureFloatSet(self._handle, + self._name, + valueToSet) + if errorCode != 0: + raise VimbaException(errorCode) + + def _getEnumFeature(self): + """ + Get the value of an enum feature. + + :returns: enum -- value of the specified feature. + """ + + # create args + valueToGet = c_char_p() + + errorCode = VimbaDLL.featureEnumGet(self._handle, + self._name, + byref(valueToGet)) + if errorCode != 0: + raise VimbaException(errorCode) + + return valueToGet.value + + def _setEnumFeature(self, valueToSet): + """ + Set the value of an enum feature. + + :param valueToSet: the enum value to set for the feature. + """ + + errorCode = VimbaDLL.featureEnumSet(self._handle, + self._name, + valueToSet) + if errorCode != 0: + raise VimbaException(errorCode) + + def _getStringFeature(self): + """ + Get the value of a string feature. + + :returns: string -- value of the specified feature. + """ + + # create args + bufferSize = 256 + valueToGet = create_string_buffer('\000' * bufferSize) + sizeFilled = c_uint32() + + errorCode = VimbaDLL.featureStringGet(self._handle, + self._name, + valueToGet, + bufferSize, + byref(sizeFilled)) + if errorCode != 0: + raise VimbaException(errorCode) + + return valueToGet.value + + def _setStringFeature(self, valueToSet): + """ + Set the value of a string feature. + + :param valueToSet: the string value to set for the feature. + """ + + errorCode = VimbaDLL.featureStringSet(self._handle, + self._name, + valueToSet) + if errorCode != 0: + raise VimbaException(errorCode) + + def _getBoolFeature(self): + """ + Get the value of a bool feature. + + :returns: bool -- value of the specified feature. + """ + + # create args + valueToGet = c_bool() + + errorCode = VimbaDLL.featureBoolGet(self._handle, + self._name, + byref(valueToGet)) + if errorCode != 0: + raise VimbaException(errorCode) + + return valueToGet.value + + def _setBoolFeature(self, valueToSet): + """ + Set the value of a bool feature. + + :param valueToSet: the bool value to set for the feature. + """ + + errorCode = VimbaDLL.featureBoolSet(self._handle, + self._name, + valueToSet) + if errorCode != 0: + raise VimbaException(errorCode) + + def _unknownRange(self): + """ + Returns empty for ranges that have not been implemented. + """ + return '' + + def _rangeQueryIntFeature(self): + """ + Get the range of an int feature. + + :returns: tuple -- min and max range. + """ + + # create args + minToGet = c_int64() + maxToGet = c_int64() + + errorCode = VimbaDLL.featureIntRangeQuery(self._handle, + self._name, + byref(minToGet), + byref(maxToGet)) + if errorCode != 0: + raise VimbaException(errorCode) + + return (int(str(minToGet.value)), int(str(maxToGet.value))) + + def _rangeQueryFloatFeature(self): + """ + Get the range of a float feature. + + :returns: tuple -- min and max range. + """ + + # create args + minToGet = c_double() + maxToGet = c_double() + + errorCode = VimbaDLL.featureFloatRangeQuery(self._handle, + self._name, + byref(minToGet), + byref(maxToGet)) + if errorCode != 0: + raise VimbaException(errorCode) + + return (minToGet.value, maxToGet.value) + + #def _rangeQueryEnumFeature(self): + # """ + # Get the range of an enum feature. + # + # :returns: tuple -- min and max range. + # """ + # + # # create args + # minToGet = c_uint32() + # maxToGet = c_uint32() + # + # errorCode = VimbaDLL.featureEnumRangeQuery(self._handle, + # self._name, + # byref(minToGet), + # byref(maxToGet)) + # if errorCode != 0: + # raise VimbaException(errorCode) + # + # return (minToGet.value, maxToGet.value) + diff --git a/pymba/vimbaframe.py b/pymba/vimbaframe.py new file mode 100644 index 0000000..e97360e --- /dev/null +++ b/pymba/vimbaframe.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbaexception import VimbaException +from vimbadll import VimbaDLL +from vimbadll import VimbaC_MemoryBlock +from ctypes import * + +class VimbaFrame(object): + """ + A Vimba frame. + """ + def __init__(self, camera): + self._camera = camera + self._handle = camera.handle + + # get frame sizes + self.payloadSize = self._camera.PayloadSize + self.width = self._camera.Width + self.height = self._camera.Height + + # frame structure + self._frame = structs.VimbaFrame() + + def announceFrame(self): + """ + Announce frames to the API that may be queued for frame capturing later. + """ + # size of expected frame + sizeOfFrame = self.payloadSize + + # keep this reference to keep block alive for life of frame + self._cMem = VimbaC_MemoryBlock(sizeOfFrame) + # set buffer to have length of expected payload size + self._frame.buffer = self._cMem.block + + # set buffer size to expected payload size + self._frame.bufferSize = sizeOfFrame + + errorCode = VimbaDLL.frameAnnounce(self._handle, + byref(self._frame), + sizeof(self._frame)) + if errorCode != 0: + raise VimbaException(errorCode) + + def revokeFrame(self): + """ + Revoke a frame from the API. + """ + errorCode = VimbaDLL.frameRevoke(self._handle, + byref(self._frame)) + if errorCode != 0: + raise VimbaException(errorCode) + + def queueFrameCapture(self): + """ + Queue frames that may be filled during frame capturing. + """ + errorCode = VimbaDLL.captureFrameQueue(self._handle, + byref(self._frame), + None) # callback not implemented, callback example in pico? + if errorCode != 0: + raise VimbaException(errorCode) + + def waitFrameCapture(self, timeout = 2000): + """ + Wait for a queued frame to be filled (or dequeued). + """ + errorCode = VimbaDLL.captureFrameWait(self._handle, + byref(self._frame), + timeout) + if errorCode != 0: + raise VimbaException(errorCode) + + # custom method for simplified usage + def getBufferByteData(self): + """ + Retrieve buffer data in a useful format. + + :returns: array -- buffer data. + """ + + # cast frame buffer memory contents to a usable type + data = cast(self._frame.buffer, + POINTER(c_ubyte * self.payloadSize)) + + # make array of c_ubytes from buffer + array = (c_ubyte * self.height * self.width).from_address(addressof(data.contents)) + + return array diff --git a/pymba/vimbainterface.py b/pymba/vimbainterface.py new file mode 100644 index 0000000..03a5f80 --- /dev/null +++ b/pymba/vimbainterface.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbaobject import VimbaObject +from vimbaexception import VimbaException +from vimbadll import VimbaDLL +from ctypes import * + +# interface features are automatically readable as object attributes. +class VimbaInterface(VimbaObject): + """ + A Vimba interface object. This class provides the minimal access + to Vimba functions required to control the interface. + """ + + @property + def interfaceIdString(self): + return self._interfaceIdString + + # own handle is inherited as self._handle + def __init__(self, interfaceIdString): + + # call super constructor + super(VimbaInterface, self).__init__() + + # set ID + self._interfaceIdString = interfaceIdString + + def openInterface(self): + """ + Open the interface. + """ + errorCode = VimbaDLL.interfaceOpen(self._interfaceIdString, + byref(self._handle)) + if errorCode != 0: + raise VimbaException(errorCode) + + def closeInterface(self): + """ + Close the interface. + """ + errorCode = VimbaDLL.interfaceClose(self._handle) + if errorCode != 0: + raise VimbaException(errorCode) + \ No newline at end of file diff --git a/pymba/vimbaobject.py b/pymba/vimbaobject.py new file mode 100644 index 0000000..8e2f812 --- /dev/null +++ b/pymba/vimbaobject.py @@ -0,0 +1,213 @@ +# -*- coding: utf-8 -*- +import vimbastructure as structs +from vimbaexception import VimbaException +from vimbafeature import VimbaFeature +from vimbadll import VimbaDLL +from ctypes import * + +class VimbaObject(object): + """ + A Vimba object has a handle and features associated with it. + Objects include System, Camera, Interface and AncillaryData. + """ + + @property + def handle(self): + return self._handle + + def __init__(self): + # create own handle + self._handle = c_void_p() + + # list of VimbaFeatureInfo objects + # can't set yet as the object (e.g. a camera) won't be + # opened yet, therefore no event for object opening + # so will have it populate by user interaction + # and blame them if the object is not opened then + self._featureInfos = None + + # override getattr for undefined attributes + def __getattr__(self, attr): + + # if a feature value requested (requires object (camera) open) + if attr in self.getFeatureNames(): + return VimbaFeature(attr, self._handle).value + + # otherwise don't know about it + raise AttributeError(''.join(["'VimbaObject' has no attribute '", + attr, + "'"])) + + # override setattr for undefined attributes + def __setattr__(self, attr, val): + + # set privates as normal + # check this first to allow all privates to set normally + # and avoid recursion errors + if attr.startswith('_'): + super(VimbaObject, self).__setattr__(attr, val) + + # if it's an actual camera feature (requires camera open) + elif attr in self.getFeatureNames(): + VimbaFeature(attr, self._handle).value = val + + # otherwise just set the attribute value as normal + else: + super(VimbaObject, self).__setattr__(attr, val) + + def _getFeatureInfos(self): + """ + Gets feature info of all available features. Will + cause error if object/camera is not opened. + + :returns: list -- feature info for available features. + """ + # check it's populated as can't populate it in __init__ + if self._featureInfos is None: + # args + dummyFeatureInfo = structs.VimbaFeatureInfo() + numFound = c_uint32(-1) + + # call once to get number of available features + # Vimba DLL will return an error code + errorCode = VimbaDLL.featuresList(self._handle, + byref(dummyFeatureInfo), + 0, + byref(numFound), + sizeof(dummyFeatureInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + + # number of features specified by Vimba + numFeatures = numFound.value + + # args + featureInfoArray = (structs.VimbaFeatureInfo * numFeatures)() + + # call again to get the features + # Vimba DLL will return an error code + errorCode = VimbaDLL.featuresList(self._handle, + featureInfoArray, + numFeatures, + byref(numFound), + sizeof(dummyFeatureInfo)) + if errorCode != 0: + raise VimbaException(errorCode) + + self._featureInfos = list(featInfo for featInfo in featureInfoArray) + return self._featureInfos + + def getFeatureNames(self): + """ + Get names of all available features. + + :returns: list -- feature names for available features. + """ + return list(featInfo.name for featInfo in self._getFeatureInfos()) + + def getFeatureInfo(self, featureName): + """ + Gets feature info object of specified feature. + + :param featureName: the name of the feature. + + :returns: VimbaFeatureInfo object -- the feature info object specified. + """ + # don't do this live as we already have this info + # return info object, if it exists + for featInfo in self._getFeatureInfos(): + if featInfo.name == featureName: + return featInfo + # otherwise raise error + raise VimbaException(-53) + + # don't think we ever need to return a feature object... + #def getFeature(self, featureName): + + def getFeatureRange(self, featureName): + """ + Get valid range of feature values. + + :param featureName: name of the feature to query. + + :returns: tuple -- range as (feature min value, feature max value). + """ + # can't cache this, need to look it up live + return VimbaFeature(featureName, self._handle).range + + def runFeatureCommand(self, featureName): + """ + Run a feature command. + + :param featureName: the name of the feature. + """ + # run a command + errorCode = VimbaDLL.featureCommandRun(self._handle, + featureName) + if errorCode != 0: + raise VimbaException(errorCode) + + def readRegister(self, address): + # note that the underlying Vimba function allows reading of an array + # of registers, but only one address/value at a time is implemented here + """ + Read from a register of the module (camera). + + :param address: the address of the register to read. + + :returns: int -- value of register. + """ + readCount = 1 + + # check address validity + try: + regAddress = c_uint64(int(address, 16)) + except: + raise VimbaException(-52) + + regData = c_uint64() + numCompleteReads = c_uint32() + + errorCode = VimbaDLL.registersRead(self.handle, + readCount, + byref(regAddress), + byref(regData), + byref(numCompleteReads)) + + if errorCode != 0: + raise VimbaException(errorCode) + + return regData.value + + def writeRegister(self, address, value): + # note that the underlying Vimba function allows writing of an array + # of registers, but only one address/value at a time is implemented here + """ + Read from a register of the module (camera). + + :param address: the address of the register to read. + :param value: the value to set in hex. + """ + writeCount = 1 + + # check address validity + try: + regAddress = c_uint64(int(address, 16)) + except: + raise VimbaException(-52) + + # check value validity + try: + regData = c_uint64(int(value, 16)) + except: + raise VimbaException(-52) + + numCompleteWrites = c_uint32() + + errorCode = VimbaDLL.registersWrite(self.handle, + writeCount, + byref(regAddress), + byref(regData), + byref(numCompleteWrites)) + if errorCode != 0: + raise VimbaException(errorCode) diff --git a/pymba/vimbastructure.py b/pymba/vimbastructure.py new file mode 100644 index 0000000..7b1e65c --- /dev/null +++ b/pymba/vimbastructure.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +from ctypes import * + + +class VimbaVersion(Structure): + _fields_ = [('major', c_uint32), + ('minor', c_uint32), + ('patch', c_uint32)] + + +class VimbaInterfaceInfo(Structure): + _fields_ = [('interfaceIdString', c_char_p), # Unique identifier for each interface + ('interfaceType', c_uint32), # Interface type, see VmbInterfaceType + ('interfaceName', c_char_p), # Interface name, given by the transport layer + ('serialString', c_char_p), # Serial number + ('permittedAccess', c_uint32)] # Used access mode, see VmbAccessModeType + + def getFieldNames(self): + """ + Get field names. + """ + return [field[0] for field in self._fields_] + + +class VimbaCameraInfo(Structure): + _fields_ = [('cameraIdString', c_char_p), # Unique identifier for each camera + ('cameraName', c_char_p), # Name of the camera + ('modelName', c_char_p), # Model name + ('serialString', c_char_p), # Serial number + ('permittedAccess', c_uint32), # Used access mode, see VmbAccessModeType + ('interfaceIdString', c_char_p)] # Unique value for each interface or bus + + def getFieldNames(self): + """ + Get field names. + """ + return [field[0] for field in self._fields_] + + +class VimbaFeatureInfo(Structure): + + _fields_ = [('name', c_char_p), + ('featureDataType', c_uint32), + ('featureFlags', c_uint32), + ('category', c_char_p), + ('displayName', c_char_p), + ('pollingTime', c_uint32), + ('unit', c_char_p), + ('representation', c_char_p), + ('visibility', c_uint32), + ('tooltip', c_char_p), + ('description', c_char_p), + ('sfncNamespace', c_char_p), + ('isStreamable', c_bool), + ('hasAffectedFeatures', c_bool), + ('hasSelectedFeatures', c_bool)] + + def getFieldNames(self): + """ + Get field names. + """ + return [field[0] for field in self._fields_] + + +class VimbaFrame(Structure): + + # IN + _fields_ = [('buffer', c_void_p), # Comprises image and ancillary data + ('bufferSize', c_uint32), # Size of the data buffer + + ('context', c_void_p * 4), # User context filled during queuing + + # OUT + ('receiveStatus', c_int32), # Resulting status of the receive operation + ('receiveFlags', c_uint32), # Resulting flags of the receive operation + + ('imageSize', c_uint32), # Size of the image data inside the data buffer + ('ancillarySize', c_uint32), # Size of the ancillary data inside the data buffer + + ('pixelFormat', c_uint32), # Pixel format of the image + + ('width', c_uint32), # Width of an image + ('height', c_uint32), # Height of an image + ('offsetX', c_uint32), # Horizontal offset of an image + ('offsetY', c_uint32), # Vertical offset of an image + + ('frameID', c_uint64), # Unique ID of this frame in this stream + ('timestamp', c_uint64)] # Timestamp of the data transfer + + def getFieldNames(self): + """ + Get field names. + """ + return [field[0] for field in self._fields_] + + + + + diff --git a/pymba/vimbasystem.py b/pymba/vimbasystem.py new file mode 100644 index 0000000..1e04755 --- /dev/null +++ b/pymba/vimbasystem.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from vimbaobject import VimbaObject +from ctypes import * + +# system features are automatically readable as attributes. +class VimbaSystem(VimbaObject): + """ + A Vimba system object. This class provides the minimal access + to Vimba functions required to control the system. + """ + + # own handle is inherited as self._handle + def __init__(self): + + # call super constructor + super(VimbaSystem, self).__init__() + + # set own handle manually + self._handle = c_void_p(1) diff --git a/pyvimba/__init__.py b/pyvimba/__init__.py deleted file mode 100644 index 96a931e..0000000 --- a/pyvimba/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# -*- coding: utf-8 -*- -from vimba import Vimba -from vimbaexception import VimbaException - diff --git a/pyvimba/tests/__init__.py b/pyvimba/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/pyvimba/tests/opencv_example.py b/pyvimba/tests/opencv_example.py deleted file mode 100644 index a1d7269..0000000 --- a/pyvimba/tests/opencv_example.py +++ /dev/null @@ -1,80 +0,0 @@ -from pyvimba.vimba import * -import numpy as np -import cv2 -import time - -#very crude example, assumes your camera is PixelMode = BAYERRG8 - -# start Vimba -vimba = Vimba() -vimba.startup() - -# get system object -system = vimba.getSystem() - -# list available cameras (after enabling discovery for GigE cameras) -if system.GeVTLIsPresent: - system.runFeatureCommand("GeVDiscoveryAllOnce") - time.sleep(0.2) -cameraIds = vimba.getCameraIds() -for cameraId in cameraIds: - print 'Camera ID:', cameraId - -# get and open a camera -camera0 = vimba.getCamera(cameraIds[0]) -camera0.openCamera() - -# list camera features -cameraFeatureNames = camera0.getFeatureNames() -for name in cameraFeatureNames: - print 'Camera feature:', name - -# read info of a camera feature -#featureInfo = camera0.getFeatureInfo('AcquisitionMode') -#for field in featInfo.getFieldNames(): -# print field, '--', getattr(featInfo, field) - -# get the value of a feature -print camera0.AcquisitionMode - -# set the value of a feature -camera0.AcquisitionMode = 'SingleFrame' - -# create new frames for the camera -frame0 = camera0.getFrame() # creates a frame -frame1 = camera0.getFrame() # creates a second frame - -# announce frame -frame0.announceFrame() - -# capture a camera image -count = 0 -while count < 10: - camera0.startCapture() - frame0.queueFrameCapture() - camera0.runFeatureCommand('AcquisitionStart') - camera0.runFeatureCommand('AcquisitionStop') - frame0.waitFrameCapture() - - # get image data... - imgData = frame0.getBufferByteData() - - moreUsefulImgData = np.ndarray(buffer = frame0.getBufferByteData(), - dtype = np.uint8, - shape = (frame0.height, - frame0.width, - 1)) - rgb = cv2.cvtColor(moreUsefulImgData, cv2.COLOR_BAYER_RG2RGB) - cv2.imwrite('foo{}.png'.format(count), rgb) - print "image {} saved".format(count) - count += 1 - camera0.endCapture() -# clean up after capture -camera0.revokeAllFrames() - -# close camera -camera0.closeCamera() - -# shutdown Vimba -vimba.shutdown() - diff --git a/pyvimba/tests/test_cameras.py b/pyvimba/tests/test_cameras.py deleted file mode 100644 index b9f42d4..0000000 --- a/pyvimba/tests/test_cameras.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/python - -from pyvimba.vimba import * -import time - -def test_cameras(): - # start Vimba - vimba = Vimba() - vimba.startup() - - # get system object - system = vimba.getSystem() - - # list available cameras (after enabling discovery for GigE cameras) - if system.GeVTLIsPresent: - system.runFeatureCommand("GeVDiscoveryAllOnce") - time.sleep(0.2) - cameraIds = vimba.getCameraIds() - for cameraId in cameraIds: - print 'Camera ID:', cameraId - - # get and open a camera - camera0 = vimba.getCamera(cameraIds[0]) - camera0.openCamera() - - # list camera features - cameraFeatureNames = camera0.getFeatureNames() - for name in cameraFeatureNames: - print 'Camera feature:', name - - # get the value of a feature - print camera0.AcquisitionMode - - # set the value of a feature - camera0.AcquisitionMode = 'SingleFrame' - - # create new frames for the camera - frame0 = camera0.getFrame() # creates a frame - frame1 = camera0.getFrame() # creates a second frame - - # announce frame - frame0.announceFrame() - - # capture a camera image - camera0.startCapture() - frame0.queueFrameCapture() - camera0.runFeatureCommand('AcquisitionStart') - camera0.runFeatureCommand('AcquisitionStop') - frame0.waitFrameCapture() - - # get image data... - imgData = frame0.getBufferByteData() - - # ...or use NumPy for fast image display (for use with OpenCV, etc) - import numpy as np - moreUsefulImgData = np.ndarray(buffer = frame0.getBufferByteData(), - dtype = np.uint8, - shape = (frame0.height, - frame0.width, - 1)) - - # clean up after capture - camera0.endCapture() - camera0.revokeAllFrames() - - # close camera - camera0.closeCamera() - - # shutdown Vimba - vimba.shutdown() diff --git a/pyvimba/tests/test_installation.py b/pyvimba/tests/test_installation.py deleted file mode 100644 index 060f453..0000000 --- a/pyvimba/tests/test_installation.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/python - -from pyvimba.vimba import * - -def test_installation(): - vimba = Vimba() - version = vimba.getVersion() - assert version == '1.2.0' diff --git a/pyvimba/tests/test_interfaces.py b/pyvimba/tests/test_interfaces.py deleted file mode 100644 index 2620289..0000000 --- a/pyvimba/tests/test_interfaces.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/python - -from pyvimba.vimba import * -import time - -def test_interfaces(): - # start Vimba - vimba = Vimba() - vimba.startup() - - # get list of available interfaces - interfaceIds = vimba.getInterfaceIds() - for interfaceId in interfaceIds: - print 'Interface ID:', interfaceId - - # get interface object and open it - interface0 = vimba.getInterface(interfaceIds[0]) - interface0.openInterface() - - # list interface features - interfaceFeatureNames = interface0.getFeatureNames() - for name in interfaceFeatureNames: - print 'Interface feature:', name - - # close interface - interface0.closeInterface() - - # shutdown Vimba - vimba.shutdown() diff --git a/pyvimba/tests/test_systemfeature.py b/pyvimba/tests/test_systemfeature.py deleted file mode 100644 index 49a2d3e..0000000 --- a/pyvimba/tests/test_systemfeature.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/python - -from pyvimba.vimba import * - -def test_systemfeature(): - # get system object - vimba = Vimba() - system = vimba.getSystem() - - # list system features - for featureName in system.getFeatureNames(): - print 'System feature:', featureName - - # shutdown Vimba - vimba.shutdown() - diff --git a/pyvimba/vimba.py b/pyvimba/vimba.py deleted file mode 100644 index 0add297..0000000 --- a/pyvimba/vimba.py +++ /dev/null @@ -1,231 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbadll import VimbaDLL -from vimbaexception import VimbaException -from vimbasystem import VimbaSystem -from vimbacamera import VimbaCamera -from vimbainterface import VimbaInterface -from ctypes import * - -class Vimba(object): - """ - An Allied Vision Technology Vimba API. - This API provides access to AVT cameras. - """ - - # todo - assign camera info and feature info as own object proeprties - - def __init__(self): - - # create own system singleton object - self._system = VimbaSystem() - - # lists of VimbaCameraInfo and VimbaInterfaceInfo objects - # can't be called before startup() so populate later - self._cameraInfos = None - self._interfaceInfos = None - - # dict of {camera ID : VimbaCamera object} as we don't want to forget them - self._cameras = {} - - # dict of {interface ID : VimbaInterface object} as we don't want to forget them - self._interfaces = {} - - def _getInterfaceInfos(self): - """ - Gets interface info of all available interfaces. - - :returns: list -- interface info for available interfaces. - """ - if self._interfaceInfos is None: - # args - dummyInterfaceInfo = structs.VimbaInterfaceInfo() - numFound = c_uint32(-1) - - # call once just to get the number of interfaces - # Vimba DLL will return an error code - errorCode = VimbaDLL.interfacesList(byref(dummyInterfaceInfo), - 0, - byref(numFound), - sizeof(dummyInterfaceInfo)) - if errorCode != 0: - print errorCode - raise VimbaException(errorCode) - - numInterfaces = numFound.value - - # args - interfaceInfoArray = (structs.VimbaInterfaceInfo * numInterfaces)() - - # call again to get the features - # Vimba DLL will return an error code - errorCode = VimbaDLL.interfacesList(interfaceInfoArray, - numInterfaces, - byref(numFound), - sizeof(dummyInterfaceInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - self._interfaceInfos = list(interfaceInfo for interfaceInfo in interfaceInfoArray) - return self._interfaceInfos - - def _getCameraInfos(self): - """ - Gets camera info of all attached cameras. - - :returns: list -- camera info for available cameras. - """ - if self._cameraInfos is None: - # args - dummyCameraInfo = structs.VimbaCameraInfo() - numFound = c_uint32(-1) - - # call once just to get the number of cameras - # Vimba DLL will return an error code - errorCode = VimbaDLL.camerasList(byref(dummyCameraInfo), - 0, - byref(numFound), - sizeof(dummyCameraInfo)) - if errorCode != 0: - print errorCode - raise VimbaException(errorCode) - - numCameras = numFound.value - - # args - cameraInfoArray = (structs.VimbaCameraInfo * numCameras)() - - # call again to get the features - # Vimba DLL will return an error code - errorCode = VimbaDLL.camerasList(cameraInfoArray, - numCameras, - byref(numFound), - sizeof(dummyCameraInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - self._cameraInfos = list(camInfo for camInfo in cameraInfoArray) - return self._cameraInfos - - def getSystem(self): - """ - Gets system singleton object. - - :returns: VimbaSystem object -- the system singleton object. - """ - return self._system - - def getInterfaceIds(self): - """ - Gets IDs of all available interfaces. - - :returns: list -- interface IDs for available interfaces. - """ - return list(interfaceInfo.interfaceIdString for interfaceInfo in self._getInterfaceInfos()) - - def getCameraIds(self): - """ - Gets IDs of all available cameras. - - :returns: list -- camera IDs for available cameras. - """ - return list(camInfo.cameraIdString for camInfo in self._getCameraInfos()) - - def getInterfaceInfo(self, interfaceId): - """ - Gets interface info object of specified interface. - - :param interfaceId: the ID of the interface object to get. - - :returns: VimbaInterfaceInfo object -- the interface info object specified. - """ - # don't do this live as we already have this info - # return info object if it exists - for interfaceInfo in self._getInterfaceInfos(): - if interfaceInfo.interfaceIdString == interfaceId: - return interfaceInfo - # otherwise raise error - raise VimbaException(-54) - - def getCameraInfo(self, cameraId): - """ - Gets camera info object of specified camera. - - :param cameraId: the ID of the camera object to get. - - :returns: VimbaCameraInfo object -- the camera info object specified. - """ - # don't do this live as we already have this info - # return info object if it exists - for camInfo in self._getCameraInfos(): - if camInfo.cameraIdString == cameraId: - return camInfo - # otherwise raise error - raise VimbaException(-50) - - def getInterface(self, interfaceId): - """ - Gets interface object based on interface ID string. Will not recreate - interface object if it already exists. - - :param interfaceId: the ID of the interface. - - :returns: VimbaInterface object -- the interface object specified. - """ - # check ID is valid - if interfaceId in self.getInterfaceIds(): - # create it if it doesn't exist - if interfaceId not in self._interfaces: - self._interfaces[interfaceId] = VimbaInterface(interfaceId) - return self._interfaces[interfaceId] - raise VimbaException(-54) - - def getCamera(self, cameraId): - """ - Gets camera object based on camera ID string. Will not recreate - camera object if it already exists. - - :param cameraId: the ID of the camera. - - :returns: VimbaCamera object -- the camera object specified. - """ - # check ID is valid - if cameraId in self.getCameraIds(): - # create it if it doesn't exist - if cameraId not in self._cameras: - self._cameras[cameraId] = VimbaCamera(cameraId) - return self._cameras[cameraId] - raise VimbaException(-50) - - def getVersion(self): - """ - Retrieve the version number of VimbaC. - - :returns: string - Vimba API version info. - """ - # args - versionInfo = structs.VimbaVersion() - - # Vimba DLL will return an error code - errorCode = VimbaDLL.versionQuery(versionInfo, - sizeof(versionInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - - versionStr = '.'.join([str(versionInfo.major), - str(versionInfo.minor), - str(versionInfo.patch)]) - return versionStr - - def startup(self): - """ - Initialize the VimbaC API. - """ - # Vimba DLL will return an error code - errorCode = VimbaDLL.startup() - if errorCode != 0: - raise VimbaException(errorCode) - - def shutdown(self): - """ - Perform a shutdown on the API. - """ - VimbaDLL.shutdown() diff --git a/pyvimba/vimbacamera.py b/pyvimba/vimbacamera.py deleted file mode 100644 index 747eb4c..0000000 --- a/pyvimba/vimbacamera.py +++ /dev/null @@ -1,124 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbaobject import VimbaObject -from vimbaexception import VimbaException -from vimbaframe import VimbaFrame -from vimbadll import VimbaDLL -from ctypes import * - -# camera features are automatically readable as object attributes. -class VimbaCamera(VimbaObject): - """ - A Vimba camera object. This class provides the minimal access - to Vimba functions required to control the camera. - """ - - @property - def cameraIdString(self): - return self._cameraIdString - - # own handle is inherited as self._handle - def __init__(self, cameraIdString): - - # call super constructor - super(VimbaCamera, self).__init__() - - # set ID - self._cameraIdString = cameraIdString - - # set own info - self._info = self._getInfo() - - def getInfo(self): - """ - Get info of the camera. Does not require - the camera to be opened. - - :returns: VimbaCameraInfo object -- camera information. - """ - return self._info - - def _getInfo(self): - """ - Get info of the camera. Does not require - the camera to be opened. - - :returns: VimbaCameraInfo object -- camera information. - """ - # args for Vimba call - cameraInfo = structs.VimbaCameraInfo() - - # Vimba DLL will return an error code - errorCode = VimbaDLL.cameraInfoQuery(self._cameraIdString, - byref(cameraInfo), - sizeof(cameraInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - - return cameraInfo - - def openCamera(self): - """ - Open the camera. - """ - # args for Vimba call - cameraAccessMode = 1 # full access (see VmbAccessModeType) - - errorCode = VimbaDLL.cameraOpen(self._cameraIdString, - cameraAccessMode, - byref(self._handle)) - if errorCode != 0: - raise VimbaException(errorCode) - - def closeCamera(self): - """ - Close the camera. - """ - errorCode = VimbaDLL.cameraClose(self._handle) - if errorCode != 0: - raise VimbaException(errorCode) - - def revokeAllFrames(self): - """ - Revoke all frames assigned to the camera. - """ - errorCode = VimbaDLL.frameRevokeAll(self._handle) - if errorCode != 0: - raise VimbaException(errorCode) - - def startCapture(self): - """ - Prepare the API for incoming frames. - """ - errorCode = VimbaDLL.captureStart(self._handle) - if errorCode != 0: - raise VimbaException(errorCode) - - def endCapture(self): - """ - Stop the API from being able to receive frames. - """ - errorCode = VimbaDLL.captureEnd(self._handle) - if errorCode != 0: - raise VimbaException(errorCode) - - def flushCaptureQueue(self): - """ - Flush the capture queue. - """ - errorCode = VimbaDLL.captureQueueFlush(self._handle) - if errorCode != 0: - raise VimbaException(errorCode) - - # method for easy frame creation - def getFrame(self): - """ - Creates and returns a new frame object. Multiple frames - per camera can therefore be returned. - - :returns: VimbaFrame object -- the new frame. - """ - return VimbaFrame(self) - - - diff --git a/pyvimba/vimbadll.py b/pyvimba/vimbadll.py deleted file mode 100644 index f21f7f6..0000000 --- a/pyvimba/vimbadll.py +++ /dev/null @@ -1,387 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbaexception import VimbaException -from sys import platform -import os -from ctypes import * - -if platform == "win32": - from ctypes.util import find_msvcrt - _cruntime = cdll.LoadLibrary(find_msvcrt()) - vimbaC_path = r'C:\Program Files\Allied Vision Technologies\AVTVimba_1.2\VimbaC\Bin\Win32\VimbaC.dll' - dll_loader = windll -else: - _cruntime = CDLL("libc.so.6") - dll_loader = cdll - assert os.environ.get("GENICAM_GENTL64_PATH"), "you need your GENICAM_GENTL64_PATH environment set. Make sure you have Vimba installed, and you have loaded the /etc/profile.d/ scripts" - vimba_dir = "/".join(os.environ.get("GENICAM_GENTL64_PATH").split("/")[1:-3]) - vimbaC_path = "/" + vimba_dir + "/VimbaC/DynamicLib/x86_64bit/libVimbaC.so" - -with open(vimbaC_path) as thefile: - pass #NJO i think this is kind of like an os.exists ? - - -class VimbaDLL(object): - """ - ctypes directives to make the wrapper class work cleanly, - talks to VimbaC.dll - """ - # a full list of Vimba API methods - # (only double dashed methods have been implemented so far) - # - # -- VmbVersionQuery() - # - # -- VmbStartup() - # -- VmbShutdown() - # - # -- VmbCamerasList() - # -- VmbCameraInfoQuery() - # -- VmbCameraOpen() - # -- VmbCameraClose() - # - # -- VmbFeaturesList() - # -- VmbFeatureInfoQuery() - # VmbFeatureListAffected() - # VmbFeatureListSelected() - # VmbFeatureAccessQuery() - # - # -- VmbFeatureIntGet() - # -- VmbFeatureIntSet() - # -- VmbFeatureIntRangeQuery() - # VmbFeatureIntIncrementQuery() - # - # -- VmbFeatureFloatGet() - # -- VmbFeatureFloatSet() - # -- VmbFeatureFloatRangeQuery() - # - # -- VmbFeatureEnumGet() - # -- VmbFeatureEnumSet() - # VmbFeatureEnumRangeQuery() - # VmbFeatureEnumIsAvailable() - # VmbFeatureEnumAsInt() - # VmbFeatureEnumAsString() - # VmbFeatureEnumEntryGet() - # - # -- VmbFeatureStringGet() - # -- VmbFeatureStringSet() - # VmbFeatureStringMaxlengthQuery() - # - # -- VmbFeatureBoolGet() - # -- VmbFeatureBoolSet() - # - # -- VmbFeatureCommandRun() - # VmbFeatureCommandIsDone() - # - # VmbFeatureRawGet() - # VmbFeatureRawSet() - # VmbFeatureRawLengthQuery() - # - # VmbFeatureInvalidationRegister() - # VmbFeatureInvalidationUnregister() - # - # -- VmbFrameAnnounce() - # -- VmbFrameRevoke() - # -- VmbFrameRevokeAll() - # -- VmbCaptureStart() - # -- VmbCaptureEnd() - # -- VmbCaptureFrameQueue() - # -- VmbCaptureFrameWait() - # -- VmbCaptureQueueFlush() - # - # -- VmbInterfacesList() - # -- VmbInterfaceOpen() - # -- VmbInterfaceClose() - # - # VmbAncillaryDataOpen() - # VmbAncillaryDataClose() - # - # VmbMemoryRead() - # VmbMemoryWrite() - # -- VmbRegistersRead() - # -- VmbRegistersWrite() - - # Vimba C API DLL - - _vimbaDLL = dll_loader.LoadLibrary(vimbaC_path) - - # version query - versionQuery = _vimbaDLL.VmbVersionQuery - versionQuery.restype = c_int32 # returned error code - versionQuery.argtypes = (POINTER(structs.VimbaVersion), # pointer to version structure - c_uint32) # version structure size - - # startup - startup = _vimbaDLL.VmbStartup - startup.restype = c_int32 # returned error code - - # shutdown - shutdown = _vimbaDLL.VmbShutdown - - # list cameras - camerasList = _vimbaDLL.VmbCamerasList - camerasList.restype = c_int32 # returned error code - camerasList.argtypes = (POINTER(structs.VimbaCameraInfo), # pointer to camera info structure - c_uint32, # length of list - POINTER(c_uint32), # pointer to number of cameras - c_uint32) # camera info structure size - - # camera info query - cameraInfoQuery = _vimbaDLL.VmbCameraInfoQuery - cameraInfoQuery.restype = c_int32 - cameraInfoQuery.argtypes = (c_char_p, # camera unique id - POINTER(structs.VimbaCameraInfo), # pointer to camera info structure - c_uint32) # size of structure - - # camera open - cameraOpen = _vimbaDLL.VmbCameraOpen - cameraOpen.restype = c_int32 # returned error code - cameraOpen.argtypes = (c_char_p, # camera unique id - c_uint32, # access mode - c_void_p) # camera handle, pointer to a pointer - - # camera close - cameraClose = _vimbaDLL.VmbCameraClose - cameraClose.restype = c_int32 # returned error code - cameraClose.argtypes = (c_void_p,) # camera handle - - # list features - featuresList = _vimbaDLL.VmbFeaturesList - featuresList.restype = c_int32 - featuresList.argtypes = (c_void_p, # handle, in this case camera handle - POINTER(structs.VimbaFeatureInfo), # pointer to feature info structure - c_uint32, # list length - POINTER(c_uint32), # pointer to num features found - c_uint32) # feature info size - - # feature info query - featureInfoQuery = _vimbaDLL.VmbFeatureInfoQuery - featureInfoQuery.restype = c_int32 - featureInfoQuery.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of feature - POINTER(structs.VimbaFeatureInfo), # pointer to feature info structure - c_uint32) # size of structure - - # get the int value of a feature - featureIntGet = _vimbaDLL.VmbFeatureIntGet - featureIntGet.restype = c_int32 - featureIntGet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - POINTER(c_int64)) # value to get - - # set the int value of a feature - featureIntSet = _vimbaDLL.VmbFeatureIntSet - featureIntSet.restype = c_int32 - featureIntSet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - c_int64) # value to set # get the value of an integer feature - - # query the range of values of the feature - featureIntRangeQuery = _vimbaDLL.VmbFeatureIntRangeQuery - featureIntRangeQuery.restype = c_int32 - featureIntRangeQuery.argtypes = (c_void_p, # handle - c_char_p, # name of the feature - POINTER(c_int64), # min range - POINTER(c_int64)) # max range - - # get the float value of a feature - featureFloatGet = _vimbaDLL.VmbFeatureFloatGet - featureFloatGet.restype = c_int32 - featureFloatGet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - POINTER(c_double)) # value to get - - # set the float value of a feature - featureFloatSet = _vimbaDLL.VmbFeatureFloatSet - featureFloatSet.restype = c_int32 - featureFloatSet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - c_double) # value to set - - # query the range of values of the feature - featureFloatRangeQuery = _vimbaDLL.VmbFeatureFloatRangeQuery - featureFloatRangeQuery.restype = c_int32 - featureFloatRangeQuery.argtypes = (c_void_p, # handle - c_char_p, # name of the feature - POINTER(c_double), # min range - POINTER(c_double)) # max range - - # get the enum value of a feature - featureEnumGet = _vimbaDLL.VmbFeatureEnumGet - featureEnumGet.restype = c_int32 - featureEnumGet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - POINTER(c_char_p)) # value to get - - # set the enum value of a feature - featureEnumSet = _vimbaDLL.VmbFeatureEnumSet - featureEnumSet.restype = c_int32 - featureEnumSet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - c_char_p) # value to set - - # get the string value of a feature - featureStringGet = _vimbaDLL.VmbFeatureStringGet - featureStringGet.restype = c_int32 - featureStringGet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - c_char_p, # string buffer to fill - c_uint32, # size of the input buffer - POINTER(c_uint32)) # string buffer to fill - - # set the string value of a feature - featureStringSet = _vimbaDLL.VmbFeatureStringSet - featureStringSet.restype = c_int32 - featureStringSet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - c_char_p) # value to set - - # get the boolean value of a feature - featureBoolGet = _vimbaDLL.VmbFeatureBoolGet - featureBoolGet.restype = c_int32 - featureBoolGet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - POINTER(c_bool)) # value to get - - # set the boolean value of a feature - featureBoolSet = _vimbaDLL.VmbFeatureBoolSet - featureBoolSet.restype = c_int32 - featureBoolSet.argtypes = (c_void_p, # handle, in this case camera handle - c_char_p, # name of the feature - c_bool) # value to set - - # run a feature command - featureCommandRun = _vimbaDLL.VmbFeatureCommandRun - featureCommandRun.restype = c_int32 - featureCommandRun.argtypes = (c_void_p, # handle for a module that exposes features - c_char_p) # name of the command feature - - # announce frames to the API that may be queued for frame capturing later - frameAnnounce = _vimbaDLL.VmbFrameAnnounce - frameAnnounce.restype = c_int32 - frameAnnounce.argtypes = (c_void_p, # camera handle - POINTER(structs.VimbaFrame), # pointer to frame - c_uint32) # size of frame - - # revoke a frame from the API - frameRevoke = _vimbaDLL.VmbFrameRevoke - frameRevoke.restype = c_int32 - frameRevoke.argtypes = (c_void_p, # camera handle - POINTER(structs.VimbaFrame)) # pointer to frame - - # revoke all frames assigned to a certain camera - frameRevokeAll = _vimbaDLL.VmbFrameRevokeAll - frameRevokeAll.restype = c_int32 - frameRevokeAll.argtypes = (c_void_p,) # camera handle - - # prepare the API for incoming frames - captureStart = _vimbaDLL.VmbCaptureStart - captureStart.restype = c_int32 - captureStart.argtypes = (c_void_p,) # camera handle - - # stop the API from being able to receive frames - captureEnd = _vimbaDLL.VmbCaptureEnd - captureEnd.restype = c_int32 - captureEnd.argtypes = (c_void_p,) # camera handle - - # queue frames that may be filled during frame capturing - captureFrameQueue = _vimbaDLL.VmbCaptureFrameQueue - captureFrameQueue.restype = c_int32 - captureFrameQueue.argtypes = (c_void_p, - POINTER(structs.VimbaFrame), - c_void_p) # callback - - # wait for a queued frame to be filled (or dequeued) - captureFrameWait = _vimbaDLL.VmbCaptureFrameWait - captureFrameWait.restype = c_int32 - captureFrameWait.argtypes = (c_void_p, # camera handle - POINTER(structs.VimbaFrame), - c_uint32) # timeout - - # flush the capture queue - captureQueueFlush = _vimbaDLL.VmbCaptureQueueFlush - captureQueueFlush.restype = c_int32 - captureQueueFlush.argtypes = (c_void_p,) # camera handle - - # list interfaces - interfacesList = _vimbaDLL.VmbInterfacesList - interfacesList.restype = c_int32 - interfacesList.argtypes = (POINTER(structs.VimbaInterfaceInfo), # pointer to interface info structure - c_uint32, # length of list - POINTER(c_uint32), # pointer to number of interfaces - c_uint32) - - # open interface - interfaceOpen = _vimbaDLL.VmbInterfaceOpen - interfaceOpen.restype = c_int32 - interfaceOpen.argtypes = (c_char_p, # unique id - c_void_p) # handle - - # close interface - interfaceClose = _vimbaDLL.VmbInterfaceClose - interfaceClose.restype = c_int32 - interfaceClose.argtypes = (c_void_p,) # handle - - # read from register - registersRead = _vimbaDLL.VmbRegistersRead - registersRead.restype = c_int32 - registersRead.argtypes = (c_void_p, # handle - c_uint32, # read count - POINTER(c_uint64), # pointer to address array - POINTER(c_uint64), # pointer to data array - POINTER(c_uint32)) # pointer to num complete reads - - # write to register - registersWrite = _vimbaDLL.VmbRegistersWrite - registersWrite.restype = c_int32 - registersWrite.argtypes = (c_void_p, # handle - c_uint32, # write count - POINTER(c_uint64), # pointer to address array - POINTER(c_uint64), # pointer to data array - POINTER(c_uint32)) # pointer to num complete write - - -class VimbaC_MemoryBlock(object): - """ - Just a memory block object for dealing - neatly with C memory allocations. - """ - - # C runtime DLL - _crtDLL = _cruntime - - @property - def block(self): - return self._block - - def __init__(self, blockSize): - - # assign memory block - malloc = self._crtDLL.malloc - malloc.argtypes = (c_size_t,) - malloc.restype = c_void_p - self._block = malloc(blockSize) # todo check for NULL on failure - - # this seems to be None if too much memory is requested - if self._block is None: - raise VimbaException(-51) - - def __del__(self): - - # free memory block - free = self._crtDLL.free - free.argtypes = (c_void_p,) - free.restype = None - free(self._block) - - - - - - - - - - - - - diff --git a/pyvimba/vimbaexception.py b/pyvimba/vimbaexception.py deleted file mode 100644 index 034f194..0000000 --- a/pyvimba/vimbaexception.py +++ /dev/null @@ -1,63 +0,0 @@ -# -*- coding: utf-8 -*- -import exceptions - -class VimbaException(Exception): - """ - An exception for the AVT Vimba API. It contains a message - property which is a string indicating what went wrong. - - :param errorCode: Error code to be used to look up error message. - """ - - @property - def message(self): - return self._errorCodes[self.errorCode] - - @property - def errorCode(self): - return self._errorCode - - _errorCodes = { # Vimba C API specific errors - 0: 'No error.', - -1: 'Unexpected fault in VimbaC or driver.', - -2: 'VmbStartup() was not called before the current command.', - -3: 'The designated instance (camera, feature etc.) cannot be found.', - -4: 'The given handle is not valid, ensure device open.', - -5: 'Device was not opened for usage.', - -6: 'Operation is invalid with the current access mode.', - -7: 'One of the parameters was invalid (usually an illegal pointer).', - -8: 'The given struct size is not valid for this version of the API.', - -9: 'More data was returned in a string/list than space was provided.', - -10: 'The feature type for this access function was wrong.', - -11: 'The value was not valid; either out of bounds or not an increment of the minimum.', - -12: 'Timeout during wait.', - -13: 'Other error.', - -14: 'Resources not available (e.g. memory).', - -15: 'Call is invalid in the current context (e.g. callback).', - -16: 'No transport layers were found.', - -17: 'API feature is not implemented.', - -18: 'API feature is not supported.', - -19: 'A multiple registers read or write was partially completed.', - - # Custom errors - -50: 'Could not find the specified camera.', - -51: 'Not enough memory to assign frame buffer.', - -52: 'Invalid input.', - -53: 'Could not find the specified feature.', - -54: 'Could not find the specified interface.', - - # Miscellaneous errors - -1000: 'Oops, unknown internal error code!', - -1001: 'Oops, this VimbaFeature function is not yet implemented in pymba!'} - - def __init__(self, errorCode): - # if error code does not match expected codes then assign invalid code - if errorCode in self._errorCodes: - self._errorCode = errorCode - else: - self._errorCode = -1000 - - - - - diff --git a/pyvimba/vimbafeature.py b/pyvimba/vimbafeature.py deleted file mode 100644 index c1d7e60..0000000 --- a/pyvimba/vimbafeature.py +++ /dev/null @@ -1,321 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbaexception import VimbaException -from vimbadll import VimbaDLL -from ctypes import * - -# class may extend a generic Vimba entity class one day... -class VimbaFeature(object): - """ - A feature of a Vimba object. - """ - - @property - def name(self): - return self._name - - @property - def handle(self): - return self._handle - - # lookup relevant function for feature type and pass to that function - @property - def value(self): - return self._getSetTypeFuncs[self._info.featureDataType][0]() - @value.setter - def value(self, val): - self._getSetTypeFuncs[self._info.featureDataType][1](val) - - @property - def range(self): - return self._rangeQueryTypeFuncs[self._info.featureDataType]() - - def __init__(self, name, handle): - - # set name and handle - self._name = name - self._handle = handle - - # set own info - self._info = self._getInfo() - - # type functions dict for looking up correct get/set function to use - self._getSetTypeFuncs = {0 : (self._notYetImplemented, self._notYetImplemented), # todo - 1 : (self._getIntFeature, self._setIntFeature), - 2 : (self._getFloatFeature, self._setFloatFeature), - 3 : (self._getEnumFeature, self._setEnumFeature), - 4 : (self._getStringFeature, self._setStringFeature), - 5 : (self._getBoolFeature, self._setBoolFeature), - 6 : (self._notYetImplemented, self._notYetImplemented), # todo - 7 : (self._notYetImplemented, self._notYetImplemented), # todo - 8 : (self._notYetImplemented, self._notYetImplemented)} # todo - - # type functions dict for looking up correct range function to use - self._rangeQueryTypeFuncs = {0 : self._unknownRange, - 1 : self._rangeQueryIntFeature, - 2 : self._rangeQueryFloatFeature, - 3 : self._unknownRange, - 4 : self._unknownRange, - 5 : self._unknownRange, - 6 : self._unknownRange, - 7 : self._unknownRange, - 8 : self._unknownRange} - - def getInfo(self): - """ - Get info of the feature. - - :returns: VimbaFeatureInfo object -- feature information.. - """ - return self._info - - def _getInfo(self): - """ - Get info of the feature. - - :returns: VimbaFeatureInfo object -- feature information.. - """ - # args for Vimba call - featureInfo = structs.VimbaFeatureInfo() - - # Vimba DLL will return an error code - errorCode = VimbaDLL.featureInfoQuery(self._handle, - self._name, - byref(featureInfo), - sizeof(featureInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - - return featureInfo - - def _notYetImplemented(self, val = None): - """ - Raises exception if feature value type is not yet defined. - """ - raise VimbaException(-1001) - - def _getIntFeature(self): - """ - Get the value of an integer feature. - - :returns: int -- value of the specified feature. - """ - - # create args - valueToGet = c_int64() - - errorCode = VimbaDLL.featureIntGet(self._handle, - self._name, - byref(valueToGet)) - if errorCode != 0: - raise VimbaException(errorCode) - - return valueToGet.value - - def _setIntFeature(self, valueToSet): - """ - Set the value of an integer feature. - - :param valueToSet: the int value to set for the feature. - """ - - errorCode = VimbaDLL.featureIntSet(self._handle, - self._name, - valueToSet) - if errorCode != 0: - raise VimbaException(errorCode) - - def _getFloatFeature(self): - """ - Get the value of a float feature. - - :returns: float -- value of the specified feature. - """ - - # create args - valueToGet = c_double() - - errorCode = VimbaDLL.featureFloatGet(self._handle, - self._name, - byref(valueToGet)) - if errorCode != 0: - raise VimbaException(errorCode) - - return valueToGet.value - - def _setFloatFeature(self, valueToSet): - """ - Set the value of a float feature. - - :param valueToSet: the float value to set for the feature. - """ - - errorCode = VimbaDLL.featureFloatSet(self._handle, - self._name, - valueToSet) - if errorCode != 0: - raise VimbaException(errorCode) - - def _getEnumFeature(self): - """ - Get the value of an enum feature. - - :returns: enum -- value of the specified feature. - """ - - # create args - valueToGet = c_char_p() - - errorCode = VimbaDLL.featureEnumGet(self._handle, - self._name, - byref(valueToGet)) - if errorCode != 0: - raise VimbaException(errorCode) - - return valueToGet.value - - def _setEnumFeature(self, valueToSet): - """ - Set the value of an enum feature. - - :param valueToSet: the enum value to set for the feature. - """ - - errorCode = VimbaDLL.featureEnumSet(self._handle, - self._name, - valueToSet) - if errorCode != 0: - raise VimbaException(errorCode) - - def _getStringFeature(self): - """ - Get the value of a string feature. - - :returns: string -- value of the specified feature. - """ - - # create args - bufferSize = 256 - valueToGet = create_string_buffer('\000' * bufferSize) - sizeFilled = c_uint32() - - errorCode = VimbaDLL.featureStringGet(self._handle, - self._name, - valueToGet, - bufferSize, - byref(sizeFilled)) - if errorCode != 0: - raise VimbaException(errorCode) - - return valueToGet.value - - def _setStringFeature(self, valueToSet): - """ - Set the value of a string feature. - - :param valueToSet: the string value to set for the feature. - """ - - errorCode = VimbaDLL.featureStringSet(self._handle, - self._name, - valueToSet) - if errorCode != 0: - raise VimbaException(errorCode) - - def _getBoolFeature(self): - """ - Get the value of a bool feature. - - :returns: bool -- value of the specified feature. - """ - - # create args - valueToGet = c_bool() - - errorCode = VimbaDLL.featureBoolGet(self._handle, - self._name, - byref(valueToGet)) - if errorCode != 0: - raise VimbaException(errorCode) - - return valueToGet.value - - def _setBoolFeature(self, valueToSet): - """ - Set the value of a bool feature. - - :param valueToSet: the bool value to set for the feature. - """ - - errorCode = VimbaDLL.featureBoolSet(self._handle, - self._name, - valueToSet) - if errorCode != 0: - raise VimbaException(errorCode) - - def _unknownRange(self): - """ - Returns empty for ranges that have not been implemented. - """ - return '' - - def _rangeQueryIntFeature(self): - """ - Get the range of an int feature. - - :returns: tuple -- min and max range. - """ - - # create args - minToGet = c_int64() - maxToGet = c_int64() - - errorCode = VimbaDLL.featureIntRangeQuery(self._handle, - self._name, - byref(minToGet), - byref(maxToGet)) - if errorCode != 0: - raise VimbaException(errorCode) - - return (int(str(minToGet.value)), int(str(maxToGet.value))) - - def _rangeQueryFloatFeature(self): - """ - Get the range of a float feature. - - :returns: tuple -- min and max range. - """ - - # create args - minToGet = c_double() - maxToGet = c_double() - - errorCode = VimbaDLL.featureFloatRangeQuery(self._handle, - self._name, - byref(minToGet), - byref(maxToGet)) - if errorCode != 0: - raise VimbaException(errorCode) - - return (minToGet.value, maxToGet.value) - - #def _rangeQueryEnumFeature(self): - # """ - # Get the range of an enum feature. - # - # :returns: tuple -- min and max range. - # """ - # - # # create args - # minToGet = c_uint32() - # maxToGet = c_uint32() - # - # errorCode = VimbaDLL.featureEnumRangeQuery(self._handle, - # self._name, - # byref(minToGet), - # byref(maxToGet)) - # if errorCode != 0: - # raise VimbaException(errorCode) - # - # return (minToGet.value, maxToGet.value) - diff --git a/pyvimba/vimbaframe.py b/pyvimba/vimbaframe.py deleted file mode 100644 index e97360e..0000000 --- a/pyvimba/vimbaframe.py +++ /dev/null @@ -1,89 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbaexception import VimbaException -from vimbadll import VimbaDLL -from vimbadll import VimbaC_MemoryBlock -from ctypes import * - -class VimbaFrame(object): - """ - A Vimba frame. - """ - def __init__(self, camera): - self._camera = camera - self._handle = camera.handle - - # get frame sizes - self.payloadSize = self._camera.PayloadSize - self.width = self._camera.Width - self.height = self._camera.Height - - # frame structure - self._frame = structs.VimbaFrame() - - def announceFrame(self): - """ - Announce frames to the API that may be queued for frame capturing later. - """ - # size of expected frame - sizeOfFrame = self.payloadSize - - # keep this reference to keep block alive for life of frame - self._cMem = VimbaC_MemoryBlock(sizeOfFrame) - # set buffer to have length of expected payload size - self._frame.buffer = self._cMem.block - - # set buffer size to expected payload size - self._frame.bufferSize = sizeOfFrame - - errorCode = VimbaDLL.frameAnnounce(self._handle, - byref(self._frame), - sizeof(self._frame)) - if errorCode != 0: - raise VimbaException(errorCode) - - def revokeFrame(self): - """ - Revoke a frame from the API. - """ - errorCode = VimbaDLL.frameRevoke(self._handle, - byref(self._frame)) - if errorCode != 0: - raise VimbaException(errorCode) - - def queueFrameCapture(self): - """ - Queue frames that may be filled during frame capturing. - """ - errorCode = VimbaDLL.captureFrameQueue(self._handle, - byref(self._frame), - None) # callback not implemented, callback example in pico? - if errorCode != 0: - raise VimbaException(errorCode) - - def waitFrameCapture(self, timeout = 2000): - """ - Wait for a queued frame to be filled (or dequeued). - """ - errorCode = VimbaDLL.captureFrameWait(self._handle, - byref(self._frame), - timeout) - if errorCode != 0: - raise VimbaException(errorCode) - - # custom method for simplified usage - def getBufferByteData(self): - """ - Retrieve buffer data in a useful format. - - :returns: array -- buffer data. - """ - - # cast frame buffer memory contents to a usable type - data = cast(self._frame.buffer, - POINTER(c_ubyte * self.payloadSize)) - - # make array of c_ubytes from buffer - array = (c_ubyte * self.height * self.width).from_address(addressof(data.contents)) - - return array diff --git a/pyvimba/vimbainterface.py b/pyvimba/vimbainterface.py deleted file mode 100644 index 03a5f80..0000000 --- a/pyvimba/vimbainterface.py +++ /dev/null @@ -1,44 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbaobject import VimbaObject -from vimbaexception import VimbaException -from vimbadll import VimbaDLL -from ctypes import * - -# interface features are automatically readable as object attributes. -class VimbaInterface(VimbaObject): - """ - A Vimba interface object. This class provides the minimal access - to Vimba functions required to control the interface. - """ - - @property - def interfaceIdString(self): - return self._interfaceIdString - - # own handle is inherited as self._handle - def __init__(self, interfaceIdString): - - # call super constructor - super(VimbaInterface, self).__init__() - - # set ID - self._interfaceIdString = interfaceIdString - - def openInterface(self): - """ - Open the interface. - """ - errorCode = VimbaDLL.interfaceOpen(self._interfaceIdString, - byref(self._handle)) - if errorCode != 0: - raise VimbaException(errorCode) - - def closeInterface(self): - """ - Close the interface. - """ - errorCode = VimbaDLL.interfaceClose(self._handle) - if errorCode != 0: - raise VimbaException(errorCode) - \ No newline at end of file diff --git a/pyvimba/vimbaobject.py b/pyvimba/vimbaobject.py deleted file mode 100644 index 8e2f812..0000000 --- a/pyvimba/vimbaobject.py +++ /dev/null @@ -1,213 +0,0 @@ -# -*- coding: utf-8 -*- -import vimbastructure as structs -from vimbaexception import VimbaException -from vimbafeature import VimbaFeature -from vimbadll import VimbaDLL -from ctypes import * - -class VimbaObject(object): - """ - A Vimba object has a handle and features associated with it. - Objects include System, Camera, Interface and AncillaryData. - """ - - @property - def handle(self): - return self._handle - - def __init__(self): - # create own handle - self._handle = c_void_p() - - # list of VimbaFeatureInfo objects - # can't set yet as the object (e.g. a camera) won't be - # opened yet, therefore no event for object opening - # so will have it populate by user interaction - # and blame them if the object is not opened then - self._featureInfos = None - - # override getattr for undefined attributes - def __getattr__(self, attr): - - # if a feature value requested (requires object (camera) open) - if attr in self.getFeatureNames(): - return VimbaFeature(attr, self._handle).value - - # otherwise don't know about it - raise AttributeError(''.join(["'VimbaObject' has no attribute '", - attr, - "'"])) - - # override setattr for undefined attributes - def __setattr__(self, attr, val): - - # set privates as normal - # check this first to allow all privates to set normally - # and avoid recursion errors - if attr.startswith('_'): - super(VimbaObject, self).__setattr__(attr, val) - - # if it's an actual camera feature (requires camera open) - elif attr in self.getFeatureNames(): - VimbaFeature(attr, self._handle).value = val - - # otherwise just set the attribute value as normal - else: - super(VimbaObject, self).__setattr__(attr, val) - - def _getFeatureInfos(self): - """ - Gets feature info of all available features. Will - cause error if object/camera is not opened. - - :returns: list -- feature info for available features. - """ - # check it's populated as can't populate it in __init__ - if self._featureInfos is None: - # args - dummyFeatureInfo = structs.VimbaFeatureInfo() - numFound = c_uint32(-1) - - # call once to get number of available features - # Vimba DLL will return an error code - errorCode = VimbaDLL.featuresList(self._handle, - byref(dummyFeatureInfo), - 0, - byref(numFound), - sizeof(dummyFeatureInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - - # number of features specified by Vimba - numFeatures = numFound.value - - # args - featureInfoArray = (structs.VimbaFeatureInfo * numFeatures)() - - # call again to get the features - # Vimba DLL will return an error code - errorCode = VimbaDLL.featuresList(self._handle, - featureInfoArray, - numFeatures, - byref(numFound), - sizeof(dummyFeatureInfo)) - if errorCode != 0: - raise VimbaException(errorCode) - - self._featureInfos = list(featInfo for featInfo in featureInfoArray) - return self._featureInfos - - def getFeatureNames(self): - """ - Get names of all available features. - - :returns: list -- feature names for available features. - """ - return list(featInfo.name for featInfo in self._getFeatureInfos()) - - def getFeatureInfo(self, featureName): - """ - Gets feature info object of specified feature. - - :param featureName: the name of the feature. - - :returns: VimbaFeatureInfo object -- the feature info object specified. - """ - # don't do this live as we already have this info - # return info object, if it exists - for featInfo in self._getFeatureInfos(): - if featInfo.name == featureName: - return featInfo - # otherwise raise error - raise VimbaException(-53) - - # don't think we ever need to return a feature object... - #def getFeature(self, featureName): - - def getFeatureRange(self, featureName): - """ - Get valid range of feature values. - - :param featureName: name of the feature to query. - - :returns: tuple -- range as (feature min value, feature max value). - """ - # can't cache this, need to look it up live - return VimbaFeature(featureName, self._handle).range - - def runFeatureCommand(self, featureName): - """ - Run a feature command. - - :param featureName: the name of the feature. - """ - # run a command - errorCode = VimbaDLL.featureCommandRun(self._handle, - featureName) - if errorCode != 0: - raise VimbaException(errorCode) - - def readRegister(self, address): - # note that the underlying Vimba function allows reading of an array - # of registers, but only one address/value at a time is implemented here - """ - Read from a register of the module (camera). - - :param address: the address of the register to read. - - :returns: int -- value of register. - """ - readCount = 1 - - # check address validity - try: - regAddress = c_uint64(int(address, 16)) - except: - raise VimbaException(-52) - - regData = c_uint64() - numCompleteReads = c_uint32() - - errorCode = VimbaDLL.registersRead(self.handle, - readCount, - byref(regAddress), - byref(regData), - byref(numCompleteReads)) - - if errorCode != 0: - raise VimbaException(errorCode) - - return regData.value - - def writeRegister(self, address, value): - # note that the underlying Vimba function allows writing of an array - # of registers, but only one address/value at a time is implemented here - """ - Read from a register of the module (camera). - - :param address: the address of the register to read. - :param value: the value to set in hex. - """ - writeCount = 1 - - # check address validity - try: - regAddress = c_uint64(int(address, 16)) - except: - raise VimbaException(-52) - - # check value validity - try: - regData = c_uint64(int(value, 16)) - except: - raise VimbaException(-52) - - numCompleteWrites = c_uint32() - - errorCode = VimbaDLL.registersWrite(self.handle, - writeCount, - byref(regAddress), - byref(regData), - byref(numCompleteWrites)) - if errorCode != 0: - raise VimbaException(errorCode) diff --git a/pyvimba/vimbastructure.py b/pyvimba/vimbastructure.py deleted file mode 100644 index 7b1e65c..0000000 --- a/pyvimba/vimbastructure.py +++ /dev/null @@ -1,99 +0,0 @@ -# -*- coding: utf-8 -*- -from ctypes import * - - -class VimbaVersion(Structure): - _fields_ = [('major', c_uint32), - ('minor', c_uint32), - ('patch', c_uint32)] - - -class VimbaInterfaceInfo(Structure): - _fields_ = [('interfaceIdString', c_char_p), # Unique identifier for each interface - ('interfaceType', c_uint32), # Interface type, see VmbInterfaceType - ('interfaceName', c_char_p), # Interface name, given by the transport layer - ('serialString', c_char_p), # Serial number - ('permittedAccess', c_uint32)] # Used access mode, see VmbAccessModeType - - def getFieldNames(self): - """ - Get field names. - """ - return [field[0] for field in self._fields_] - - -class VimbaCameraInfo(Structure): - _fields_ = [('cameraIdString', c_char_p), # Unique identifier for each camera - ('cameraName', c_char_p), # Name of the camera - ('modelName', c_char_p), # Model name - ('serialString', c_char_p), # Serial number - ('permittedAccess', c_uint32), # Used access mode, see VmbAccessModeType - ('interfaceIdString', c_char_p)] # Unique value for each interface or bus - - def getFieldNames(self): - """ - Get field names. - """ - return [field[0] for field in self._fields_] - - -class VimbaFeatureInfo(Structure): - - _fields_ = [('name', c_char_p), - ('featureDataType', c_uint32), - ('featureFlags', c_uint32), - ('category', c_char_p), - ('displayName', c_char_p), - ('pollingTime', c_uint32), - ('unit', c_char_p), - ('representation', c_char_p), - ('visibility', c_uint32), - ('tooltip', c_char_p), - ('description', c_char_p), - ('sfncNamespace', c_char_p), - ('isStreamable', c_bool), - ('hasAffectedFeatures', c_bool), - ('hasSelectedFeatures', c_bool)] - - def getFieldNames(self): - """ - Get field names. - """ - return [field[0] for field in self._fields_] - - -class VimbaFrame(Structure): - - # IN - _fields_ = [('buffer', c_void_p), # Comprises image and ancillary data - ('bufferSize', c_uint32), # Size of the data buffer - - ('context', c_void_p * 4), # User context filled during queuing - - # OUT - ('receiveStatus', c_int32), # Resulting status of the receive operation - ('receiveFlags', c_uint32), # Resulting flags of the receive operation - - ('imageSize', c_uint32), # Size of the image data inside the data buffer - ('ancillarySize', c_uint32), # Size of the ancillary data inside the data buffer - - ('pixelFormat', c_uint32), # Pixel format of the image - - ('width', c_uint32), # Width of an image - ('height', c_uint32), # Height of an image - ('offsetX', c_uint32), # Horizontal offset of an image - ('offsetY', c_uint32), # Vertical offset of an image - - ('frameID', c_uint64), # Unique ID of this frame in this stream - ('timestamp', c_uint64)] # Timestamp of the data transfer - - def getFieldNames(self): - """ - Get field names. - """ - return [field[0] for field in self._fields_] - - - - - diff --git a/pyvimba/vimbasystem.py b/pyvimba/vimbasystem.py deleted file mode 100644 index 1e04755..0000000 --- a/pyvimba/vimbasystem.py +++ /dev/null @@ -1,19 +0,0 @@ -# -*- coding: utf-8 -*- -from vimbaobject import VimbaObject -from ctypes import * - -# system features are automatically readable as attributes. -class VimbaSystem(VimbaObject): - """ - A Vimba system object. This class provides the minimal access - to Vimba functions required to control the system. - """ - - # own handle is inherited as self._handle - def __init__(self): - - # call super constructor - super(VimbaSystem, self).__init__() - - # set own handle manually - self._handle = c_void_p(1) diff --git a/setup.py b/setup.py index b25f47f..8875240 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,10 @@ from setuptools import setup, find_packages import os, sys, glob, fnmatch -setup(name="pyvimba", +setup(name="pymba", version=0.1, - description="pyvimba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API.", - long_description="""pyvimba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API. It wraps the VimbaC.dll file included in the AVT Vimba installation to provide a simple Python interface for AVT cameras. It currently supports most of the functionality provided by VimbaC.dll.""", + description="pymba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API.", + long_description="""pymba is a Python wrapper for the Allied Vision Technologies (AVT) Vimba C API. It wraps the VimbaC.dll file included in the AVT Vimba installation to provide a simple Python interface for AVT cameras. It currently supports most of the functionality provided by VimbaC.dll.""", classifiers=[ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', @@ -22,7 +22,7 @@ setup(name="pyvimba", author_email='support@sightmachine.com', url='http://simplecv.org', license='BSD', - packages = ['pyvimba', 'pyvimba.tests'], + packages = ['pymba', 'pymba.tests'], zip_safe = False, requires=['cv2','cv', 'numpy', 'scipy', 'pygame', 'pil', 'svgwrite'], -- cgit v1.2.3