0

That function is too slow. So Flutter CameraImage efficiency convert to TensorImage in dart?

    var img = imglib.Image(image.width, image.height); // Create Image buffer

    Plane plane = image.planes[0];
    const int shift = (0xFF << 24);

    // Fill image buffer with plane[0] from YUV420_888
    for (int x = 0; x < image.width; x++) {
      for (int planeOffset = 0;
          planeOffset < image.height * image.width;
          planeOffset += image.width) {
        final pixelColor = plane.bytes[planeOffset + x];
        // color: 0x FF  FF  FF  FF
        //           A   B   G   R
        // Calculate pixel color
        var newVal =
            shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor;

        img.data[planeOffset + x] = newVal;
      }
    }

    return img;
  }```

1 Answers1

0

Seems your for loop is inefficient. The data for whole row (with same placeOffset, different x) will be cached at once, so would be faster to switch ordering of the two loops.

for (int y = 0; y < image.height; y++) {
  for (int x = 0; x < image.width; x++) {
    final pixelColor = plane.bytes[y * image.width + x];
    // ...
  }
}

However, your code does not seems to be reading from the actual camera stream. please refer this thread for converting CameraImage to Image.

How to convert Camera Image to Image in Flutter?

Taehee Jeong
  • 146
  • 2