GrabCallback¶
GrabCallback.py¶
from itala import itala
import threading
import time
INDENT = "\t"
# Grab images in a polling loop (until the "done" flag is False). Then call the callback function
# provided by the user. The "done" flag is passed by reference. Don't forget to catch exceptions,
# otherwise the thread will terminate. This function is meant to be ran in a dedicated thread.
def acquisition_loop(callback, device, done):
while not done[0]:
try:
image = device.get_next_image(1000)
callback(image)
except Exception as e:
error_msg = str(e)
if "timeout" in error_msg.lower():
print(INDENT + "Timeout occurred!")
else:
print(INDENT + "\r" + error_msg)
break
# The actual user callback, called when an image is available.
def grab_callback(image):
# Simulate some image processing and then dispose the image
time.sleep(0.01) # 10 milliseconds
print(INDENT + "Image with ID:" + str(image.frame_id) + " processed.")
image.dispose()
def main():
print("***** GrabCallback example started. *****")
print()
system = itala.create_system()
devices_info = system.enumerate_devices(700)
if len(devices_info) == 0:
print("No devices found. Example canceled.")
exit(1)
if devices_info[0].access_status != itala.DeviceAccessStatus_AvailableReadWrite:
print("Target device is unaccessible in RW mode. Example canceled.")
exit(1)
device = system.create_device(devices_info[0])
print("First device initialized.")
device.start_acquisition()
print("Acquisition started.")
print()
# Flag passed to the thread to stop grabbing images
# Using a list to allow mutation in the thread (mutable container)
done = [False]
# Start the acquisition thread
grabber = threading.Thread(target=acquisition_loop, args=(grab_callback, device, done))
grabber.start()
# Wait for user input to set the "done" flag to True and stop the acquisition
print("Press Enter to stop acquisition..")
print()
input()
done[0] = True
grabber.join()
device.stop_acquisition()
print()
print("Acquisition stopped.")
print()
device.dispose()
print("Device instance disposed.")
system.dispose()
print("System instance disposed.")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(str(e))