Comments (4)
I create a .png file from a Container widget. First I wrap the Container with a RepantBoundary widget:
child: RepaintBoundary( key: _globalKey, child: Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.width, color: Colors.lightBlue, child: Image.file(croppedF))))),
Then I run this code to create the png file:
`RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
var buffer = byteData.buffer;
testFile = carPath + '/' + DateTime.now().toString() + '.png';
File(testFile).writeAsBytesSync(
buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
print('test file $testFile written');
`
The file is analyzed with Tensorflow Lite:
var recognitions = await Tflite.runModelOnImage( path: testFile, numResults: 5, threshold: 0.15, imageMean: 127.5, imageStd: 127.5, );
This works fine. But shouldn't it be possible to analyze the image directly, without creating a .png file, and instead use Tflite.runModelOnBinary? I cannot get this to work. What should the binary: parameter be? I can't find that this is documented. Could you please add instructions in the Readme file?
from flutter_tflite.
.
from flutter_tflite.
Hi BoHellgren,
README does have examples of how images should be converted to binary.
// for float model
Uint8List imageToByteListFloat32(
img.Image image, int inputSize, double mean, double std) {
var convertedBytes = Float32List(1 * inputSize * inputSize * 3);
var buffer = Float32List.view(convertedBytes.buffer);
int pixelIndex = 0;
for (var i = 0; i < inputSize; i++) {
for (var j = 0; j < inputSize; j++) {
var pixel = image.getPixel(j, i);
buffer[pixelIndex++] = (img.getRed(pixel) - mean) / std;
buffer[pixelIndex++] = (img.getGreen(pixel) - mean) / std;
buffer[pixelIndex++] = (img.getBlue(pixel) - mean) / std;
}
}
return convertedBytes.buffer.asUint8List();
}
// for int model
Uint8List imageToByteListUint8(img.Image image, int inputSize) {
var convertedBytes = Uint8List(1 * inputSize * inputSize * 3);
var buffer = Uint8List.view(convertedBytes.buffer);
int pixelIndex = 0;
for (var i = 0; i < inputSize; i++) {
for (var j = 0; j < inputSize; j++) {
var pixel = image.getPixel(j, i);
buffer[pixelIndex++] = img.getRed(pixel);
buffer[pixelIndex++] = img.getGreen(pixel);
buffer[pixelIndex++] = img.getBlue(pixel);
}
}
return convertedBytes.buffer.asUint8List();
}
Or in the example app, the code is here
https://github.com/shaqian/flutter_tflite/blob/master/example/lib/main.dart#L173
Future recognizeImageBinary(File image) async {
var imageBytes = (await rootBundle.load(image.path)).buffer;
img.Image oriImage = img.decodeJpg(imageBytes.asUint8List());
img.Image resizedImage = img.copyResize(oriImage, 224, 224);
var recognitions = await Tflite.runModelOnBinary(
binary: imageToByteListFloat32(resizedImage, 224, 127.5, 127.5),
numResults: 6,
threshold: 0.05,
);
setState(() {
_recognitions = recognitions;
});
}
runModelOnBinary
is added in response to one user's request and thus it's implemented as the user requested it to be.
Thanks,
Qian
from flutter_tflite.
I'm archiving this thread. Feel free to reopen if you have further questions.
Thanks,
Qian
from flutter_tflite.
Related Issues (20)
- Do not support version greater than 3
- Can I build desktop app by this package?
- E/AndroidRuntime( 4563): Caused by: java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (StatefulPartitionedCall:0) with shape [1, 10647, 6] to a Java object with shape [1, 13, 13, 30]. HOT 1
- runModelOnImage returns null HOT 1
- I have a issue in runSegmentationOnImage
- The plugin `tflite` uses a deprecated version of the Android embedding. HOT 1
- The plugin `tflite` uses a deprecated version of the Android embedding. HOT 1
- got this error when running flutter project HOT 3
- Unhandled Exception: PlatformException(Failed to run model, Interpreter busy, java.lang.RuntimeException: Interpreter busy HOT 3
- Can anyone help giving this code in Flutter HOT 2
- help me please
- Failed to transform guice-5.1.0.jar (com.google.inject:guice:5.1.0) to match attributes HOT 1
- Please Update this package HOT 1
- crash when useGpuDelegate is true in v2 HOT 1
- E/AndroidRuntime(10989): Caused by: java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_conv2d_3_input:0) with 90000 bytes from a Java Buffer with 67500 bytes.
- URGENT!!!!! Please HELP!!! Unhandled Exception: PlatformException(Failed to run model, Attempt to invoke virtual method 'org.tensorflow.lite.Tensor org.tensorflow.lite.Interpreter.getInputTensor(int)' on a null object reference, java.lang.NullPointerException: Attempt to invoke virtual method 'org.tensorflow.lite.Tensor org.tensorflow.lite.Interpreter.getInputTensor(int)' on a null object reference
- URGENT!!!!! Please HELP!!! Unhandled Exception: PlatformException(Failed to run model, Attempt to invoke virtual method 'org.tensorflow.lite.Tensor org.tensorflow.lite.Interpreter.getInputTensor(int)' on a null object reference, java.lang.NullPointerException: Attempt to invoke virtual method 'org.tensorflow.lite.Tensor org.tensorflow.lite.Interpreter.getInputTensor(int)' on a null object reference
- Missing Plugin Exeption
- MissingPluginException(No implementation found for method loadModel on channel tflite) HOT 2
- Remove references to deprecated v1 Android embedding
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_tflite.