Comments (4)
Ok, I have realized that in order to make this work I need to scale values from original float32 buffer to 0-255 using floor((x+1)*127.5). This works and it is quite fast. If you want, I will extend this internal helper and prepare utility method for handling float32 buffers.
class ImageConversion {
static Image convertTensorBufferToImage(TensorBuffer buffer, Image image) {
// if (buffer.getDataType() != TfLiteType.uint8) {
// throw UnsupportedError(
// "Converting TensorBuffer of type ${buffer.getDataType()} to Image is not supported yet.",
// );
// }
List<int> shape = buffer.getShape();
TensorImage.checkImageTensorShape(shape);
int h = shape[shape.length - 3];
int w = shape[shape.length - 2];
if (image.width != w || image.height != h) {
throw ArgumentError(
"Given image has different width or height ${[
image.width,
image.height
]} with the expected ones ${[w, h]}.",
);
}
List<double> rgbValues = buffer.getDoubleList();
assert(rgbValues.length == w * h * 3);
for (int i = 0, j = 0, wi = 0, hi = 0; j < rgbValues.length; i++) {
int r = ((rgbValues[j++] + 1) * 127.5).floor();
int g = ((rgbValues[j++] + 1) * 127.5).floor();
int b = ((rgbValues[j++] + 1) * 127.5).floor();
image.setPixelRgba(wi, hi, r,g, b);
wi++;
if (wi % w == 0) {
wi = 0;
hi++;
}
}
return image;
}
from tflite_flutter_helper.
Glad to hear that your problem got resolved. Please open a pull request if you want to contribute. That would be very helpful
from tflite_flutter_helper.
Temporarily duplicate this function https://github.com/am15h/tflite_flutter_helper/blob/master/lib/src/image/image_conversions.dart#L10-L46 (Only for your particular use case right now) you can remove the
if (buffer.getDataType() != TfLiteType.uint8) {
throw UnsupportedError(
"Converting TensorBuffer of type ${buffer.getDataType()} to Image is not supported yet.",
);
}
and things will work fine for you if your float data is between 0.0-255.0.
I will push an update for float values after generalizing with corner-cases.
from tflite_flutter_helper.
Thanks for the answer. I appreciate your effort to help me with this. I have tried proposed approach.
If I got it right, I should
imageLib.Image _target = imageLib.Image(c.INPUT_SIZE,c.INPUT_SIZE);
_lastProcessedImage = ImageConversion.convertTensorBufferToImage(inputImage.getTensorBuffer(),_target);
It works fine, no exceptions raised, however I am getting just black square when I display it like Image.memory(imageLib.encodeJpg(_lastProcessedImage)
.
I have noticed that r, g, and b values are always -1,0 or 1 after List<int> rgbValues = buffer.getIntList()
. In original buffer, there are also values between -1.0 and 1.0, which is inline what model consumes. So it seems that getIntList just floors these values. So it makes no sense to use this as RGB values, right?
from tflite_flutter_helper.
Related Issues (20)
- Precondition failed when attempting to use with a custom model HOT 11
- ImageProcessor doesn't run through tasks during process since 0.3.1 HOT 1
- install.bat didn't wok
- squeeze TensorBuffer HOT 1
- preNormalize question
- LateInitializationError: Field 'bertQuestionAnswerer' has not been initialized. HOT 1
- Model doesnt work with the plugin HOT 1
- Flutter 3.0 error HOT 16
- Error when trying to use the tflite_flutter_helper package HOT 12
- Cannot find an axis to label. A valid axis to label should have size larger than 1.
- Issue while debugging.
- Bad state: failed precondition with TensorFlow Lite v3
- Get exception in interpreter.run
- not compatible with ffi 2.0.1. HOT 2
- TensorLabel from tflite file
- _outputBuffer.getDoubleList() value is low
- Could not find method compile() for arguments
- Upgrade Image Dependency HOT 15
- Why package is DISCONTINUED ? HOT 5
- A value of type 'ImageData?' can't be assigned to a variable of type 'List<int>?'. Try changing the type of the variable, or casting the right-hand type to 'List<int>?'
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 tflite_flutter_helper.