Is there a way to detect size of Text with MLKit

Viewed 566

I have been using Firebase MLkit to detect text within an image and I needed a way to figure out how I can detect the size of the text. I realize Kotlin has a way to do it, but I am working in Java so I was looking for a way to do it in Java on android studio.

I imagine I would need to add something to my detect text method below:

private void detectTextFromImage()
    {
        FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);
        FirebaseVisionTextDetector firebaseVisionTextDetector = FirebaseVision.getInstance().getVisionTextDetector();
        firebaseVisionTextDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
            @Override
            public void onSuccess(FirebaseVisionText firebaseVisionText) {
                displayTextFromImage(firebaseVisionText);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(PatientActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

Thanks

1 Answers

I couldn't find a way built into ML kit to extract the text size but a workaround would be getting the frame of a Line or Element and calculating its height:

// Image recognition stuff...

List<FirebaseVisionText.TextBlock> blocks = visionText.getTextBlocks();

for (FirebaseVisionText.TextBlock textBlock : visionText.getTextBlocks()) {
    for (FirebaseVisionText.Line line : textBlock.getLines()) {
        Rect frame = line.getBoundingBox();
        float fontSize = frame.bottom - frame.top;

        // Do something with fontSize, e.g. add to a list
    }
}

Note that depending on the angle the image is taken at, some text may appear larger and hence will have a larger bounding box so this will only be accurate if the image is taken head-on to the text.

Hopefully this helps! (sorry I'm a bit late)

Related