Run packages in isolate - Flutter - flutter

I'm trying to run some image processing and MLmodel prediction inside isolate for all images in the gallery device. But some packages like FaceDetector(from google_ml_kit), and PhotoGallery, don't let me run inside isolate. The question is, exist any possibility to run everything inside an isolate, like an entire package with these imports?
Without using isolate all the code is working and is kind of fast but the screen freezes when I start to load and process the images.
I also could process one by one image whitout freezing, but takes too much time.
So I m trying to run with isolate for example when a run this code:
void process(String path){
final options = FaceDetectorOptions(
performanceMode: FaceDetectorMode.accurate,
minFaceSize: .4,
);
final faceDetector = FaceDetector(options: options);
var file = File(path);
var image = InputImage.fromFile(file);
var bytes = file.readAsBytesSync();
var faces = (await faceDetector.processImage(image))
.map(
(e) => FaceRect(
e.boundingBox.left,
e.boundingBox.top,
e.boundingBox.width,
e.boundingBox.height,
),
)
.toList();
var faceImage = FacesImage(path, faces, bytes);
}
compute(process, "/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20221022-WA0006.jpeg");
I get this:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Binding has not yet been initialized.
E/flutter (32478): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter (32478): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding
But I can't call WidgetsFlutterBinding.ensureInitialized() in isolate and my app already have.
There is any alternative to find all photos and albuns that can be called in isolate? and faceDetector?
Run an entire package in isolate is a possibility?

Related

Getting learning_text_recognition library to work in Flutter

I'm trying to add the learning_text_recognition library to my Flutter project. I was able to get the example in the API docs to work with no problems (https://pub.dev/packages/learning_text_recognition/example), but now I'm trying to add it to my own project using the information found on the Readme tab of the same website. It's slightly different than how the example worked and I'm now receiving several errors that I didn't receive in the example. Specifically, the errors are on the following line:
RecognizedText result = await textRecognition.process(image);
It says that the await function can only be used in an async function, but I don't know if I should make the function or the class async? It also says that the method 'process isn't defined for the type 'TextRecognition', but I don't know what the method should be, since that part worked perfectly fine in the example. It was also complaining that image wasn't defined, but I just created a variable called image with InputCameraView, which seemed to work.
I've tried moving the code into a new function and made an image variable. This is what the code looks like now:
getInfo(){
var image = InputCameraView(
canSwitchMode: false,
mode: InputCameraMode.gallery,
title: 'Text Recognition',
onImage: (InputImage image) {
// now we can feed the input image into text recognition process
},
);
TextRecognition textRecognition = TextRecognition();
RecognizedText result = await textRecognition.process(image);
}
I've also included the following import statements:
import 'package:learning_input_image/learning_input_image.dart';
import 'package:learning_text_recognition/learning_text_recognition.dart';
import 'package:provider/provider.dart';
I'm not sure if I'm maybe missing a step?
Your function should have the async keyword to indicate that there will be wait points. See the dart async/await documentation.
Another detail for the example InputCameraView is a widget, it should not be inside the function. It must be using the onImage method of the InputCameraView to collect the recognition and the builder to build it. In the doc onImage calls the async function _startRecognition to collect the data you must do something in this line.
void getInfo() async {
var image = InputCameraView(
canSwitchMode: false,
mode: InputCameraMode.gallery,
title: 'Text Recognition',
onImage: (InputImage image) {
// now we can feed the input image into text recognition process
},
);
var textRecognition = TextRecognition();
var result = await textRecognition.process(image);
}

How can I asynchronously stream loaded objects from a list of futures in Dart

I have a list of objects that can be loaded by calling the function object.load().
This function returns a loaded version of the same object asynchronously.
I want call the load() funcion of all objects in a list at the same time and stream the loaded versions as soon as they finish loading.
The code below works but the program is loading one object at a time.
Sender:
Stream<ImageIconModel> streamLoadedIcons() async* {
for (var i = 0; i < imageIconModels.length; i++) {
yield await imageIconModels[i].load().then((loadedIconModel) {
return loadedIconModel;
});
}
}
Receiver:
await for (var loadedIcon in streamLoadedIcons()) {
final var result = doSomething(loadedIcon);
yield result;
}
The main problem is:
In the sender, if I await each load() call, it will do every step in the loop awaiting the load() to finish.
But if I remove the "await", I would be returning a future, not the loaded icon.
You need Stream.fromFutures.
final loadedIconModelStream = Stream.fromFutures([
for (final iconModel in imageIconModels) iconModel.load(),
]);
Both #hacker1024 an #pskink answers successfully answered my question!
But neither one worked as it supposed to and I think I discovered why.
I substituted the load() method for a Future.delayed(duration: random), and then the program worked as it intended to.
So what I think happened is that probably the lib I'm using to load the images (multi_image_picker: ^4.7.14) is accessing the phone files synchronously.
So even if I try to load every image at same time, it will do the task synchronously and return every image at the order I called them to load.
Thank you both for the answer!

Flutter Instantiate object on Startup

I am building an app which displays results it reads from a .json file. In that app I have a header-object, which stores the is the "table of contents"-object. In that object I have a list of other objects which are the results I want to display. I tried to just instantiate that object whenever I opened that page with calling Header().results.map(...);, but IMHO this is extremely dodgy and inelegant. When I tried to instantiate the object with var ToC = new Header(); before runApp(myApp); as follows:
void main() {
var ToC = new Header();
print(ToC);
runApp(
MaterialApp(
home: MyTabs(),
),
);
}
but when running ToC.results.map((result) => MeasCard(result: result)).toList() in the Dart file that should generate the List I just got the error message:
Undefined name 'ToC'
How do I instantiate such an object on app startup? Or is there another method to call the constructor just once?

Called OnResume Method From Activity to Fragment is not Updating Variables

I have an issue which I couldn't figure out for hours,
I have a fragments inside an activity, and sometimes I call the fragment with the codes below:
newsFeedFragment fragment = new newsFeedFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.hide(getSupportFragmentManager().findFragmentById(R.id.fragment_container));
fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag("notifications_fragment"));
fragmentTransaction.show(getSupportFragmentManager().findFragmentByTag("news_feed_fragment"));
fragmentTransaction.addToBackStack(null);
fragment.onResume();
fragmentTransaction.commit();
As the onResume is not called while showing the fragments I use "fragment.onResume();" in the code below. And when the fragment is shown, the onResume is called. However, I try to update a variable in the onResume method, but it is not updated with the code below. When ever the onResume is called, I see "1" as the result in the log, however I was expecting it to increase by 1 every time. Is there a way to make it work?
int refreshNotificationVar = 0; //in the main class
#Override
public void onResume(){
super.onResume();
refreshNotificationVar = refreshNotificationVar + 1;
System.out.println(refreshNotificationVar);
}
You cannot rely on instance variables in case of onPause and onResume; you can rely on static variables to some extent; you can use onSaveInstanceState; or use a Singleton class to store variable values; or store in shared preferences; or maybe store in a database depending on your needs. In your case, I would use a Singleton class to store the values and get/set them in onPause/onResume.

Closure call with mismatched arguments: function 'call'

I'm using the (2.0)js-interop library in combination with the JS library ImageLoaded and I'm stuck the FunctionProxy class because the code below throw the following error:
Breaking on exception: Closure call with mismatched arguments: function 'call'
js.FunctionProxy loaded = new js.FunctionProxy((){
print("called");
js.Proxy pckry = new js.Proxy(context.Packery, container, options);
});
js.Proxy img = new js.Proxy(context.imagesLoaded, container, loaded);
Which is weird because my js callback is called 5 times before the app crashes.
Looking at the Usage section of imagesLoaded it looks like the callback takes one parameter. So you have to add this parameter to your callback :
js.FunctionProxy loaded = new js.FunctionProxy((instance) {
print("called");
js.Proxy pckry = new js.Proxy(context.Packery, container, options);
});
js.Proxy img = new js.Proxy(context.imagesLoaded, container, loaded);
Additional notes :
You can avoid new js.FunctionProxy. There are only a limited number of cases where it's needed and your case here is not one of them.
imagesLoaded can be use as a function and it simplifies the code.
Thus, you should be able to use :
final img = context.imagesLoaded(container, (instance) {
print("called");
js.Proxy pckry = new js.Proxy(context.Packery, container, options);
});