GrabChunks#

GrabChunks.py#
from itala import itala

ACQUIRE_COUNT = 10


def grab_chunks():
    system = itala.create_system()
    devices_info = system.enumerate_devices(500)
    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 cancelled.")
        exit(1)
    device = system.create_device(devices_info[0])
    print("First device initialized.")
    nodemap = device.node_map
    print("Enabling chunk data..")

    # Enable the auto exposure time functionality to cause changes in the exposure
    # time value when the scene changes or the camera itself is moved.
    exposure_auto_node = nodemap.ExposureAuto
    if(not itala.is_writable(exposure_auto_node)):
        print("Unable to activate auto exposure time mode. Aborting.")
        exit(1)
    original_exposure_auto = exposure_auto_node.get_int_value()
    exposure_auto_node.from_string("Continuous")

    # The chunk data functionality is enabled via the "ChunkModeActive" feature.
    chunk_mode_active_node = nodemap.ChunkModeActive
    if(not itala.is_writable(chunk_mode_active_node)):
        print("Unable to activate chunk mode. Aborting.")
        exit(1)
    original_chunk_mode_active = chunk_mode_active_node.value
    chunk_mode_active_node.value = True

    # Each chunk data available can be enabled individually. In this case, exposure
    # time, pixel format and frame timestamp chunks are activated.
    # First select the exposure time chunk via the selector and enable it via the
    # "ChunkEnable" feature.
    chunk_selector_node = nodemap.ChunkSelector
    if(not itala.is_readable(chunk_selector_node)):
        print("Unable to retrieve chunk selector. Aborting.")
        exit(1)
    chunk_selector_node.from_string("ExposureTime")
    chunk_enable_node = nodemap.ChunkEnable
    original_exposure_time_chunk_enabled = chunk_enable_node.value
    chunk_enable_node.value = True

    # Do the same for frame ID chunk.
    chunk_selector_node.from_string("FrameID")
    original_frame_id_chunk_enabled = chunk_enable_node.value
    chunk_enable_node.value = True

    # And again for timestamp chunk
    chunk_selector_node.from_string("Timestamp")
    original_timestamp_chunk_enabled = chunk_enable_node.value
    chunk_enable_node.value = True

    device.start_acquisition()
    print("Acquisition started with chunk functionality enabled.\n")
    image_list = []
    for i in range(ACQUIRE_COUNT):
        image = device.get_next_image(1000)

        if(image.is_incomplete):
            print("Incomplete image.")
            print(f"\tBytes filled: {image.bytes_filled}/{image.payload_size}")
        else:
            print("Image grabbed.")

        # Add the image to the acquired frames list so that image chunks can be
        # retrieved and displayed after the grab.  
        image_list.append(image)

    # The chunks embedded in the available images are ready to be read.
    for i in image_list:
        print(f"Retrieving chunks for image {i.frame_id}")

        # Check if the acquired image has embedded chunk data.
        if(i.has_chunk_data):
            # Chunk data is exposed as a set GenICam features which names ar
            # regulated by the SFNC specification. In general, the names of
            # chunk features are defined by prepending the "Chunk" prefix
            # (e.g. ChunkExposureTime) to the equivalent standard feature
            # (e.g. ExposureTime).
            chunk_exposure_time = i.get_chunk_node("ChunkExposureTime")
            chunk_frame_id = i.get_chunk_node("ChunkFrameID")
            chunk_timestamp = i.get_chunk_node("ChunkTimestamp")

            print(f"\tChunkExposureTime: {chunk_exposure_time.value}")
            print(f"\tChunkFrameID: {chunk_frame_id.value}")
            print(f"\tChunkTimestamp: {chunk_timestamp.value}")
        else:
            print("\tNo chunk data attached.")

        i.dispose()

    # The acquisition must be stopped AFTER the acquired images have been used
    # (to display chunk data, in this case). If the acquisition was stopped
    # prematurely, errors may have occurred when accessing the image data (released
    # by the library).
    device.stop_acquisition()

    # Restore the original features state.
    exposure_auto_node.set_int_value(original_exposure_auto)

    chunk_selector_node.from_string("ExposureTime")
    chunk_enable_node.value = original_exposure_time_chunk_enabled

    chunk_selector_node.from_string("FrameID")
    chunk_enable_node.value = original_frame_id_chunk_enabled

    chunk_selector_node.from_string("Timestamp")
    chunk_enable_node.value = original_timestamp_chunk_enabled

    chunk_mode_active_node.value = original_chunk_mode_active

    device.dispose()
    print("Device instance disposed.")
    system.dispose()
    print("System instance disposed.")


if __name__ == "__main__":
    try:
        grab_chunks()
    except Exception as e:
        print(str(e))
        exit(1)
    exit(0)