convertBytesToObject static method

Object convertBytesToObject(
  1. Uint8List bytes,
  2. TensorType tensorType,
  3. List<int> shape
)

Implementation

static Object convertBytesToObject(
    Uint8List bytes, TensorType tensorType, List<int> shape) {
  // stores flattened data
  List<dynamic> list = [];
  if (tensorType.value == TfLiteType.kTfLiteInt32) {
    for (var i = 0; i < bytes.length; i += 4) {
      list.add(ByteData.view(bytes.buffer).getInt32(i, Endian.little));
    }
    return list.reshape<int>(shape);
  } else if (tensorType.value == TfLiteType.kTfLiteFloat32) {
    for (var i = 0; i < bytes.length; i += 4) {
      list.add(ByteData.view(bytes.buffer).getFloat32(i, Endian.little));
    }
    return list.reshape<double>(shape);
  } else if (tensorType.value == TfLiteType.kTfLiteInt16) {
    for (var i = 0; i < bytes.length; i += 2) {
      list.add(ByteData.view(bytes.buffer).getInt16(i, Endian.little));
    }
    return list.reshape<int>(shape);
  } else if (tensorType.value == TfLiteType.kTfLiteFloat16) {
    Uint8List list32 = Uint8List(bytes.length * 2);
    for (var i = 0; i < bytes.length; i += 2) {
      list32[i] = bytes[i];
      list32[i + 1] = bytes[i + 1];
    }
    for (var i = 0; i < list32.length; i += 4) {
      list.add(ByteData.view(list32.buffer).getFloat32(i, Endian.little));
    }
    return list.reshape<double>(shape);
  } else if (tensorType.value == TfLiteType.kTfLiteInt8) {
    for (var i = 0; i < bytes.length; i += 1) {
      list.add(ByteData.view(bytes.buffer).getInt8(i));
    }
    return list.reshape<int>(shape);
  } else if (tensorType.value == TfLiteType.kTfLiteUInt8) {
    for (var i = 0; i < bytes.length; i += 1) {
      list.add(ByteData.view(bytes.buffer).getUint8(i));
    }
    return list.reshape<int>(shape);
  } else if (tensorType.value == TfLiteType.kTfLiteInt64) {
    for (var i = 0; i < bytes.length; i += 8) {
      list.add(ByteData.view(bytes.buffer).getInt64(i));
    }
    return list.reshape<int>(shape);
  }
  throw UnsupportedError("$tensorType is not Supported.");
}