3

I am trying to use google ml kit to process an image so i extract the left eye open probability, but it requires the input image to be either a file,bytes or file path, see below

final inputImage = InputImage.fromFile(file);

final inputImage = InputImage.fromBytes(bytes: bytes, inputImageData: inputImageData);

final inputImage = InputImage.fromFilePath(filePath);

it requires one of those above, i am trying to use a Camera image stream to achieve this,

 _cameraService.cameraController.startImageStream((image) async {
// i am trying to convert the image received here to be converted into either a File, Bytes, File path
}

3 Answers3

1

You can convert a CameraImage to a InputImage with the following function. But be aware: This code only works for the bgra8888 format which is single plane. yuv420 would be multi-plane and you would have to adjust the method where it requires Plane data. I have tested this with Flutter 3.7.7 on an iPhone 12 / iOS 16.1 simulator. I have set imageFormatGroup of CameraController to ImageFormatGroup.bgra8888. This code also assumes that the rotation of the image is zero degrees.

InputImage _convertCameraImageToInputImage(CameraImage cameraImage) {
    InputImageFormat? inputImageFormat;
    switch (cameraImage.format.group) {
      /*case ImageFormatGroup.yuv420:
        if (Platform.isAndroid) {
          inputImageFormat = InputImageFormat.yuv_420_888;
        }
        if (Platform.isIOS) {
          inputImageFormat = InputImageFormat.yuv420;
        }
        break;*/
      case ImageFormatGroup.bgra8888:
        inputImageFormat = InputImageFormat.bgra8888;
        break;
    }

    if (inputImageFormat == null) {
      throw Exception("InputImageFormat is null");
    }

    InputImagePlaneMetadata inputImagePlaneMetadata = InputImagePlaneMetadata(
        bytesPerRow: cameraImage.planes[0].bytesPerRow,
        height: cameraImage.planes[0].height,
        width: cameraImage.planes[0].width);
    InputImageData inputImageData = InputImageData(
        size: Size(cameraImage.width.toDouble(), cameraImage.height.toDouble()),
        imageRotation: InputImageRotation.rotation0deg,
        inputImageFormat: inputImageFormat,
        planeData: [inputImagePlaneMetadata]);
    return InputImage.fromBytes(
        bytes: cameraImage.planes[0].bytes, inputImageData: inputImageData);
  }

Resources:

AliceTheCat
  • 111
  • 1
  • 1
  • 8
0

ML Kit team doesn't own the flutter_mlkit repo, so please file issue at https://github.com/azihsoyn/flutter_mlkit/issues

zhouyi
  • 226
  • 1
  • 4
0
var bytes = <your_image_file_name>!.bytes;
String tempath = (await getTemporaryDirectory()).path;
File imgfile = File('$tempath/AddFile.png');
await imgfile.writeAsBytes(bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
final InputImage inputImage = InputImage.fromFile(imageFile!);

final TextRecognizer textDetector = GoogleMlKit.vision.textRecognizer();

final RecognizedText recognizedText = await textDetector.processImage(inputImage);   
final String text = recognizedText.text;
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 20 '23 at 05:31