Problem:
I am working on a Flutter project where I need to run a TensorFlow Lite (TFLite) object detection model. The model has specific input and output shape requirements, and I am encountering difficulties preprocessing the input image to match the required input format.
I have the following model details:
- Input shape:
[1, 640, 640, 3]
(a single image with 640×640 resolution and 3 color channels) - Output shape:
[1, 25200, 7]
(model will output a list of detection results)
The main challenge is correctly preprocessing the input image. I need to resize the image to 640×640 pixels and convert it into the correct data format (float32
) that the model expects.
I have tried multiple approaches, but none of them seem to work as expected. The image is not being processed correctly, leading to errors and incorrect predictions.
Specific Issues:
- The image preprocessing pipeline (resizing, normalization, and format conversion) is not working as expected.
- I’ve used the
tflite_flutter_helper
package, but it depends on outdated dependencies, which causes compatibility issues. - The input format needs to be a
float32
array, but I am unsure how to correctly convert the image and feed it into the model.
What I Tried:
I tried multiple approaches to preprocess the input image for my TensorFlow Lite model. Here’s what I did:
- First Attempt:
I used the following code to load the image and convert it tofloat32
:
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tflite/tflite.dart';
import 'dart:developer' as developer;
import 'package:flutter/material.dart';
import 'package:tflite_flutter/tflite_flutter.dart';
class ScanController extends GetxController {
@override
void onInit() {
super.onInit();
loadModel();
}
void loadModel() async{
final interpreter = await Interpreter.fromAsset('assets/model.tflite');
var image = File('assets/test.jpg');
var imageBytes = await image.readAsBytes();
// conver the image to float32
final input = imageBytes.buffer.asFloat32List();
// output size is 1 x 25200 x 7
final output = List.filled(1 * 25200 * 7, 0.0).reshape([1, 25200, 7]);
interpreter.run(input, output);
developer.log(output.toString());
}
}
but this didn’t work
i also tried to use TensorImage and ImageProcessorBuilder from tflite_flutter_helper in this code
import 'package:get/get.dart';
import 'package:get/get_connect/http/src/utils/utils.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tflite_flutter/tflite_flutter.dart' as tflite;
import 'dart:developer' as developer;
import 'dart:io';
import 'package:image/image.dart';
import 'package:image/image.dart' as img;
import 'package:tflite_flutter_helper/tflite_flutter_helper.dart';
import 'package:tflite_flutter/tflite_flutter.dart';
import 'package:tflite_flutter_helper/tflite_flutter_helper.dart';
class ScanController extends GetxController {
@override
void onInit() {
super.onInit();
loadModel();
}
void loadModel() async {
try {
final interpreter = await tflite.Interpreter.fromAsset('assets/model.tflite');
developer.log('Model loaded successfully.', name: 'scan_controller.dart');
developer.log('input info:');
var inputShape = interpreter.getInputTensor(0).shape;
developer.log('shape: $inputShape');
var inputType = interpreter.getInputTensor(0).type;
developer.log('type: $inputType');
var outputShape = interpreter.getOutputTensor(0).shape;
developer.log('output shape: $outputShape');
var outputType = interpreter.getOutputTensor(0).type;
developer.log('output type: $outputType');
var file = File('assets/test.jpg');
// Input shape: [1, 640, 640, 3]
ImageProcessor imageProcessor = ImageProcessorBuilder()
.add(ResizeOp(640, 640, ResizeMethod.BILINEAR))
.add(NormalizeOp(0, 255))
.build();
TensorImage tensorImage = TensorImage.fromFile(file);
tensorImage = imageProcessor.process(tensorImage);
// Output shape: [1, 25200, 7]
TensorBuffer outputBuffer = TensorBuffer.createFixedSize(outputShape, tflite.TfLiteType.float32);
// Run inference
interpreter.run(tensorImage.buffer, outputBuffer.buffer);
// print the result
var output = outputBuffer.getDoubleList();
developer.log('output: $output');
} catch (e) {
developer.log('Failed to load model: $e', name: 'scan_controller.dart');
}
}
}
but it also didn’t work
Can someone please guide me on how to properly preprocess the input image to match the input shape [1, 640, 640, 3] and convert it to the required float32 format so that it can be passed to the model?
yousef sultan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.