【问题标题】:google_ml_kit - how to force download of text recognition module at compile?google_ml_kit - 如何在编译时强制下载文本识别模块?
【发布时间】:2021-11-27 09:35:47
【问题描述】:

我是 Flutter 的新手,正在尝试编写一个简单的模块来选择图像并将其传递给 google_ml_kit,仅用于设备上的 OCR 文本识别。我已将 Android 最小 SDK 版本设置为 21,并将 build.gradle 更新为使用 FileNotFoundException。图像选择工作正常,但是当我将选择的图像文件传递给 ml_kit 函数时,我收到一组错误,似乎表明 OCR 模块丢失并且需要下载。有没有办法强制在编译时发生这种情况?

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:google_ml_kit/google_ml_kit.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Image Selector with OCR',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key ?key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File? txtImage;
  String? imageString;

  Future getImage()async{
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);
      if (image == null) return;
      final imageFile = File(image.path);
      final ocrText = await readText(imageFile);
      setState(()=>this.imageString = ocrText);
      setState(()=>this.txtImage = imageFile);
    } on PlatformException catch(e){print('error'+e.message.toString());}
  }

  Future readText(_file)async{

    try {
      final inputImage = InputImage.fromFile(_file);
      final textDetector = GoogleMlKit.vision.textDetector();
      final RecognisedText recognisedText = await textDetector.processImage(
          inputImage);
      print(recognisedText);
      return (recognisedText);
    } on PlatformException catch(e){print(e.message.toString());}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Reading Assistant'),
      ),
      body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,
        children: [
          txtImage != null ? Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: [
            Image.file(txtImage!),Text('OCR:')]) : Text('select an image'),
        ],
      )),

      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: Stack(
        fit: StackFit.expand,
        children: [
          Positioned(
            right: 20,
            bottom: 10,
            child: FloatingActionButton(
              heroTag: 'pick',
              onPressed: () {getImage();},
              child: Icon(
                Icons.photo_album_outlined,
                size: 40,
              ),
            ),
          ),
          Positioned(
            bottom: 10,
            right: 90,
            child: FloatingActionButton(
              heroTag: 'camera',
              onPressed: () {/* TODO implement camera later */},
              child: Icon(
                Icons.camera,
                size: 40,
              ),
            ),
          ),
        ],
      ),


    );;
  }
}

W/DynamiteModule(8758):未找到 com.google.android.gms.vision.ocr 的本地模块描述符类。 I/DynamiteModule(8758):考虑本地模块 com.google.android.gms.vision.ocr:0 和远程模块 com.google.android.gms.vision.ocr:0 E/Vision(8758):加载模块 com.google.android.gms.vision.ocr 可选模块时出错,true:gv:未找到可接受的模块。本地版本为 0,远程版本为 0。 I/Vision (8758):请求下载引擎 ocr D/skia (8758):着色器编译错误 d/skia(8758):------------ D/skia (8758):错误: d/skia (8758): D/skia (8758):着色器编译错误 d/skia(8758):------------ D/skia (8758):错误: d/skia (8758): D/skia (8758):着色器编译错误 d/skia(8758):------------ D/skia (8758):错误: d/skia (8758): D/skia (8758):着色器编译错误 d/skia(8758):------------ D/skia (8758):错误: d/skia (8758): D/TransportRuntime.SQLiteEventStore(8758): 存储优先级 = VERY_LOW、名称 = FIREBASE_ML_SDK 的事件,用于目标 cct D/TransportRuntime.JobInfoScheduler(8758):已安排上传上下文 TransportContext(cct,VERY_LOW,MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc)。返回... D/TransportRuntime.SQLiteEventStore(8758): 存储优先级 = VERY_LOW、名称 = FIREBASE_ML_SDK 用于目标 cct 的事件 D/TransportRuntime.JobInfoScheduler(8758):已安排上传上下文 TransportContext(cct,VERY_LOW,MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc)。返回... I/flutter (8758): com.google.mlkit.common.MlKitException: 等待下载文本识别模块。请稍候。

【问题讨论】:

    标签: flutter google-mlkit


    【解决方案1】:

    我通过将 Android Studio 中的模拟器更改为使用启用了最新版本 Play 商店的不同模拟器解决了这个问题(并非 AVD 管理器中的所有图像都有这些)

    【讨论】:

      【解决方案2】:

      正如您在此链接https://developers.google.com/ml-kit/migration/android#new 中看到的,只有文本识别的测试版可以工作,无需下载任何东西。因此,我鼓励您将 google_ml_kit 包本地化到您的 Flutter 项目中,并将文本识别实现替换为

      实现 'com.google.mlkit:text-recognition:16.0.0-beta1'

      那么该错误将不会出现在 toast 消息中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-22
        • 1970-01-01
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 2020-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多