blit_buffer 函数在没有任何警告的情况下关闭 Kivy 应用程序

blit_buffer function closes Kivy app without any warning

我想将 ROS 传感器图像转换为 Kivy 纹理。但是 blit_buffer 没有任何消息就关闭了应用程序。我检查了 sensor_msg 的颜色格式是 bgr8。我不知道是什么问题,因为没有错误信息。

def convert_to_texture(self, sensor_msg):
    cv_image = CvBridge().imgmsg_to_cv2(sensor_msg, "bgr8")
    resized_image = cv2.resize(cv_image, (900, 450))

    buf = cv2.flip(resized_image, 0).tostring()

    texture = Texture.create(size=(resized_image.shape[1], resized_image.shape[0]), colorfmt="bgr")
    texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")

我以前看过这个。我相信 texture.blit_buffer() 必须在主线程上完成。我还没有找到任何这样说的文档,但这是我的经验。尝试这样的事情:

def convert_to_texture(self, sensor_msg):
    cv_image = CvBridge().imgmsg_to_cv2(sensor_msg, "bgr8")
    resized_image = cv2.resize(cv_image, (900, 450))

    buf = cv2.flip(resized_image, 0).tostring()

    texture = Texture.create(size=(resized_image.shape[1], resized_image.shape[0]), colorfmt="bgr")
    # texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")
    Clock.schedule_once(partial(self.updateTexture, texture, buf))

    def updateTexture(self, texture, buf, *args):
        texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')