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

Flush queue after calling `StopGrabbing` #647

Open zacharynevin-stemcell opened 1 year ago

zacharynevin-stemcell commented 1 year ago

Hi there,

There seem to be a lot of different variables related to queueing of images. There are variables such as MaxNumQueuedBuffer, MaxNumBuffer, and OutputQueueSize, and I am not clear on the difference between them.

For context, I want to do an acquisition with the GrabStrategy_OneByOne strategy and use an ImageEventHandler to handle conversion and saving to disk.

class ImageEventHandler(pylon.ImageEventHandler):
    def __init__(self):
        super().__init__()
        self.converter = pylon.ImageFormatConverter()
        self.converter.OutputPixelFormat = pylon.PixelType_RGB8packed
        self.converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

    def OnImageGrabbed(self, camera, grabResult):
        if grabResult.GrabSucceeded():
            image = self.converter.convert(grabResult)
            cv2.imwrite(f"myimage_{grabResult.GetImageNumber()}.tif", image.GetArray())

image_event_handler = ImageEventHandler()
pycam.RegisterImageEventHandler(image_event_handler, pylon.RegistrationMode_ReplaceAll, pylon.Cleanup_Delete)
pycam.StartGrabbing(pylon.GrabStrategy_OneByOne, pylon.GrabLoop_ProvidedByInstantCamera)

When I call pycam.StopGrabbing(), I want to basically "flush" the remaining items in the queue.

For example, if I am running the camera at 60fps for 10 seconds and I stop the acquisition, I still want to save any existing items in the queue instead of discarding. Additionally, I want to ensure that even if the ImageEventHandler.onImageGrabbed function can't keep up with the framerate, that I don't lose any frames.

However, it seems that when I call StopGrabbing, that the whole image event handler also just stops. If I run the acquisition function for 10 seconds at 60fps, I should have 600 frames eventually but I end up with ~80-100.

SMA2016a commented 1 year ago

All parameters are explained on this documentation page.

https://docs.baslerweb.com/pylonapi/net/T_Basler_Pylon_PLCameraInstance

I would say, that your setup loses images since writing image to the hard disk takes longer that 16ms.

try to set MaxNumBuffer = 500

thiesmoeller commented 1 year ago

.. and call StopGrabbing from your OnImageGrabbed callback. Then you'll know the exactly when all images have been processed