0

I am developing an Android app to detect text from the PDF file.

First, I tried to use Google Cloud Vision API. But it required to OAuth 2.0. So I changed from it to Firebase ML Kit.

But when I run 'fromFilePath' method, NPE occurred.

val file = getPdfFile()
Log.d(TAG, "file.length: ${file.length()}") // File size is printed correctly!

// NPE occurred while below code running
val image = FirebaseVisionImage.fromFilePath(context, Uri.fromFile(file))

// Because already NPE occurred, I cannot reach out to below code.
val detector = FirebaseVision.getInstance()
    .cloudDocumentTextRecognizer
Process: com.youknow.redact, PID: 13122
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

It looks like the Firebase ML kit doesn't support PDF file, right?

Is there any good solution?

Is it impossible to recognize text from the PDF file using Firebase ML kit?


I tried to test more file formats: JPG, TIFF

All is same, just input file is changed. JPG works fine, but TIFF has the same problem.

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
    at com.google.android.gms.internal.firebase_ml.zzox.zza(Unknown Source)
    at com.google.firebase.ml.vision.common.FirebaseVisionImage.fromFilePath(Unknown Source)
yoonhok
  • 2,575
  • 2
  • 30
  • 58

1 Answers1

2

TIFF is not an officially supported image format on Android. PDF is a document format and not an image format. Refer to the following link for list of all supported image formats: https://developer.android.com/guide/topics/media/media-formats#image-formats

[UPDATE] Understood what the problem of OP is now. Firebase ML Kit support two types of text recognition:

  1. text in an image of city or landscape (like signs in a photo of a street)
  2. text in an image of a document

What OP wants is to recognize the text in a PDF "document" and this is not supported.

I think OP misunderstood what document meant in the context of ML Kit.

To recognize text from a PDF file you would need to use 3rd party library to convert the PDF to a bitmap first.

Alan
  • 667
  • 5
  • 15
  • Thanks for your answer here Alan. You're indeed correct that ML Kit can only work on images. The reference to "document" is more about the amount of text in the image. Cloud based ML can for example extract text from an picture of a page of a book (for example [here](https://firebase.google.com/docs/ml-kit/recognize-text#document_text)), while the on-device model can only extra more sparse text (as shown [here](https://firebase.google.com/docs/ml-kit/recognize-text#sparse_text)). – Frank van Puffelen Mar 10 '19 at 16:56