TheImagingSource / ic4-examples

IC4 Example Programs
Apache License 2.0
5 stars 3 forks source link

<ErrorCode.Timeout: 27>, 'ic4_snapsink_snap_single: Timeout elapsed' #22

Open Claywzfb opened 1 month ago

Claywzfb commented 1 month ago

Hi, I tried using this code from the examples: save-jpeg-file.py. It was working fine before, but for some reason it stopped working (seems to have started after running the "ic4 Demo Application"). This is the error message the program gives:

runfile('D:/0/save-jpeg-file.py', wdir='D:/0')
[0] DMK 41AU02 (40024083) [IC GenTL Virtual Interface]
Select device [0..0]: 
[ic4      core][   info][             InitLibrary.cpp:99  ] Initializing IC Imaging Control 4 Core Library Version 1.2.0.2954

0

Press ENTER to snap and save a jpeg image
Traceback (most recent call last):

  File "D:\0\save-jpeg-file.py", line 44, in <module>
    example_save_jpeg_file()

  File "D:\0\save-jpeg-file.py", line 27, in example_save_jpeg_file
    buffer = sink.snap_single(1000)

  File "D:\download\anaconda\lib\site-packages\imagingcontrol4\snapsink.py", line 164, in snap_single
    IC4Exception.raise_exception_from_last_error()

  File "D:\download\anaconda\lib\site-packages\imagingcontrol4\ic4exception.py", line 46, in raise_exception_from_last_error
    raise IC4Exception(err, message.value.decode("utf-8"))

IC4Exception: (<ErrorCode.Timeout: 27>, 'ic4_snapsink_snap_single: Timeout elapsed')

[ic4      core][  error][           FrameSnapSink.cpp:117 ] ic4_snapsink_snap_single: Timeout elapsed

BTW, I am trying to use CCD to capture an image every 1 second. Is there something wrong with my code? (It worked fine before, but now I get the same error message as "save-jpeg-file.py")

import imagingcontrol4 as ic4
import time
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np

#%% Function to capture multiple images for a long time and calculate the average
def capture_and_average(one_cam_device):
    # Open the camera with the grabber to control the camera information flow
    grabber = ic4.Grabber(one_cam_device)
    # Create a SnapSink to grab the buffer
    sink = ic4.SnapSink()
    # Start the camera data stream and send it to the buffer sink
    grabber.stream_setup(sink)
    # Get the camera property map
    map = grabber.device_property_map

    #%% Print the allowed range
    print("=======================================")
    print("*Get the allowed range of camera properties*")
    # Get the minimum and maximum values of the resolution width property
    WIDTH_min = map[ic4.PropId.WIDTH].minimum
    WIDTH_max = map[ic4.PropId.WIDTH].maximum
    print("*The range of resolution width is:", WIDTH_min, "~", WIDTH_max)
    # Get the minimum and maximum values of the resolution height property
    HEIGHT_min = map[ic4.PropId.HEIGHT].minimum
    HEIGHT_max = map[ic4.PropId.HEIGHT].maximum
    print("*The range of resolution height is:", HEIGHT_min, "~", HEIGHT_max)
    # Get the minimum and maximum values of the gain property
    GAIN_min = map[ic4.PropId.GAIN].minimum
    GAIN_max = map[ic4.PropId.GAIN].maximum
    print("*The range of gain is:", GAIN_min, "~", GAIN_max)
    # Get the minimum and maximum values of the exposure property
    EXPOSURE_TIME_min = map[ic4.PropId.EXPOSURE_TIME].minimum
    EXPOSURE_TIME_max = map[ic4.PropId.EXPOSURE_TIME].maximum
    print("*The range of exposure is:", EXPOSURE_TIME_min, "~", EXPOSURE_TIME_max)
    # Get the minimum and maximum values of the frame rate property
    ACQUISITION_FRAME_RATE_min = map[ic4.PropId.ACQUISITION_FRAME_RATE].minimum
    ACQUISITION_FRAME_RATE_max = map[ic4.PropId.ACQUISITION_FRAME_RATE].maximum
    print("*The range of frame rate is:", ACQUISITION_FRAME_RATE_min, "~", ACQUISITION_FRAME_RATE_max)
    # Get the minimum and maximum values of the GAMMA property
    GAMMA_min = map[ic4.PropId.GAMMA].minimum
    GAMMA_max = map[ic4.PropId.GAMMA].maximum
    print("*The range of GAMMA is:", GAMMA_min, "~", GAMMA_max)
    print("=======================================")
    #%% Set camera parameters
    #map.set_value(ic4.PropId.EXPOSURE_AUTO, "Off") # Turn off automatic exposure, there is an error
    map.try_set_value(ic4.PropId.EXPOSURE_AUTO, "Off")  # Turn off automatic exposure
    map.set_value(ic4.PropId.EXPOSURE_TIME, 100)  # Adjust exposure time

    map.set_value(ic4.PropId.WIDTH, WIDTH_max)  # Set to maximum resolution width 1280
    map.set_value(ic4.PropId.HEIGHT, HEIGHT_max)  # Set to maximum resolution height 960
    map.set_value(ic4.PropId.GAIN, GAIN_min)  # Set to minimum gain value 260
    map.set_value(ic4.PropId.ACQUISITION_FRAME_RATE, ACQUISITION_FRAME_RATE_max)  # Set frame rate to maximum value 15
    map.set_value(ic4.PropId.GAMMA, 100)  # Gamma is set to 100, which means no change to the original image
    map.set_value(ic4.PropId.PIXEL_FORMAT, ic4.PixelFormat.Mono8)  # Set to 8-bit grayscale pixel format, i.e. 8-bit grayscale image

    # You can view the set value
    #frame_rate = map[ic4.PropId.ACQUISITION_FRAME_RATE].value
    print("Parameter setting completed")
    #%%
    # Create a list to store all NumPy arrays
    all_images = []
    # Capture images and save
    for i in range(3):
        # Grab the buffer of the next image
        buffer = sink.snap_single(1000)  # Give up waiting at most 1000ms
        print(f"Capturing the {i+1}th image is complete")
        # Convert the buffer data to a NumPy array
        img_array = buffer.numpy_wrap()
        all_images.append(img_array)
        # Pause for n seconds
        time.sleep(1)

    # Calculate the average brightness of all images
    average_image = np.mean(all_images, axis=0)

    # Close the camera after use, otherwise it cannot be used next time
    grabber.stream_stop()
    grabber.device_close()
    print("Closing the camera is complete")
    return average_image, all_images

#%% 
if __name__ == "__main__":
    # Initialize the camera
    with ic4.Library.init_context(api_log_level=ic4.LogLevel.INFO, log_targets=ic4.LogTarget.STDERR):
        # Get the device list
        device_list = ic4.DeviceEnum.devices()
        # Select DMK 41AU02
        cam_41AU02 = device_list[0]
        #
        average_image, all_imgs = capture_and_average(cam_41AU02)
        # Save the average brightness image
        cv2.imwrite("average_image.bmp", average_image)
        print("=====Calculating the average image is complete=====")
TIS-Stefan commented 1 month ago

Hello

First of all, your script works on my computer. But: I needed to change the frame rate down to 7.5 instead of 15.0 fps. The DMK 41AU02 cameras do not run with full frame rate on Intel USB 3 controllers. This is caused by an incompatibility between the the camera and the USB 3 chip. Then I needed to add an USB 3 hub between camera and computer, because the camera immediately disconnects after starting the stream, when connected directly to my AMD USB 3 controller.

So the main measure to be taken is to reduce the frame rate.

I also would like to suggest to configure the camera before starting the stream, so you move

# Start the camera data stream and send it to the buffer sink
 grabber.stream_setup(sink)

After the line

 print("Parameter setting completed")

Stefan