我正在开发一个用于图像分割的Flutter应用程序,用户可以在其中上传或捕获照片,并且该应用程序使用TensorFlow Lite模型分析这些照片。但是,当我尝试使用图像库将像素数据(模型分析后)转换为图像时遇到了 RangeError。
以下是我的工作流程的简要概述:
这是代码有问题的部分:
List<int> pixelData = [];
// Fill pixelData based on model's output
// ...
// Convert List<int> to Uint8List
Uint8List byteData = Uint8List.fromList(pixelData);
// Attempt to create ByteBuffer from byteData
ByteBuffer buffer = byteData.buffer;
// Try to create an image from the ByteBuffer
// Don't be confused about the imgimg -- i imported the library like that
// import 'package:image/image.dart' as imgimg;
imgimg.Image? segmentedImage = imgimg.Image.fromBytes(
width: imgWidth,
height: imgHeight,
bytes: buffer,
);
尝试创建segmentedImage时出现错误:
RangeError (end): Invalid value: Not in inclusive range 261120..262144: 262656
pixelData 的长度正好是 262144,这对于 512x512 图像来说是正确的,其中每个像素都由单个整数颜色值表示。该错误似乎表明转换过程或 ByteBuffer 的处理方式存在问题。任何人都可以深入了解可能导致此 RangeError 的原因以及如何将我的 PixelData 正确转换为 ByteBuffer 以进行图像创建吗?有没有更好的方法在 Flutter 中将 TensorFlow 模型的输出可视化为图像?
任何帮助或建议将不胜感激!
如果需要,这里是剩余功能的简要概述。
Future<void> _onUseImagePressed() async {
if (_image == null) return;
// Load and resize the image
imgimg.Image? image = imgimg.decodeImage(File(_image!.path).readAsBytesSync());
imgimg.Image resizedImage = imgimg.copyResize(image!, width: 512, height: 512);
// Prepare the model input
var input = List.generate(1, (_) => List.generate(512, (_) => List.generate(512, (_) => List.generate(3, (_) => 0.0))));
// Populate input with normalized pixel values
for (int y = 0; y < 512; y++) {
for (int x = 0; x < 512; x++) {
var pixel = resizedImage.getPixel(x, y);
input[0][y][x][0] = pixel.r / 255.0;
input[0][y][x][1] = pixel.g / 255.0;
input[0][y][x][2] = pixel.b / 255.0;
}
}
// Run the TensorFlow Lite model
final interpreter = await Interpreter.fromAsset('assets/models/production_model_resnet50.tflite');
var output = List.filled(1 * 512 * 512 * 2, 0).reshape([1, 512, 512, 2]);
interpreter.run(input, output);
// Generate pixel data based on model output
List<int> pixelData = [];
for (int y = 0; y < 512; y++) {
for (int x = 0; x < 512; x++) {
int classIndex = output[0][y][x][0] > output[0][y][x][1] ? 0 : 1;
int color = classIndex == 0 ? 0xFFFF0000 : 0xFF00FF00;
pixelData.add(color);
}
}
// Convert pixelData to Uint8List and ByteBuffer
Uint8List byteData = Uint8List.fromList(pixelData);
ByteBuffer buffer = byteData.buffer;
// Attempt to create an image from bytes
try {
imgimg.Image? segmentedImage = imgimg.Image.fromBytes(512, 512, buffer);
} catch (e) {
print("Error processing model output: $e");
}
}
我做了以下调整:
List<int> pixelData = [];
for (int y = 0; y < imgHeight; y++) {
for (int x = 0; x < imgWidth; x++) {
int classIndex = output[0][y][x][0] > output[0][y][x][1] ? 0 : 1;
int colorValue = classIndex == 0 ? 0 : 255;
pixelData.add(colorValue);
}
}
segmentedImage = imgimg.Image.fromBytes(
width: imgWidth,
height: imgHeight,
bytes: buffer,
format: imgimg.Format.uint8,
numChannels: 1
);