tflite_flutter_helper icon indicating copy to clipboard operation
tflite_flutter_helper copied to clipboard

[FEATURE] ImageConversion: support for converting float32 TensorBuffer to Image

Open bazinac opened this issue 4 years ago • 2 comments

bazinac avatar Sep 20 '20 08:09 bazinac

@bazinac We will need more flexibility here, along with your changes. Thanks a lot for your contribution. I will make changes on top of these and merge them.

am15h avatar Oct 01 '20 14:10 am15h

Can we also add support for converting Image to float32 TensorBuffer for those who need it for their model? I did this successfully.

static void convertImageToTensorBuffer(Image image, TensorBuffer buffer) { int w = image.width; int h = image.height; List intValues = image.data; List shape = [h, w, 3]; if (buffer.getDataType() == TfLiteType.uint8) { List rgbValues = List(h * w * 3); for (int i = 0, j = 0; i < intValues.length; i++) { if (intValues[i] == null) { print(i); } rgbValues[j++] = ((intValues[i]) & 0xFF); rgbValues[j++] = ((intValues[i] >> 8) & 0xFF); rgbValues[j++] = ((intValues[i] >> 16) & 0xFF); }

  buffer.loadList(rgbValues, shape: shape);
} else if (buffer.getDataType() == TfLiteType.float32) {
  Float32List rgbValues = new Float32List(w * h * 3);
  for (int i = 0, j = 0; i < intValues.length; i++) {
    if (intValues[i] == null) {
      print(i);
    }
    rgbValues[j++] = (((intValues[i]) & 0xFF) / 255.0).toDouble();
    rgbValues[j++] = (((intValues[i] >> 8) & 0xFF) / 255.0).toDouble();
    rgbValues[j++] = (((intValues[i] >> 16) & 0xFF) / 255.0).toDouble();
  }
  buffer.loadList(rgbValues, shape: shape);
} else {
  throw UnsupportedError(
    "The TensorBuffer of type ${buffer.getBuffer()} to Image is not supported yet.",
  );
}

}`

TexMexMax avatar Oct 23 '20 20:10 TexMexMax