getOrtValueData method
Gets the data from an OrtValue
valueId
is the ID of the OrtValue to get data from
Implementation
@override
Future<Map<String, dynamic>> getOrtValueData(String valueId) async {
try {
// Check if the tensor exists
if (!_ortValues.containsKey(valueId)) {
throw PlatformException(code: "INVALID_VALUE", message: "OrtValue not found with ID: $valueId", details: null);
}
final tensor = _ortValues[valueId]!;
// Get tensor type
final type = tensor.getProperty('type'.toJS);
// Get tensor shape
final jsShape = tensor.getProperty('dims'.toJS) as JSObject;
final shapeLength = (jsShape.getProperty('length'.toJS) as JSNumber).toDartInt;
final shape = <int>[];
// Convert shape to Dart list
for (var i = 0; i < shapeLength; i++) {
final dim = jsShape.getProperty(i.toString().toJS) as JSNumber;
shape.add(dim.toDartInt);
}
// Get tensor data
final jsData = tensor.getProperty('data'.toJS) as JSObject;
final dataLength = (jsData.getProperty('length'.toJS) as JSNumber).toDartInt;
final data = <dynamic>[];
// Convert data to Dart list based on type
switch (type.toString()) {
case 'float32':
for (var i = 0; i < dataLength; i++) {
final val = jsData.getProperty(i.toString().toJS) as JSNumber;
data.add(val.toDartDouble);
}
break;
case 'int32':
for (var i = 0; i < dataLength; i++) {
final val = jsData.getProperty(i.toString().toJS) as JSNumber;
data.add(val.toDartInt);
}
break;
case 'int64':
// BigInt handling
for (var i = 0; i < dataLength; i++) {
final val = jsData.getProperty(i.toString().toJS) as JSNumber;
// Convert BigInt or similar to standard number if possible
// For dart:js_interop, try direct conversion to num or int
data.add(val.toDartInt);
}
break;
case 'uint8':
for (var i = 0; i < dataLength; i++) {
final val = jsData.getProperty(i.toString().toJS) as JSNumber;
data.add(val.toDartInt);
}
break;
case 'bool':
for (var i = 0; i < dataLength; i++) {
// For boolean tensors, convert the numeric values back to proper Dart booleans
final val = jsData.getProperty(i.toString().toJS) as JSNumber;
// Return the actual numeric value (1 or 0), not a boolean,
// to match native implementations which return 1/0
data.add(val.toDartInt != 0 ? 1 : 0);
}
break;
case 'string':
for (var i = 0; i < dataLength; i++) {
final val = jsData.getProperty(i.toString().toJS) as JSString;
// For string tensors, extract string values
data.add(val.toString());
}
break;
default:
throw PlatformException(
code: "UNSUPPORTED_TYPE",
message: "Unsupported data type for extraction: $type",
details: null,
);
}
return {'data': data, 'shape': shape};
} catch (e) {
if (e is PlatformException) {
rethrow;
}
throw PlatformException(code: "DATA_EXTRACTION_ERROR", message: "Failed to get OrtValue data: $e", details: null);
}
}