basler / pypylon

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

how to send the variable value in Class ImageEventPrinter(pylon.ImageEventHandler) to the main thread? #274

Open ningjingzhiwei opened 4 years ago

ningjingzhiwei commented 4 years ago

Hello, everyone, I want to process the image data in Class ImageEventPrinter, and send the data to the main thread, referring to the demo"grabusinggrabloopthread.py", but it did not work. Can anyone help? Thanks in advance. The modified ImageEventPrinter code is as following, and I want to send Images to the main thread.

from pypylon import pylon import time

class ImageEventPrinter(pylon.ImageEventHandler): def OnImagesSkipped(self, camera, countOfSkippedImages): print("OnImagesSkipped event for device ", camera.GetDeviceInfo().GetModelName()) print(countOfSkippedImages, " images have been skipped.") print()

def OnImageGrabbed(self, camera, grabResult):
    print("OnImageGrabbed event for device ", camera.GetDeviceInfo().GetModelName())
    Images =[]
    # Image grabbed successfully?
    time1 =time.perf_counter_ns()
    if grabResult.GrabSucceeded():
        print("SizeX: ", grabResult.GetWidth())
        print("SizeY: ", grabResult.GetHeight())
        img = grabResult.GetArray()
        Images.append(img)
        print('the process time is:{} ms'.format((time.perf_counter_ns()-time1)/1e6))  
    else:
        print("Error: ", grabResult.GetErrorCode(), grabResult.GetErrorDescription())
ningjingzhiwei commented 4 years ago

@stefanklug

thiesmoeller commented 4 years ago

what is not working? You have to use a datastructure that is capable to handle multi-threaded access like a queue. Then you simply push the data ,that you generate in "OnImageGrabbed" into the queue. And in the main-thread you wait/query on image data available and than pop this data out of the queue

In an older issue I demonstrated this strategy in: https://github.com/basler/pypylon/issues/145#issuecomment-516848507

thiesmoeller commented 3 years ago

@ningjingzhiwei ... any feedback ?