在flutter中将CameraImage/Uint8List转换为rgb数组

问题描述 投票:0回答:1

我目前正在努力将我的 Python 后端与我的 Flutter 前端连接起来。我的后端托管一个姿势估计模型,需要一个 numpy 数组作为输入,表示图像。因此,我的目标是将在 Dart 中获得的 CameraImage 数据直接转换为 RGB/BGR numpy 数组。

with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image.flags.writeable = False
    results = pose.process(image)
    #...

挑战在于 CameraImage 数据格式可能会根据设备操作系统的不同而有所不同。

我也想过将其转换为 png 并将 png 文件发送到我的后端,但我认为这种方法效率非常低。

我能够将其转换为 Uint8List 并且我尝试按照此处的步骤进行操作

controller!.startImageStream((CameraImage img) {
        if (!isDetecting) {
          isDetecting = true;
          Uint8List imageByte = Uint8List.fromList(img.planes.expand((plane) => plane.bytes).toList());
          final decoder = Imagi.JpegDecoder();
          final decodedImg = decoder.decodeImage(imageByte);
          // ...
          isDetecting = false;
        }
      });

将 Image 对象转换为 RGB 像素数组并返回 Flutter

并明确尝试过,但decodeImage不再可用,可能已贬值。

如果有人能帮助我,我会非常高兴:D.

arrays flutter image dart rgb
1个回答
0
投票

image: ^4.0.17

您通常会使用前缀导入它:

import 'package:image/image.dart' as image;

然后你可以:

  • 获取特定的解码器实例并使用它:
final decoder = image.JpegDecoder();
final decodedImage = decoder.decode(bytes) as image.Image;
  • 使用快捷功能直接解码已知图像格式:
final decodedImage = image.decodeJpg(bytes) as image.Image;
  • 如果您不知道源图像格式,请使用通用
    decodeImage
    。 这是最昂贵的,因为它会尝试所有可用的解码器,直到找到正确的解码器。
final decodedImage = image.decodeImage(bytes) as image.Image;

一旦获得解码的图像实例,您就可以使用自定义通道顺序获取字节:

final data = decodedImage.getBytes(order: image.ChannelOrder.rgb);

如果确实需要经历这一切,我也会尝试使用

CameraImage.format
。也许图像已经是正确的格式,您不必对它们进行解码。至少它会帮助您决定使用什么解码器。

就我个人而言,我会将图像以 JPEG 格式发送到服务器并在那里进行解码,因为以原始格式传输图像会占用更多带宽。

© www.soinside.com 2019 - 2024. All rights reserved.