ctypes python3中的回调函数只在

2024-04-23 07:08:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在开发一种医学相机,它可以生成图像(每秒25帧)并发送到pythonqt应用程序。我正撞到一堵砖墙上,试图从照相机上得到一系列的图像。每个帧都由回调函数处理,然后由frameprocessor处理。你知道吗

我们在Windows7 64位和python3下运行

我们有以下c++标题:

DC_API  signed int Start(const ImageConfig * cfg, 
                  void (*NewImage)(const char * image, unsigned int width, unsigned int height, unsigned int frameNumber, unsigned long long frameTime));

我在python中使用的是:

def __init__(self, frame_processor ):
    self.callback_frame_processor = frame_processor
    def new_image(image, width, height, framenbr, frametime):
        self.width = width
        self.height = height
        self.frametime = frametime
        self.framenr = framenbr
        self.callback_frame_processor(image)
    self.new_image_function = ctypes.CFUNCTYPE( None, ctypes.POINTER(ctypes.c_char), ctypes.c_int,
                                       ctypes.c_int, ctypes.c_int, ctypes.c_longlong )
    self.callback_function = self.new_image_function(new_image)

帧处理器是:

def start_grabbing_frames(self):
    f = self._preparecall('Start')
    image_config_struct, image_config_pointer = self._fill_image_config()
    f.argtypes = [image_config_pointer, self.new_image_function]
    return self.process_errorcodes(f(ctypes.byref(image_config_struct), self.callback_function))

帧处理器是:

def camera_callback(self, raw_image, width, height):
    qt_image = QImage(raw_image, width, height, QImage.Format.RGB888)
    qpixmap = QPixmap(width, height)
    pix = qpixmap.fromImage(qt_image)
    self.emit(pix)

问题是该图像似乎不是有效帧(len(rawëimage)是13036022,而width是2208,height是1648)。这将导致挂起应用程序,其中不显示图像。你知道吗


Tags: 图像imageselfconfignewdefcallbackfunction