Android Question MLKit TextRecognition and Text Orientation

Pitag

Member
Licensed User
Longtime User
For a project that i would like to create, i have to take some picture of paper with text that needs to be recognized.

I made some experiment with MLKit TextRecognition example (https://www.b4x.com/android/forum/threads/161210/#content) and it works very well.

One function i would like to get, is to detect if the text i rotated and, eventually, rotate the image to the correct alignment with the screen. No need to rotate image with few degrees of inclination, Images have to be rotated only if it has 45 degrees or more of inclination, for example.

On the MLkit documentation (at the page https://developers.google.com/ml-kit/vision/text-recognition/v2?hl=it), it's reported that the API returns also the degrees of inclination of the text recognized.

Is it possible to get that data in a B4A app and eventually rotate the image?
 

drgottjr

Expert
Licensed User
Longtime User
in the textRecognizer class at line 51, you will see: If task.RunMethod("isSuccessful", Null) Then
it's an If/End If block.
replace that block with this block:
B4X:
    If task.RunMethod("isSuccessful", Null) Then
        res.Success = True
        Dim Text As JavaObject = task.RunMethod("getResult", Null)
        res.Text = Text.RunMethod("getText", Null)
        ' to get the angle you have to iterate through all the lines...
    
        Dim textblocks As List = Text.RunMethod("getTextBlocks",Null)
        Log("blocks: " & textblocks.Size)
        Dim i As Int
        
        For i = 0 To textblocks.Size - 1
            Dim tb As JavaObject
            tb = textblocks.Get(i)
            Dim lines As List = tb.RunMethod("getLines",Null)
            Log("lines in this block: " & lines.Size)
            For Each line As JavaObject In lines
                Dim angle As Float = line.RunMethod("getAngle",Null)
                Log("angle: " & angle)
            Next
        Next
    Else
        Log(task.RunMethod("getException", Null))
    End If

this will return the angle of rotation for each line in each block of recognized text. this is the way mlkit does it. there is no global angle of rotation. you have to understand how mlkit deals with ocr. what you do with this angles is up to you. you could, for example, add them all up and take an average and then rotate your image accordingly. below find an example. i know nothing about mlkit text recognition for b4i; it could be the same as for b4a.
 

Attachments

  • rotation.txt
    2.9 KB · Views: 83
Upvote 0
Top