convertImageToTensorBuffer static method

void convertImageToTensorBuffer(
  1. Image image,
  2. TensorBuffer buffer
)

Implementation

static void convertImageToTensorBuffer(Image image, TensorBuffer buffer) {
  int w = image.width;
  int h = image.height;
  List<int> intValues = image.data;
  int flatSize = w * h * 3;
  List<int> shape = [h, w, 3];
  switch (buffer.getDataType()) {
    case TfLiteType.uint8:
      List<int> byteArr = List.filled(flatSize, 0);
      for (int i = 0, j = 0; i < intValues.length; i++) {
        byteArr[j++] = ((intValues[i]) & 0xFF);
        byteArr[j++] = ((intValues[i] >> 8) & 0xFF);
        byteArr[j++] = ((intValues[i] >> 16) & 0xFF);
      }
      buffer.loadList(byteArr, shape: shape);
      break;
    case TfLiteType.float32:
      List<double> floatArr = List.filled(flatSize, 0.0);
      for (int i = 0, j = 0; i < intValues.length; i++) {
        floatArr[j++] = ((intValues[i]) & 0xFF).toDouble();
        floatArr[j++] = ((intValues[i] >> 8) & 0xFF).toDouble();
        floatArr[j++] = ((intValues[i] >> 16) & 0xFF).toDouble();
      }
      buffer.loadList(floatArr, shape: shape);
      break;
    default:
      throw StateError(
          "${buffer.getDataType()} is unsupported with TensorBuffer.");
  }
}