首页 > 解决方案 > 如何使用 ML Kit 云文本识别器进行颤振?

问题描述

我在我的项目中使用包“firebase_ml_vision”来进行 OCR。我可以很好地阅读基于拉丁语的语言,但是,我想阅读汉字。我知道文本识别器在设备和基于云的版本上有。但是,我不知道如何在我的应用程序中“启用”基于云的版本。我已经在 Firebase 中激活了基于云的 API,如下图所示:已激活的云 API

我目前使用的代码是:

void _initializeVision() async{
final File imageFile = File(imagePath);
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);


final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();
final VisionText visionText = await textRecognizer.processImage(visionImage);



for(TextBlock blocks in visionText.blocks){
  for(TextLine line in blocks.lines){
    print(line.text);
  }
}}

我尝试阅读的图像

结果:

I/flutter (10432): FamilyMart Collection
I/flutter (10432): 10
I/flutter (10432): Pocket facial tissue
I/flutter (10432): Without fluorescent virgin fber from wood puip
I/flutter (10432): pampers your skin

谁能向我解释如何使用 Flutter 的云文本识别器?

标签: firebasedartfluttergoogle-cloud-visionfirebase-mlkit

解决方案


有同样的问题,不要认为 cloud-OCR 目前可以与 ML-Package 一起使用。我设法通过 POST 请求使其工作。以下是您需要的一切:发出 Vision API 请求

// Upload Image to Firebase and get
// 1. DownloadUrl or 
// 2. StorageBucket or
//
// 3. Convert Image to base64 with
// String base64Image = base64Encode(File(imagePath).readAsBytesSync());
// (does not work for me, if you use this way make sure your `body` is correct)

String body = """{
  'requests': [
    {
      'image': {
        'source': {
          'imageUri': '$downloadUrl'
        }
      },
      'features': [
        {
          'type': 'DOCUMENT_TEXT_DETECTION'
        }
      ]
    }
  ]
}""";

http.Response res = await http
  .post(
    "https://vision.googleapis.com/v1/images:annotate?key=$API_KEY",
    body: body
  );

print("${res.body}");

推荐阅读