diff --git a/android/build.gradle b/android/build.gradle index 8cf93da..61cfc68 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -55,13 +55,13 @@ android { } defaultConfig { - minSdkVersion 26 + minSdkVersion 19 } } dependencies { - def tflite_version = "2.12.0" + def tflite_version = "2.11.0" implementation("org.tensorflow:tensorflow-lite:${tflite_version}") implementation("org.tensorflow:tensorflow-lite-gpu:${tflite_version}") diff --git a/lib/src/util/byte_conversion_utils.dart b/lib/src/util/byte_conversion_utils.dart index a52054e..2538c67 100644 --- a/lib/src/util/byte_conversion_utils.dart +++ b/lib/src/util/byte_conversion_utils.dart @@ -14,6 +14,8 @@ * limitations under the License. */ +import 'dart:convert'; +import 'dart:ffi'; import 'dart:typed_data'; import 'package:tflite_flutter/tflite_flutter.dart'; @@ -165,6 +167,34 @@ class ByteConversionUtils { ); } + /// Decodes a TensorFlow string to a List + static List decodeTFStrings(Uint8List bytes) { + /// The decoded string + List decodedStrings = []; + + /// get the first 32bit int representing num of strings + int numStrings = ByteData.view(bytes.sublist(0, sizeOf()).buffer) + .getInt32(0, Endian.little); + + /// parse subsequent string position and sizes + for (int s = 0; s < numStrings; s++) { + // get current str index + int startIdx = ByteData.view(bytes + .sublist((1 + s) * sizeOf(), (2 + s) * sizeOf()) + .buffer) + .getInt32(0, Endian.little); + // get next str index, or in last case the ending byte position + int endIdx = ByteData.view(bytes + .sublist((2 + s) * sizeOf(), (3 + s) * sizeOf()) + .buffer) + .getInt32(0, Endian.little); + + decodedStrings.add(utf8.decode(bytes.sublist(startIdx, endIdx))); + } + + return decodedStrings; + } + static Object convertBytesToObject( Uint8List bytes, TensorType tensorType, List shape) { // stores flattened data @@ -209,6 +239,9 @@ class ByteConversionUtils { list.add(ByteData.view(bytes.buffer).getInt64(i)); } return list.reshape(shape); + } else if (tensorType.value == TfLiteType.kTfLiteString) { + list.add(decodeTFStrings(bytes)); + return list; } throw UnsupportedError("$tensorType is not Supported."); }