Read complete story

Android Text Recognizer Using Camera and Firebase ML Kit

In this blog, we will learn about implementing a Text Recognizer Using Camera and Firebase ML Kit.

With the updated firebase release, the developers have released new powers to image processing in a very easy and resource-friendly way.

Now, If you want you can use image processing and machine learning techniques in your application very easily using Firebase ML kit.

ML Kit is a mobile SDK that brings Google’s machine learning expertise to Android and iOS apps in a powerful yet easy-to-use package. Whether you’re new or experienced in machine learning, you can implement the functionality you need in just a few lines of code. There’s no need to have deep knowledge of neural networks or model optimization to get started. On the other hand, if you are an experienced ML developer, ML Kit provides convenient APIs that help you use your custom TensorFlow Lite models in your mobile apps.

— Firebase Developer Guide

Well, the firebase ML kit contains 5 options currently, which are :

  1. Recognize Text
  2. Face Detection
  3. Barcode Scanning
  4. Label images
  5. Recognize Landmarks

We will currently focus on how we can recognize the text using the camera of an Android device.

Just a precap of what you will be able to do after reading this blog :

 

 

Things you need to get started :

  1. Android Studio 3.1(or a newer version).
  2. Google Account to link firebase.

With these, you are good to go.

Before you start coding :

After these initial steps, you are good to start writing code for the text recognizer.

APPROACH

CODE

I have named my Activity as LauncherActivity.

Xml File :->

 

Java Class File( LauncherActivity ) : ->

TextRecognitionProcessor : –>

Some other demos :

 

 

 

Sources :

https://firebase.google.com/docs/ml-kit/android/recognize-text

https://github.com/firebase/quickstart-android/tree/master/mlkit/

 

Keep coding and Keep Sharing 🙂

. . .

Comments (19)

Add Your Comment

  • Farooq Arshed
    Your code is incomplete. Can you provide a link to final version?
    • anchit
      Dear Farooq,
      The code in the blog is a complete reference.
      Please do let me know which part in the blog you think is incomplete or you are unable to link with.
      I will try to improvise that part of the blog so that it helps all the readers.
  • Lucas
    Hi, I tried to apply the tutorial above but I could not. Could you just make the above demo code available in a github repository? Thanks!
    • anchit
      Hi Lucas,
      Thanks for your comment, but currently i cannot put this code on Github repository.

      You can ask me over here the issue you are facing and I will try to reply as soon as possible for me.

  • Sultan
    Hi, Thanks for the tutorial its really helping one.I have an issue. in LauncherActivity.java how did you call updateSpinnerFromTextResults(FirebaseVisionText textresults) method.help appreciated.
    • anchit
      For calling method “updateSpinnerFromTextResults(FirebaseVisionText textresults) ” of the Launcher Activity, you will need to look where exactly you have recieved the results from Firebase.

      For Example, i have recieved the results in the method “onSuccess” of TextRecognitionProcessor.java which is a child of VisionProcessorBase

      I personally modified the constructor of TextRecognitionProcessor.java and passed the instance of Launcher activity in the constructor.

      You can also implement the same using interface as well.

      If you still have any doubts, do let me know, i will help you as much as i can.

      • Sultan
        Thanks anchit. I have already did it using interface.
        • anchit
          That Sound’s Great.
      • Kingsley
        Thanks anchit, appreciate the work on this, I have the official example working,

        Can you post the code on how to do this (including the call back to the Launcher.
        “I personally modified the constructor of TextRecognitionProcessor.java and passed the instance of Launcher activity in the constructor.”
        thanks heaps
        Kingsley

  • Kingsley
    Hi anchit, thanks for the tutorial,
    Can you post the code snippets on how you did this?

    “I personally modified the constructor of TextRecognitionProcessor.java and passed the instance of Launcher activity in the constructor.”

    including the call back to the Launcher method. thanks heaps

    • anchit
      Hi Kingsley,

      The constructor of TextRecognitionProcessor.java will be like :
      public TextRecognitionProcessor(LauncherActivity activity) {
      detector = FirebaseVision.getInstance().getVisionTextDetector();
      activityInstance = activity;
      }

      Now in this your onSuccess method will be something like this :

      @Override
      protected void onSuccess(
      @NonNull FirebaseVisionText results,
      @NonNull FrameMetadata frameMetadata,
      @NonNull GraphicOverlay graphicOverlay) {
      graphicOverlay.clear();
      activityInstance.updateSpinnerFromTextResults(results);
      }

      I am also adding this file in the article as well, so that you can refer it completely.

      If still you have any confusions, please feel free to ask.

      • Kingsley
        Thankyou so much anchit, that was the hint I needed,
        appreciate the quick update too.
        • anchit
          Well, Thanks to you for making this article better.
  • Ahmed Ibrahim
    App is showing a black screen in camera preview even though I have added the permissions in manifest file. can you help please ?
    • anchit
      Well without seeing code it is hard to tell what exactly is the reason for your problem.

      But the problem you are stating is most probably due to improper use of CameraSourcePreview Object.

      Please cross check that before you access the camera have you initialized the camera source and then started it in on resume of your activity.

  • Mattia
    I receive ‘fail to connect to camera service’. How can i fix this?
    • anchit
      Hi there,

      Please do check first if you have added the camera permission in your manifest file and are also checking for it as per the android api level.

  • Vinod Gaur
    Hi Anchit,

    Can you share me the code as i need it as very urgent basis and the MLKIT library is outdated.

    • anchit
      Hello Vinod,

      Yes, the MLKIT library dependency mentioned in the blog is outdated, but then this is quite a common thing for firebase dependencies. You can still use this version if you want or even update, it is completely up to you and your use case.

      Secondly, the blog in here is a complete reference to tell you how you can use this functionality.
      I do not have any code other than the files shared in the blog.

      Still, If you want you can share your code and I will try to help you out

  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close