Created
March 3, 2016 14:50
-
-
Save jrast/eeda7458d8216d9dd73f to your computer and use it in GitHub Desktop.
Async Image grab with pymba and Vimba 1.4, not working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from pymba import * | |
import time, Queue | |
import numpy as np | |
def frame_cb(frame): | |
print("Start Callback {} with Frame {}".format(str(frame_cb), str(frame))) | |
img = np.ndarray(buffer=frame.getBufferByteData(), | |
dtype=np.uint8, | |
shape=(frame.height, frame.width)) | |
frame.queueFrameCapture(frame_cb) | |
print("End Callback {} with Frame {}".format(str(frame_cb), str(frame))) | |
def test_frame_callback(): | |
# 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() | |
# get and open a camera | |
camera0 = vimba.getCamera(cameraIds[0]) | |
camera0.openCamera() | |
# set the value of a feature | |
camera0.AcquisitionMode = 'Continuous' | |
# get ready to capture | |
camera0.startCapture() | |
# create new frames for the camera | |
frames = [camera0.getFrame() for _ in xrange(2)] | |
for frame in frames: | |
print("Created Frame {}, Struct {}".format(str(frame), str(frame._frame))) | |
# announce and queue frames | |
for frame in frames: | |
frame.announceFrame() | |
frame.queueFrameCapture(frameCallback=frame_cb) | |
# capture some images | |
run_duration = 10.0 # seconds | |
camera0.runFeatureCommand('AcquisitionStart') | |
time.sleep(run_duration) | |
camera0.runFeatureCommand('AcquisitionStop') | |
time.sleep(0.5) | |
# clean up after capture | |
camera0.endCapture() | |
camera0.revokeAllFrames() | |
# close camera | |
camera0.closeCamera() | |
# shutdown Vimba | |
vimba.shutdown() | |
test_frame_callback() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADA30>, Struct <pymba.vimbastructure.VimbaFrame object at 0x02BC8A80> | |
Created Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADBB0>, Struct <pymba.vimbastructure.VimbaFrame object at 0x02BC8C10> | |
Qeueing Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADA30> with callback <function frame_cb at 0x024DC1F0> | |
Qeueing Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADBB0> with callback <function frame_cb at 0x024DC1F0> | |
Start Callback Wrapper for Cam 5923, Frame <pymba.vimbadll.LP_VimbaFrame object at 0x02BC8C60>, Callback <function frame_cb at 0x024DC1F0> | |
Start Callback <function frame_cb at 0x024DC1F0> with Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADA30> | |
Qeueing Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADA30> with callback <function frame_cb at 0x024DC1F0> | |
End Callback <function frame_cb at 0x024DC1F0> with Frame <pymba.vimbaframe.VimbaFrame object at 0x02BADA30> | |
End Callback Wrapper for Cam 5923, Frame <pymba.vimbadll.LP_VimbaFrame object at 0x02BC8C60>, Callback <function frame_cb at 0x024DC1F0> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# callback for frame queue, note the new parameter | |
frameDoneCallback = CFUNCTYPE(c_void_p, # return type | |
c_void_p, # camera handle | |
POINTER(structs.VimbaFrame)) # pointer to frame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The changed queueFrameCapture() function of the VimbaFrame Class | |
def queueFrameCapture(self, frameCallback = None): | |
""" | |
Queue frames that may be filled during frame capturing. | |
Runs VmbCaptureFrameQueue | |
Call after announceFrame and startCapture | |
Callback must accept argument of type frame. Remember to requeue the | |
frame by calling frame.queueFrameCapture(frameCallback) at the end of | |
your callback function. | |
""" | |
# remember the given callback function | |
print("Qeueing Frame {} with callback {}".format(str(self), str(frameCallback))) | |
self._frameCallback = frameCallback | |
# define a callback wrapper here so it doesn't bind self | |
def frameCallbackWrapper(p_cam, p_frame): | |
# call the user's callback with the self bound outside the wrapper | |
# ignore the frame pointer since we already know the callback | |
# refers to this frame | |
print("Start Callback Wrapper for Cam {}, Frame {}, Callback {}".format(str(p_cam), str(p_frame), str(self._frameCallback))) | |
self._frameCallback(self) | |
print("End Callback Wrapper for Cam {}, Frame {}, Callback {}".format(str(p_cam), str(p_frame), str(self._frameCallback))) | |
if self._frameCallback is None: | |
self._frameCallbackWrapper_C = None | |
else: | |
# keep a reference to prevent gc issues | |
self._frameCallbackWrapper_C = VimbaDLL.frameDoneCallback(frameCallbackWrapper) | |
errorCode = VimbaDLL.captureFrameQueue(self._handle, | |
byref(self._frame), | |
self._frameCallbackWrapper_C) | |
if errorCode != 0: | |
raise VimbaException(errorCode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment