0

I have a Flutter app that calls an object detector in CoreML via the iOS platform channel. I would like to ensure the model is getting the image in the correct format by viewing it in a Flutter widget.

I have access to the CVPixelBuffer data. How do I send it back to Flutter and display it in a widget?

Fred
  • 381
  • 5
  • 17

2 Answers2

0
import 'dart:ui' as ui;
import 'package:flutter_vision/flutter_vision.dart';

Future<ui.Image> convertPixelBufferToImage(CVPixelBuffer pixelBuffer) async {
  // Convert the CVPixelBuffer to a Flutter image object.
  ui.Image image = await FlutterVision.imageFromPixelBuffer(pixelBuffer);
  return image;
}
-1

Platform channels allow for two way communication. You're already done with half the task.

  1. Flutter -> Native

You must have already done this using something like

await platform.invokeMethod('sendDataToIOS',{'data':data});
  1. Native -> Flutter

     platform.setMethodCallHandler((call) async {
      if (call.method == 'receiveDataFromiOS') {
       final Map<String, dynamic> arguments = call.arguments;
       final String data = arguments['data'];
       receiveDataFromiOS(data);
     }});
    
  • How do I convert `CVPixelBuffer` to a format that I can display in a widget? This is where I'm getting stuck. Also, I just discovered using textures: https://api.flutter.dev/flutter/widgets/Texture-class.html Trying to figure that out too. – Fred Apr 16 '23 at 19:30
  • It’s good to know about Texture thanks for sharing Im not familiar with it . I’m posting a tiny function that will help you convert the CVPixelBuffer to an image on the flutter side . Try passing the CVPixelBuffer data using method channel to the flutter side. The use the flutter_vision package and the function I’ve provided . Please try it and let me know – Vipin Kumar Kashyap Apr 17 '23 at 06:56