Impact Acquire SDK Python
Loading...
Searching...
No Matches
GenICamCallbackOnEvent.py

The GenICamCallbackOnEvent program is a simple example which illustrates how GenICam™ events can be used to inform an application about a certain event via a callback.

How it works:
  1. Open the device by calling
    pDev.open()
  2. Enable GenICam™ events.
  3. Attach a custom callback to the ExposureEndTimestamp property that gets called whenever a property is modified.
  4. Start the image acquisition in order to cause the callbacks to get triggered.

The full explanation regarding the callback usage can be found at the chapter Callbacks Triggered By GenICam™ Events .

Source code
import os, platform, sys
# import all the stuff from our SDK into the current scope
from mvIMPACT import acquire
# import all the helper functions for the examples using our SDK such as 'conditionalSetProperty' into the current scope
# If you want to use this module in your code feel free to do so but make sure the 'Common' folder resides in a sub-folder of your project then
from mvIMPACT.Common import exampleHelper
#------------------------------------------------------------------
# Event Callback implementation
#------------------------------------------------------------------
class EventCallback(acquire.ComponentCallback):
def __init__(self, pUserData):
acquire.ComponentCallback.__init__(self)
self.pUserData_ = pUserData
def execute(self, c, pUserData):
try:
# re-generating the object/data previously attached to the callback object. This could now be used to call a certain member function e.g. to update a class instance about this event!
ec = self.pUserData_
if( c.isProp ):
p = acquire.Property(c.hObj())
print("Component " + c.name() + " has changed. Its current value: " + p.readS() + "us. FrameID is: " + ec.eventExposureEndFrameID.readS())
except Exception as e:
print("An exception has been raised by code that is not supposed to raise one: '" + str(e) + "'! If this is NOT handled here the application will crash as this Python exception instance will be returned back into the native code that fired the callback!")
devMgr = acquire.DeviceManager()
pDev = exampleHelper.getDeviceFromUserInput(devMgr)
if pDev == None:
exampleHelper.requestENTERFromUser()
sys.exit(-1)
pDev.open()
print("Please enter the number of buffers to capture followed by [ENTER]: ", end='')
framesToCapture = exampleHelper.getNumberFromUser()
if framesToCapture < 1:
print("Invalid input! Please capture at least one image")
sys.exit(-1)
# The mvDisplay library is only available on Windows systems for now
isDisplayModuleAvailable = platform.system() == "Windows"
if isDisplayModuleAvailable:
display = acquire.ImageDisplayWindow("A window created from Python")
else:
print("The display library of this SDK is not available on this('" + platform.system() + "') system. Consider using the PIL(Python Image Library) and numpy(Numerical Python) packages instead. Have a look at the source code of the ContinuousCapture example to get an idea how.")
fi = acquire.FunctionInterface(pDev)
while fi.imageRequestSingle() == acquire.DMR_NO_ERROR:
print("Buffer queued")
pPreviousRequest = None
# enable GenICam events
ec = acquire.EventControl(pDev)
ec.eventSelector.writeS("ExposureEnd")
ec.eventNotification.writeS("On")
# register a callback to eventExposureEndTimestamp
eventCallback = EventCallback(ec)
eventCallback.registerComponent(ec.eventExposureEndTimestamp)
exampleHelper.manuallyStartAcquisitionIfNeeded(pDev, fi)
for i in range(framesToCapture):
requestNr = fi.imageRequestWaitFor(10000)
if fi.isRequestNrValid(requestNr):
pRequest = fi.getRequest(requestNr)
if pRequest.isOK:
if isDisplayModuleAvailable:
display.GetImageDisplay().SetImage(pRequest)
display.GetImageDisplay().Update()
if pPreviousRequest != None:
pPreviousRequest.unlock()
pPreviousRequest = pRequest
fi.imageRequestSingle()
else:
# Please note that slow systems or interface technologies in combination with high resolution sensors
# might need more time to transmit an image than the timeout value which has been passed to imageRequestWaitFor().
# If this is the case simply wait multiple times OR increase the timeout(not recommended as usually not necessary
# and potentially makes the capture thread less responsive) and rebuild this application.
# Once the device is configured for triggered image acquisition and the timeout elapsed before
# the device has been triggered this might happen as well.
# The return code would be -2119(DEV_WAIT_FOR_REQUEST_FAILED) in that case, the documentation will provide
# additional information under TDMR_ERROR in the interface reference.
# If waiting with an infinite timeout(-1) it will be necessary to call 'imageRequestReset' from another thread
# to force 'imageRequestWaitFor' to return when no data is coming from the device/can be captured.
print("imageRequestWaitFor failed (" + str(requestNr) + ", " + acquire.ImpactAcquireException.getErrorCodeAsString(requestNr) + ")")
exampleHelper.manuallyStopAcquisitionIfNeeded(pDev, fi)
eventCallback.unregisterComponent( ec.eventExposureEndTimestamp)
exampleHelper.requestENTERFromUser()
Definition Common/__init__.py:1