basler / pypylon

The official python wrapper for the pylon Camera Software Suite
http://www.baslerweb.com
BSD 3-Clause "New" or "Revised" License
566 stars 207 forks source link

Failed to receive frame from basler camera for several consecutive frames #421

Open MiladGhorbaniG opened 3 years ago

MiladGhorbaniG commented 3 years ago

Hi @rgov I am using a Jetson Nano with OpenCV 4.5 to read the Basler camera every 50 ms. I call the following function:

from pypylon import pylon
global camera
global converter
# conecting to the first available camera
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()
camera.AcquisitionMode.SetValue('Continuous')
camera.Width.SetValue(2448)
camera.Height.SetValue(2048)
#camera.MaxNumBuffer.SetValue(80)
camera.AcquisitionMode.SetValue('Continuous')
#camera.AutoFunctionProfile.SetValue('MinimizeGain')
camera.ExposureAuto.SetValue('Continuous')
camera.ExposureMode.SetValue('Timed')

camera.AcquisitionFrameRateEnable.SetValue(True)
camera.AcquisitionFrameRate.SetValue(30.0)
#camera.BalanceWhiteAuto.SetValue('Continuous')
camera.GainAuto.SetValue('Continuous')
# Grabing Continusely (video) with minimal delay
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly) 
converter = pylon.ImageFormatConverter()

# converting to opencv bgr format
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAqlignment = pylon.OutputBitAlignment_MsbAligned
def Cam_one(w,h):
    global camera_success,image_success,camera,converter
    camera_success=0
    image_success=0
    img = None
    if camera.IsGrabbing():
        camera_success=1
        grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
        if grabResult.GrabSucceeded():
            image_success=1
            # Access the image data
            image = converter.Convert(grabResult)
            im = image.GetArray()
            img=cv2.resize(im,(w,h))
        else:
            image_success=0
        grabResult.Release()
    else:
        camera_success=0
    return image_success , img

About 1 percent of the time, it fails for several consecutive frames, and I can receive a new frame for example 700 ms later. Does anyone have a suggestion?

SMA2016a commented 3 years ago

1) first, measure the time taken for the following block.

grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException) if grabResult.GrabSucceeded(): image_success=1

Access the image data

        image = converter.Convert(grabResult)
        im = image.GetArray()
        img=cv2.resize(im,(w,h))
    else:
        image_success=0
    grabResult.Release()
else:
    camera_success=0

2) Then set the AOI to 640*480 and check if the issue is still occurring. 3) since you are already using opencv, try also use opencv for image conversation too.