aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormorefigs <morefigs@gmail.com>2015-01-29 14:57:55 +1100
committermorefigs <morefigs@gmail.com>2015-01-29 14:57:55 +1100
commit2ebdf4b27a6feb13f351f40cafc4a797560b28b2 (patch)
tree3fd15599fa42b8fb72bad83df54c3b8c7f743254
parent1cede4b2d22656991adb46adde8050b091faa1f3 (diff)
parent6e75392943a2a40984e609f387145ded8d7c1b53 (diff)
downloadpymba-2ebdf4b27a6feb13f351f40cafc4a797560b28b2.tar.gz
pymba-2ebdf4b27a6feb13f351f40cafc4a797560b28b2.zip
Merge pull request #11 from fmder/vimba-context
Vimba context manager fixing issue #10
-rw-r--r--README.md188
-rw-r--r--README.txt187
-rw-r--r--pymba/tests/opencv_example.py116
-rw-r--r--pymba/tests/opencv_liveview_example.py120
-rw-r--r--pymba/tests/opencv_liveview_example_color.py119
-rw-r--r--pymba/tests/test_cameras.py123
-rw-r--r--pymba/tests/test_installation.py6
-rw-r--r--pymba/tests/test_interfaces.py39
-rw-r--r--pymba/tests/test_systemfeature.py20
-rw-r--r--pymba/vimba.py21
10 files changed, 456 insertions, 483 deletions
diff --git a/README.md b/README.md
index 5e73596..4a8c484 100644
--- a/README.md
+++ b/README.md
@@ -17,9 +17,9 @@ Install pymba.
If Vimba and pymba are installed correctly, then the following code should give the installed Vimba version. No camera is needed.
from pymba import *
-
- vimba = Vimba()
- print vimba.getVersion()
+
+ with Vimba() as vimba:
+ print vimba.getVersion()
### Interacting with cameras
@@ -29,69 +29,63 @@ Discover, open, manipulate, and capture frames from a camera.
import time
# 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()
+ with Vimba() as vimba:
+ # 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
### Interacting with the Vimba system
@@ -99,18 +93,13 @@ Get a reference to the Vimba system object and list available system features.
from pymba import *
- vimba = Vimba()
- vimba.startup()
-
- # get system object
- system = vimba.getSystem()
-
- # list system features
- for featureName in system.getFeatureNames():
- print 'System feature:', featureName
+ with Vimba() as vimba:
+ # get system object
+ system = vimba.getSystem()
- # shutdown Vimba
- vimba.shutdown()
+ # list system features
+ for featureName in system.getFeatureNames():
+ print 'System feature:', featureName
### Interacting with transport layer interfaces
@@ -118,39 +107,34 @@ Get a reference to an interface object and list available interface features.
from pymba import *
- 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()
+ with Vimba() as vimba:
+ # 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()
### Handling Vimba exceptions
from pymba import *
- vimba = Vimba()
try:
- camera0 = vimba.getCamera(123)
- except VimbaException as ve:
- print ve.message
+ with Vimba() as vimba:
+ except VimbaException as e:
+ print e.message
diff --git a/README.txt b/README.txt
index f7f9dda..733ba7c 100644
--- a/README.txt
+++ b/README.txt
@@ -21,135 +21,128 @@ Testing installation
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 *
+ from pymba import *
- vimba = Vimba()
- print vimba.getVersion()
+ with Vimba() as vimba:
+ print vimba.getVersion()
Interacting with cameras
------------------------
Discover, open, manipulate, and capture frames from a camera.
- from vimba import *
+ from pymba import *
import time
# 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()
+ with Vimba() as vimba:
+ # 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
Interacting with the Vimba system
---------------------------------
Get a reference to the Vimba system object and list available system features.
- from vimba import *
+ from pymba import *
- # get system object
- system = vimba.getSystem()
-
- # list system features
- for featureName in system.getFeatureNames():
- print 'System feature:', featureName
+ with Vimba() as vimba:
+ # get system object
+ system = vimba.getSystem()
+
+ # list system features
+ for featureName in system.getFeatureNames():
+ print 'System feature:', featureName
- # shutdown Vimba
- vimba.shutdown()
Interacting with transport layer interfaces
-------------------------------------------
Get a reference to an interface object and list available interface features.
- from vimba import *
-
- # 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()
+ from pymba import *
- # list interface features
- interfaceFeatureNames = interface0.getFeatureNames()
- for name in interfaceFeatureNames:
- print 'Interface feature:', name
-
- # close interface
- interface0.closeInterface()
+ with Vimba() as vimba:
+ # 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()
Handling Vimba exceptions
-------------------------
- from vimba import *
+ from pymba import *
try:
- vimba = Vimba()
- vimba.startup()
+ with Vimba() as vimba:
except VimbaException as e:
- print e.message
+ print e.message
Known issues
============
diff --git a/pymba/tests/opencv_example.py b/pymba/tests/opencv_example.py
index 1b80e05..e89cccc 100644
--- a/pymba/tests/opencv_example.py
+++ b/pymba/tests/opencv_example.py
@@ -6,74 +6,70 @@ import time
#very crude example, assumes your camera is PixelMode = BAYERRG8
# start Vimba
-vimba = Vimba()
-vimba.startup()
+with Vimba() as vimba:
+ # get system object
+ system = vimba.getSystem()
-# 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
-# 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()
-# 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
-# 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)
-# 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
-# get the value of a feature
-print camera0.AcquisitionMode
+ # set the value of a feature
+ camera0.AcquisitionMode = 'SingleFrame'
-# 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
-# create new frames for the camera
-frame0 = camera0.getFrame() # creates a frame
-frame1 = camera0.getFrame() # creates a second frame
+ # announce frame
+ frame0.announceFrame()
-# 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()
-# 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()
-# close camera
-camera0.closeCamera()
-
-# shutdown Vimba
-vimba.shutdown()
diff --git a/pymba/tests/opencv_liveview_example.py b/pymba/tests/opencv_liveview_example.py
index 415f892..e6fc89f 100644
--- a/pymba/tests/opencv_liveview_example.py
+++ b/pymba/tests/opencv_liveview_example.py
@@ -12,71 +12,67 @@ import time
cv2.namedWindow("test")
-vimba = Vimba()
-vimba.startup()
+with Vimba() as vimba:
+ system = vimba.getSystem()
-system = vimba.getSystem()
+ system.runFeatureCommand("GeVDiscoveryAllOnce")
+ time.sleep(0.2)
-system.runFeatureCommand("GeVDiscoveryAllOnce")
-time.sleep(0.2)
+ camera_ids = vimba.getCameraIds()
-camera_ids = vimba.getCameraIds()
+ for cam_id in camera_ids:
+ print "Camera found: ", cam_id
+
+ c0 = vimba.getCamera(camera_ids[0])
+ c0.openCamera()
-for cam_id in camera_ids:
- print "Camera found: ", cam_id
-
-c0 = vimba.getCamera(camera_ids[0])
-c0.openCamera()
-
-try:
- #gigE camera
- print c0.GevSCPSPacketSize
- print c0.StreamBytesPerSecond
- c0.StreamBytesPerSecond = 100000000
-except:
- #not a gigE camera
- pass
-
-#set pixel format
-c0.PixelFormat="Mono8"
-#c0.ExposureTimeAbs=60000
-
-frame = c0.getFrame()
-frame.announceFrame()
-
-c0.startCapture()
-
-framecount = 0
-droppedframes = []
-
-while 1:
try:
- frame.queueFrameCapture()
- success = True
+ #gigE camera
+ print c0.GevSCPSPacketSize
+ print c0.StreamBytesPerSecond
+ c0.StreamBytesPerSecond = 100000000
except:
- droppedframes.append(framecount)
- success = False
- c0.runFeatureCommand("AcquisitionStart")
- c0.runFeatureCommand("AcquisitionStop")
- frame.waitFrameCapture(1000)
- frame_data = frame.getBufferByteData()
- if success:
- img = np.ndarray(buffer=frame_data,
- dtype=np.uint8,
- shape=(frame.height,frame.width,1))
- cv2.imshow("test",img)
- framecount+=1
- k = cv2.waitKey(1)
- if k == 0x1b:
- cv2.destroyAllWindows()
- print "Frames displayed: %i"%framecount
- print "Frames dropped: %s"%droppedframes
- break
-
-
-c0.endCapture()
-c0.revokeAllFrames()
-
-c0.closeCamera()
-
-vimba.shutdown()
+ #not a gigE camera
+ pass
+
+ #set pixel format
+ c0.PixelFormat="Mono8"
+ #c0.ExposureTimeAbs=60000
+
+ frame = c0.getFrame()
+ frame.announceFrame()
+
+ c0.startCapture()
+
+ framecount = 0
+ droppedframes = []
+
+ while 1:
+ try:
+ frame.queueFrameCapture()
+ success = True
+ except:
+ droppedframes.append(framecount)
+ success = False
+ c0.runFeatureCommand("AcquisitionStart")
+ c0.runFeatureCommand("AcquisitionStop")
+ frame.waitFrameCapture(1000)
+ frame_data = frame.getBufferByteData()
+ if success:
+ img = np.ndarray(buffer=frame_data,
+ dtype=np.uint8,
+ shape=(frame.height,frame.width,1))
+ cv2.imshow("test",img)
+ framecount+=1
+ k = cv2.waitKey(1)
+ if k == 0x1b:
+ cv2.destroyAllWindows()
+ print "Frames displayed: %i"%framecount
+ print "Frames dropped: %s"%droppedframes
+ break
+
+
+ c0.endCapture()
+ c0.revokeAllFrames()
+
+ c0.closeCamera()
diff --git a/pymba/tests/opencv_liveview_example_color.py b/pymba/tests/opencv_liveview_example_color.py
index a13d56c..5b531fe 100644
--- a/pymba/tests/opencv_liveview_example_color.py
+++ b/pymba/tests/opencv_liveview_example_color.py
@@ -22,71 +22,68 @@ import sys
cv2.namedWindow("test")
-vimba = Vimba()
-vimba.startup()
+with Vimba() as vimba:
+ system = vimba.getSystem()
-system = vimba.getSystem()
+ system.runFeatureCommand("GeVDiscoveryAllOnce")
+ time.sleep(0.2)
-system.runFeatureCommand("GeVDiscoveryAllOnce")
-time.sleep(0.2)
+ camera_ids = vimba.getCameraIds()
-camera_ids = vimba.getCameraIds()
+ for cam_id in camera_ids:
+ print("Camera found: ", cam_id)
-for cam_id in camera_ids:
- print("Camera found: ", cam_id)
+ c0 = vimba.getCamera(camera_ids[0])
+ c0.openCamera()
-c0 = vimba.getCamera(camera_ids[0])
-c0.openCamera()
-
-try:
- #gigE camera
- print("Packet size:", c0.GevSCPSPacketSize)
- c0.StreamBytesPerSecond = 100000000
- print("BPS:", c0.StreamBytesPerSecond)
-except:
- #not a gigE camera
- pass
-
-#set pixel format
-c0.PixelFormat = "BGR8Packed" # OPENCV DEFAULT
-time.sleep(0.2)
-
-frame = c0.getFrame()
-frame.announceFrame()
-
-c0.startCapture()
-
-framecount = 0
-droppedframes = []
-
-while 1:
try:
- frame.queueFrameCapture()
- success = True
+ #gigE camera
+ print("Packet size:", c0.GevSCPSPacketSize)
+ c0.StreamBytesPerSecond = 100000000
+ print("BPS:", c0.StreamBytesPerSecond)
except:
- droppedframes.append(framecount)
- success = False
- c0.runFeatureCommand("AcquisitionStart")
- c0.runFeatureCommand("AcquisitionStop")
- frame.waitFrameCapture(1000)
- frame_data = frame.getBufferByteData()
- if success:
- img = np.ndarray(buffer=frame_data,
- dtype=np.uint8,
- shape=(frame.height, frame.width, frame.pixel_bytes))
- cv2.imshow("test", img)
- framecount += 1
- k = cv2.waitKey(1)
- if k == 0x1b:
- cv2.destroyAllWindows()
- print("Frames displayed: %i" % framecount)
- print("Frames dropped: %s" % droppedframes)
- break
-
-
-c0.endCapture()
-c0.revokeAllFrames()
-
-c0.closeCamera()
-
-vimba.shutdown()
+ #not a gigE camera
+ pass
+
+ #set pixel format
+ c0.PixelFormat = "BGR8Packed" # OPENCV DEFAULT
+ time.sleep(0.2)
+
+ frame = c0.getFrame()
+ frame.announceFrame()
+
+ c0.startCapture()
+
+ framecount = 0
+ droppedframes = []
+
+ while 1:
+ try:
+ frame.queueFrameCapture()
+ success = True
+ except:
+ droppedframes.append(framecount)
+ success = False
+ c0.runFeatureCommand("AcquisitionStart")
+ c0.runFeatureCommand("AcquisitionStop")
+ frame.waitFrameCapture(1000)
+ frame_data = frame.getBufferByteData()
+ if success:
+ img = np.ndarray(buffer=frame_data,
+ dtype=np.uint8,
+ shape=(frame.height, frame.width, frame.pixel_bytes))
+ cv2.imshow("test", img)
+ framecount += 1
+ k = cv2.waitKey(1)
+ if k == 0x1b:
+ cv2.destroyAllWindows()
+ print("Frames displayed: %i" % framecount)
+ print("Frames dropped: %s" % droppedframes)
+ break
+
+
+ c0.endCapture()
+ c0.revokeAllFrames()
+
+ c0.closeCamera()
+
diff --git a/pymba/tests/test_cameras.py b/pymba/tests/test_cameras.py
index e51a042..3699e93 100644
--- a/pymba/tests/test_cameras.py
+++ b/pymba/tests/test_cameras.py
@@ -6,67 +6,62 @@ 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()
+ with Vimba() as vimba:
+ # 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()
diff --git a/pymba/tests/test_installation.py b/pymba/tests/test_installation.py
index 3b6b90d..775c529 100644
--- a/pymba/tests/test_installation.py
+++ b/pymba/tests/test_installation.py
@@ -4,6 +4,6 @@ from pymba import Vimba
def test_installation():
- vimba = Vimba()
- version = vimba.getVersion()
- assert version == '1.2.0'
+ with Vimba() as vimba:
+ version = vimba.getVersion()
+ assert version == '1.2.0'
diff --git a/pymba/tests/test_interfaces.py b/pymba/tests/test_interfaces.py
index 63deceb..4014690 100644
--- a/pymba/tests/test_interfaces.py
+++ b/pymba/tests/test_interfaces.py
@@ -6,25 +6,20 @@ 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()
+ with Vimba() as vimba:
+ # 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() \ No newline at end of file
diff --git a/pymba/tests/test_systemfeature.py b/pymba/tests/test_systemfeature.py
index 3c63b71..e5168d5 100644
--- a/pymba/tests/test_systemfeature.py
+++ b/pymba/tests/test_systemfeature.py
@@ -5,18 +5,14 @@ from pymba import *
def test_systemfeature():
# get system object
- vimba = Vimba()
- vimba.startup()
+ with Vimba() as vimba:
+ system = vimba.getSystem()
- 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)
- # 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
index e301aa2..97eb96e 100644
--- a/pymba/vimba.py
+++ b/pymba/vimba.py
@@ -35,6 +35,27 @@ class Vimba(object):
# forget them
self._interfaces = {}
+ def __enter__(self):
+ """
+ Define vimba context for safe execution.
+
+ The vimba object should be used like this:
+ # start Vimba
+ with Vimba() as vimba:
+ system = vimba.getSystem()
+ # ...
+ """
+ self.startup()
+ return self
+
+ def __exit__(self, type, value, traceback):
+ """
+ Shutdown Vimba when the with context is left. This allows cleanup
+ when an error occurs in the main program. The system will not hang
+ on a kernel call after an exception.
+ """
+ self.shutdown()
+
def _getInterfaceInfos(self):
"""
Gets interface info of all available interfaces.