genicam / harvesters

Image Acquisition Library for GenICam-based Machine Vision System
Apache License 2.0
513 stars 88 forks source link

Acquisition of frames lags before reaching the desired number of frames #400

Open gottn opened 1 year ago

gottn commented 1 year ago

Description I am facing the following issue. I am using Harvesters to acquire photos from two usb 3 cameras (GO-5000M-USB). In my application the two cameras should start at the same time (there is a small delay between the two cameras as I am not using a hardware trigger but It is acceptable) then acquire a certain number of frames with a pre defined frame rate (1 fps). I wrote a simple python script that performs this procedure. Sometimes the script just lags before reaching the desired number of frames (for example I want to acquire 500 frames but it just stops at 43 frames). When this happens I can only exit the terminal and run the script again. The main issue is I don't have the required number of frames per run. I am doing material testing so when I run the script I also run some mechanical testing machine. When the script stops in the middle of an experiment I lose information due to not recording all the frames.

To Reproduce Steps to reproduce the behavior:

  1. Run python file in terminal

The script

from harvesters.core import Harvester
import cv2
import os
import sys
import numpy as np

width = 2560
height = 2048
sdk_cti_path = r'C:\Program Files\JAI\SDK\bin\JaiUSB3vTL.cti'
cam1 = {'serial_number': 'U502186'}
cam2 = {'serial_number': 'U502937'}

#---------------------------------------------------------------------------------------
# Functions
#---------------------------------------------------------------------------------------

def init_two_cameras(cam1, cam2, sdk_cti_path, fps):
    """Function to initiate a computer vision camera"""

    # Create harvester object 
    h = Harvester()
    h.add_file(sdk_cti_path)
    h.update()

    # Create image acquirer
    ia1 = h.create(cam1)
    ia2 = h.create(cam2)

    # Control fps
    ia1.remote_device.node_map.AcquisitionFrameRate.value = fps
    ia2.remote_device.node_map.AcquisitionFrameRate.value = fps

    # Start aquisition
    ia1.start()
    ia2.start()

    return h, ia1, ia2 

def shut_camera(image_acquirer, harvester):
    """function to shut down the camera"""
    image_acquirer.stop()
    image_acquirer.destroy()
    harvester.reset()

#-----------------------------------------------------------------------------------------------------------------
# Main 
n_frames_cam1 = 500 # Number of frames required for camera 1
n_frames_cam2 = 500 # Number of frames required for camera 2
fps = 1 # Number of frames per second 

#------------------------------------------------------------------------------------------------------------

h, ia1, ia2 = init_two_cameras(cam1, cam2, sdk_cti_path, 1)

# Acquire frames and write to disk
counter = 0
for i in range(n_frames_cam1):
    with ia1.fetch() as buffer:  
        #create an alias of the 2D image component:
        component = buffer.payload.components[0]

        #Reshape the NumPy array into a 2D array:
        frame = component.data.reshape(component.height, component.width)

        # convert to bgr
        frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) 

        # save a snapshots
        cv2.imwrite('cam1_' + str(i) + '.jpg', frame)

    with ia2.fetch() as buffer:
        #create an alias of the 2D image component:
        component2 = buffer.payload.components[0]

        #Reshape the NumPy array into a 2D array:
        frame2 = component2.data.reshape(component2.height, component2.width)

        # convert to bgr
        frame2 = cv2.cvtColor(frame2, cv2.COLOR_GRAY2BGR) 

        # save a snapshots
        cv2.imwrite('cam2_' + str(i) + '.jpg', frame2)

    counter = counter +1
    print(counter)

shut_camera(ia1, h) 
shut_camera(ia2, h)

Expected Behavior I expect all the frames from both cameras to be acquired and saved safely.

Configuration

Reproducibility

This phenomenon can be stably reproduced:

Actions You Have Taken

gottn commented 1 year ago

It turned out the cables used to connect the cameras were faulty, after replacing them the problem was solved.