google_ml_kit - how to force download of text recognition module at compile?

Viewed 499

I am new to Flutter, trying to write a simple module to pick an image and pass it to google_ml_kit for on device OCR text recognition only. I have set the Android min SDK version to 21 and updated the build.gradle to use FileNotFoundException. Image pick works fine, but when I pass the picked image file to the ml_kit function, I get a set of errors that seem to indicate the OCR module is missing and needs to be downloaded. Is there a way to force this to happen at compile time?

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): Local module descriptor class for com.google.android.gms.vision.ocr not found. I/DynamiteModule( 8758): Considering local module com.google.android.gms.vision.ocr:0 and remote module com.google.android.gms.vision.ocr:0 E/Vision ( 8758): Error loading module com.google.android.gms.vision.ocr optional module true: gv: No acceptable module found. Local version is 0 and remote version is 0. I/Vision ( 8758): Request download for engine ocr D/skia ( 8758): Shader compilation error D/skia ( 8758): ------------------------ D/skia ( 8758): Errors: D/skia ( 8758): D/skia ( 8758): Shader compilation error D/skia ( 8758): ------------------------ D/skia ( 8758): Errors: D/skia ( 8758): D/skia ( 8758): Shader compilation error D/skia ( 8758): ------------------------ D/skia ( 8758): Errors: D/skia ( 8758): D/skia ( 8758): Shader compilation error D/skia ( 8758): ------------------------ D/skia ( 8758): Errors: D/skia ( 8758): D/TransportRuntime.SQLiteEventStore( 8758): Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct D/TransportRuntime.JobInfoScheduler( 8758): Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning... D/TransportRuntime.SQLiteEventStore( 8758): Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct D/TransportRuntime.JobInfoScheduler( 8758): Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning... I/flutter ( 8758): com.google.mlkit.common.MlKitException: Waiting for the text recognition module to be downloaded. Please wait.

2 Answers

I solved this by changing the emulator in Android Studio to use a different emulator with the latest version of Play Store enabled (not all the images in the AVD manager have these)enter image description here

As you can see in this link https://developers.google.com/ml-kit/migration/android#new, only beta version of Text Recognition is working without need to download something. So I would encourage you to make the google_ml_kit package local to your flutter project and replace the text recognition implementation with

implementation 'com.google.mlkit:text-recognition:16.0.0-beta1'

then that error won't appear in toast message.

Related