簡體   English   中英

從 Uint8List 或圖像 Object 創建用於 Google ML Kit 人臉檢測的 InputImageData

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

如果我們只有Uint8ListImage object,我們如何創建一個InputImage 我們如何創建初始化 InputImageData 所需的平面InputImageData

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

Uint8List imageData = await imageFile.readAsBytes();

下面來自 Google ML Kit GitHub 頁面的鏈接沒有描述如果我們只有 Uint8List object 我們如何創建輸入圖像。

創建輸入圖像

您在代碼中命名的“imageData”實際上並不是 ImageData。 這是一個字節列表。

你可以試試這個:

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*/),
    );

還有一個.fromFile選項。 既然你已經有了 imageFile,為什么不使用它呢? 像這樣:

  InputImage inputImage2 = InputImage.fromFile(imageFile);

編輯:這段代碼運行良好,對我來說:

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'));
  }
}

我得到一個黑屏,上面寫着“hi”,控制台打印:

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'

如果我對 InputImageData 的聲明是 go,它看起來像這樣:

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

如果沒有圖像文件,任何人都可以幸運地做到這一點。 我發現自己將圖像保存到臨時文件以使用它們。

即image.dart image to input image from Google ml人臉檢測

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM