Android Text Recognizer Using Camera and Firebase ML Kit

Save

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 : –>

ResultAdapter :–>

camera_result_item (Xml File) : –>

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 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


29 comments

  • 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.
      • Irjavan
        camera_simple_spinner_item layout is absent, also I see the black screen instead of camera
        • anchit (Moderator)
          Dear Irjavan,
          the camera_simple_spinner_item layout missing is something used in the namespace tools, you can easily remove that line or replace that layout with the camera_result_item whose code is shared in the blog itself.

          Regarding the black screen on the camera, for this the issue is that the CameraSourcePreview class is not initiating correctly in your code. Please do debug that code or share your code with us and we will help you with the same.

          Hope this helps you.

  • 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 (Moderator)
      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 (Moderator)
      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 (Moderator)
          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 (Moderator)
      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 (Moderator)
          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 (Moderator)
      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 (Moderator)
      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 (Moderator)
      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

      • Josh
        i got error in resultadapter, how can i resolve that?
        • anchit (Moderator)
          Hello josh,

          What exactly is the error you are facing?

          Please do share some insight about the error so that I can look into it & help you.

          • Richard
            Same problem. ResultAdapter is not defined.
            • anchit (Moderator)
              Hello Richard,

              ResultAdapter is just a simple recycler view Adapter class in which I have just inflated a text view and a background(just to make the views presentable).
              I have also mentioned the same in the comment along with the declaration of the ResultAdapter object.

              Still, if you face any issue, then do let me know.

              • Khalis Ramni
                Hi Anchit,

                Can you share the ResultAdapter code here?

                • anchit (Moderator)
                  I have added the same in the last section of the Blog.

                  Please do have a look.

  • Khalis Ramni
    Hi Anchit,

    Can you share the ResultAdapter code here?

    • anchit (Moderator)
      Ok,

      I have added the same in the last section of the Blog.

      Please do have a look.

  • css.php

    Excellent work, fast, good quality and understood the brief perfectly! Quick responses developing the project and very good cooperation. I suggest to anyone.

    Stathis Plakidas

    Stathis Plakidas

    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home