top of page

Integrating and using ML Kit with Flutter


Integrating and using ML Kit with Flutter

Google ML Kit is a powerful set of Flutter plugins that allows developers to incorporate machine learning capabilities into their Flutter apps. With ML Kit, you can leverage various machine learning features, such as text recognition, face detection, image labeling, landmark recognition, and barcode scanning.


In this blog post, we will guide you through the process of integrating and using ML Kit with Flutter. We'll demonstrate the integration by building a simple app that utilizes ML Kit to recognize text in an image.


Prerequisites


Before we get started, make sure you have the following:

  • A Flutter development environment set up

  • Basic understanding of Flutter framework

  • A Google Firebase project (ML Kit relies on Firebase for certain functionalities)

Now, let's dive into the steps for integrating and using ML Kit with Flutter.


Step 1: Add the dependencies


To begin, we need to add the necessary ML Kit dependencies to our Flutter project. Open the pubspec.yaml file in your project and include the following lines:

dependencies:google_ml_kit: ^4.0.0

Save the file and run flutter pub get to fetch the required dependencies.


Step 2: Initialize ML Kit


To use ML Kit in your Flutter app, you need to initialize it first. This initialization process is typically done in the main() function of your app. Open the main.dart file and modify the code as follows:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  initMLKit();
  runApp(MyApp());
}

The initMLKit() function is a custom function that we'll define shortly. It handles the initialization of ML Kit. The WidgetsFlutterBinding.ensureInitialized() line ensures that Flutter is initialized before ML Kit is initialized.


Step 3: Create a text recognizer


Now, let's create a text recognizer object. The text recognizer is responsible for detecting and recognizing text in an image. Add the following code snippet to the main.dart file:

TextRecognizer recognizer = TextRecognizer.instance();

The TextRecognizer.instance() method creates an instance of the text recognizer.


Step 4: Recognize text in an image


With the text recognizer created, we can now use it to recognize text in an image. To achieve this, call the recognizeText() method on the recognizer object and pass the image as a parameter. Update the code as shown below:

List<TextBlock> textBlocks = recognizer.recognizeText(image);

Here, image represents the image on which you want to perform text recognition. The recognizeText() method processes the image and returns a list of TextBlock objects. Each TextBlock represents a distinct block of recognized text.


Step 5: Display the recognized text


Finally, let's display the recognized text in our app. For the sake of simplicity, we'll print the recognized text to the console. Replace the placeholder code with the following snippet:

for (TextBlock textBlock in textBlocks) {
  print(textBlock.text);
}

This loop iterates through each TextBlock in the textBlocks list and prints its content to the console.


Complete code


Now that we've covered all the necessary steps, let's take a look at the complete code for our Flutter app:

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  initMLKit();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ML Kit Text Recognition',
      home: Scaffold(
        appBar: AppBar(
          title: Text('ML Kit Text Recognition'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                height: 200,
                width: 200,
                child: Image.asset('assets/image.jpg'),
              ),
              Text('Recognized text:'),
              Text('(Will be displayed here)')
            ],
          ),
        ),
      ),
    );
  }
}

void initMLKit() async {
  await TextRecognizer.instance().initialize();
}

This code defines a basic Flutter app with a simple UI. When the app runs, it displays an image and a placeholder for the recognized text.


Running the app


To run the app, you can build and run it from your preferred Flutter development environment. Once the app is running, tap on the image to initiate text recognition. The recognized text will be printed to the console.


Conclusion


Congratulations! In this blog post, we walked you through the process of integrating and using ML Kit with Flutter. We built a simple app that utilizes ML Kit to recognize text in an image. You can use this tutorial as a starting point to develop your own ML Kit-powered apps.


For more in-depth information on ML Kit and its capabilities, please refer to the official ML Kit documentation: https://developers.google.com/ml-kit/.


Feel free to experiment with different ML Kit features and explore its vast potential in your Flutter apps.


Happy coding!

Comments


Blog for Mobile App Developers, Testers and App Owners

 

This blog is from Finotes Team. Finotes is a lightweight mobile APM and bug detection tool for iOS and Android apps.

​

In this blog we talk about iOS and Android app development technologies, languages and frameworks like Java, Kotlin, Swift, Objective-C, Dart and Flutter that are used to build mobile apps. Read articles from Finotes team about good programming and software engineering practices, testing and QA practices, performance issues and bugs, concepts and techniques. 

Monitor & Improve Performance of your Mobile App

 

Detect memory leaks, abnormal memory usages, crashes, API / Network call issues, frame rate issues, ANR, App Hangs, Exceptions and Errors, and much more.

Explore Finotes

bottom of page