fromTensorData static method

Future<Uint8List> fromTensorData(
  1. TensorData tensorData
)

Implementation

static Future<Uint8List> fromTensorData(TensorData tensorData) async {
  // Converter os dados do tensor de volta para Float32List
  final floatData = Float32List.view(tensorData.data.buffer);
  // Obter as dimensões da imagem
  final int width = tensorData.shape[3] as int;
  // Obter as dimensões da imagem
  final int height = tensorData.shape[2] as int;

  // Criar uma imagem vazia
  final image = img.Image(width: width, height: height);

  // Preencher a imagem com os dados do tensor
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      final r = ((floatData[(i * width + j) * 3] * 0.229 + 0.485) * 255)
          .clamp(0, 255)
          .toInt();
      final g = ((floatData[(i * width + j) * 3 + 1] * 0.224 + 0.456) * 255)
          .clamp(0, 255)
          .toInt();
      final b = ((floatData[(i * width + j) * 3 + 2] * 0.225 + 0.406) * 255)
          .clamp(0, 255)
          .toInt();

      image.setPixelRgb(j, i, r, g, b);
    }
  }
  // Codificar a imagem de volta para PNG
  return Uint8List.fromList(img.encodePng(image));
}