简体   繁体   中英

Creating InputImageData from Uint8List or Image Object For Google ML Kit Face Detection

Ho do we create an InputImage if all we have is Uint8List or Image object? How do we create the Plane object needed to initialize the InputImageData ?

//imageFile is an XFile dataType (From Camera Package) 

Uint8List imageData = await imageFile.readAsBytes();

The link below from the Google ML Kit GitHub page doesn't describe how we can create an Input Image if we only have Uint8List object.

CREATING INPUT IMAGE

What you have named "imageData" in your code is not actually an ImageData. It's a list of bytes.

You could try this:

import 'package:google_ml_kit/google_ml_kit.dart';  
// The above should have been there in your qn! 
// We shouldn't have to find it ourselves to help you...

...

Uint8List bytes = await imageFile.readAsBytes();

... 

    InputImage inputImage = InputImage.fromBytes(
      bytes: bytes,
      inputImageData: InputImageData(/*put nothing here to get default values*/),
    );

There is also a .fromFile option. Since you already have the imageFile, why don't you use that? Like this:

  InputImage inputImage2 = InputImage.fromFile(imageFile);

Edit: This code runs fine, for me:

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';

class TodoeyStorage{
  static final Future<Directory> directory = getAppDirectory();
  static final Future<String> path = getAppPath(directory);

  static Future<Directory> getAppDirectory() async {
   print('Running getAppDirectory');
    Directory _directory = await getApplicationDocumentsDirectory();
    return _directory;
  }

  static Future<String> getAppPath(Future<Directory> _futureDirectory) async {
   print('Running getAppPath ');
    Directory _directory = await _futureDirectory;
    return _directory.path;
  }
}


class Screen extends StatelessWidget {
  const Screen({Key? key}) : super(key: key);

  Future func () async {
    final Future<String> appPath = TodoeyStorage.getAppPath(TodoeyStorage.directory);
    File themeFile = File('${await appPath}/themeFile.txt');
    FileMode mode = FileMode.write;
    themeFile.writeAsStringSync('hi\n', mode: mode);

    File imageFile = themeFile;
    print(imageFile);

    Uint8List bytes = await imageFile.readAsBytes();  // This line is from your code!

    InputImage inputImage = InputImage.fromBytes(
      bytes: bytes,
      inputImageData: InputImageData(/*put nothing here to get default values*/),
    );
    print('inputImage is $inputImage');
    return;
  }

  @override
  Widget build(BuildContext context) {
    func();
    return Center(child: Text('hi'));
  }
}

I get a black screen with the word "hi" on it, and the console prints:

I/flutter ( 5541): Running getAcuteDirectory
I/flutter ( 5541): Running getAcutePath 
I/flutter ( 5541): File: '/data/data/com.karolinadart.my_app/app_flutter/themeFile.txt'
I/flutter ( 5541): inputImage is Instance of 'InputImage'
I/flutter ( 5541): Running getAcutePath 
I/flutter ( 5541): File: '/data/data/com.karolinadart.my_app/app_flutter/themeFile.txt'
I/flutter ( 5541): inputImage is Instance of 'InputImage'

If I go to the declaration of InputImageData, it looks like this:

  InputImageData(
      {this.size,
      this.imageRotation,
      this.inputImageFormat = InputImageFormat.NV21});

Anybody had any luck doing this with out an image file. I find myself saving images to temp files to use them.

Ie image.dart image to input image from Google ml face detection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM