You are viewing limited content. For full access, please sign in.

Question

Question

Detect if the text orientation is different from the actual page orientation

SDK
asked on May 2, 2016 Show version history

Is there a way using the SDK to detect in which oriatation the text was captured in OCR when using the autorotate option? When we import document from import agent or when we use DCC to ocr document and we use the autorotate option in the ocr settings, the page is auto-rotated for the ocr process but not autorotated in Laserfiche.  If we highlight the text in the text pane in Laserfiche client it correcly highlight the text in the proper oriantation of the page.  Is there a flag that we could read to know if a page was rotated in the ocr process? Can we extract the page orientation from the WordLocation? Would there be another way to detect the page orientation using the Laserfiche SDK?

 

0 0

Answer

SELECTED ANSWER
replied on May 4, 2016

If found the solution (I'm using a script in the workflow to read the LOC(LocationsPagePart) and find the text orientaion) :

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports Laserfiche.RepositoryAccess

Namespace WorkflowActivity.Scripting.SDKScript
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits RAScriptClass100
        '''<summary>
        '''This method is run when the activity is performed.
        '''</summary>
        Protected Overrides Sub Execute()
           
            If Me.BoundEntryInfo.EntryType = EntryType.Document Then
                If Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).PageCount <> 0 Then
                Using PageReader as PageInfoReader = Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).GetPageInfos()
                    For Each pInfo As PageInfo In PageReader
                        Dim rectangle as New Laserfiche.RepositoryAccess.Common.LfRectangle(0,0,0,0)
                        Dim FirstTextLinePosition as Integer = 0
                        Dim LastTextLinePosition as Integer = 0
                        If pInfo.HasWordLocations Then
                        'Determine if page is portrait or landscape
                        If pInfo.ImageHeight>pInfo.ImageWidth Then
                            
                            'Read all rectangles on page and get FirstTextLinePosition and LastTextLinePosition
                            Using wlr As WordLocationsReader = pInfo.ReadLocationsPagePart()
                                For i as Integer = 1 to wlr.WordLocationCount
                                    rectangle = wlr.Read()
                                    If FirstTextLinePosition = 0 Then
                                        FirstTextLinePosition = rectangle.Bottom
                                    End If
                                
                                    LastTextLinePosition = rectangle.Bottom
                                Next
                            End Using
                            
                            'Are the line moving from the top down the page or up from the button
                            If FirstTextLinePosition>LastTextLinePosition Then
                                pInfo.ImageRotation = PageRotation.UpsideDown
                                pInfo.Save()
                            End If
                        
                            Else 
                            
                            'Read all rectangles on page and get FirstTextLinePosition and LastTextLinePosition
                            Using wlr As WordLocationsReader = pInfo.ReadLocationsPagePart()
                                For i as Integer = 1 to wlr.WordLocationCount
                                    rectangle = wlr.Read()
                                    If FirstTextLinePosition = 0 Then
                                        FirstTextLinePosition = rectangle.Right
                                    End If
                                    LastTextLinePosition = rectangle.Right
            
                                Next
                            End Using
                        
                            'Are the line moving from right to left or left to right
                            If FirstTextLinePosition>LastTextLinePosition Then
                                pInfo.ImageRotation = PageRotation.Counterclockwise
                                pInfo.Save()
                            Else
                                pInfo.ImageRotation = PageRotation.Clockwise
                                pInfo.Save()
                            End If
                        End IF
                    End IF
                    Next
                End Using
                End If
            End If
        End Sub
    End Class
End Namespace

 

0 0

Replies

replied on May 9, 2016 Show version history

Another approach, you can get the left/right and up/down word possission trends to determine what the orientation should be and then compare that with the pInfo.ImageRotationAngle.  If the angles don't match, then rotate to correct angle.

                                    For Each pInfo As PageInfo In PageReader
                                        If pInfo.HasWordLocations Then
                                            ' Get all word location data for the page
                                            Using WordLocations As WordLocationsReader = pInfo.ReadLocationsPagePart()
                                                Dim iTop As Integer = 0
                                                Dim iLeft As Integer = 0
                                                Dim UpDown As Integer = 0
                                                Dim LeftRight As Integer = 0
                                                Dim iRotation As Integer = 0
                                                ' Create a buffer to hold all location rectangles from page
                                                Dim AllRects(WordLocations.WordLocationCount - 1) As Common.LfRectangle
                                                ' Get all location rectangles from page
                                                WordLocations.Read(AllRects, 0, AllRects.Count)
                                                ' Process each rectangle
                                                For Each Rect As Common.LfRectangle In AllRects
                                                    ' Get possission trend (do rectangles move top to bottom or vise-versa)
                                                    If Rect.Top > iTop Then
                                                        UpDown = UpDown + 1
                                                    ElseIf Rect.Top < iTop Then
                                                        UpDown = UpDown - 1
                                                    Else
                                                        ' if Rect.Top = iTop then there is no up/down
                                                    End If
                                                    ' Save top value for comparison with next object
                                                    iTop = Rect.Top
                                                    ' Get possission trend (do rectangles move Left to Right or vise-versa)
                                                    If Rect.Left > iLeft Then
                                                        LeftRight = LeftRight + 1
                                                    ElseIf Rect.Left < iLeft Then
                                                        LeftRight = LeftRight - 1
                                                    Else
                                                        ' If Rect.Left = iLeft then there is no Left/Right
                                                    End If
                                                    ' Save Left value for comparison with next object
                                                    iLeft = Rect.Left
                                                Next
                                                ' Use possition movement trends to determin oriantation
                                                If UpDown > 0 Then
                                                    If LeftRight > 0 Then
                                                        'Image rotation = 0
                                                        iRotation = 0
                                                    ElseIf LeftRight < 0 Then
                                                        'Image rotation = 270
                                                        iRotation = 270
                                                    Else
                                                        ' Image rotation = 270
                                                        iRotation = 270
                                                    End If
                                                ElseIf UpDown < 0 Then
                                                    If LeftRight > 0 Then
                                                        ' Image rotation = 90
                                                        iRotation = 90
                                                    ElseIf LeftRight < 0 Then
                                                        ' Image rotation = 180
                                                        iRotation = 180
                                                    Else
                                                        ' Left/Right Trend = 0  - Too little text
                                                        ' Unknown image rotation (I think it is 90, but need to do more testing)
                                                        iRotation = 90
                                                    End If
                                                Else
                                                    ' Up/Down Trend = 0  - Too little text
                                                    If LeftRight > 0 Then
                                                        ' Unknown image rotation (I think it is 0, but need to do more testing)
                                                        iRotation = 0
                                                    ElseIf LeftRight < 0 Then
                                                        ' Image rotation = 180
                                                        iRotation = 180
                                                    Else
                                                        ' Unknown image rotation (I think it is 0, but need to do more testing)
                                                        iRotation = 0
                                                    End If
                                                End If
                                                ' Rotate image based on possission trends
                                                ' If image rotation does not match trend, rotate
                                                If pInfo.ImageRotationAngle <> iRotation Then
                                                    pInfo.ImageRotationAngle = iRotation
                                                    pInfo.Save()
                                                End If
                                            End Using
                                        End If
                                    Next

 

0 0
replied on May 11, 2016
If Me.BoundEntryInfo.EntryType = EntryType.Document Then
                If Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).PageCount <> 0 Then
                Using PageReader as PageInfoReader = Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).GetPageInfos()
                    For Each pInfo As PageInfo In PageReader
            
                        Dim iLeft As Integer = 0
                        Dim iTop As Integer = 0
                        Dim StartY As Integer = 0
                        Dim EndY As Integer = 0
                        Dim GoingUp As Integer = 0
                        Dim GoingDown As Integer = 0
                        
                        If pInfo.HasWordLocations Then
                        
                        Using WordLocations As WordLocationsReader = pInfo.ReadLocationsPagePart()
                        ' Create a buffer to hold all location rectangles from page
                        Dim AllRects(WordLocations.WordLocationCount - 1) As Laserfiche.RepositoryAccess.Common.LfRectangle
                        ' Get all location rectangles from page
                        WordLocations.Read(AllRects, 0, AllRects.Length)
                        'Determine if page is portrait or landscape
                        If pInfo.ImageHeight>pInfo.ImageWidth Then
                            
                            'Since the last rectangle is always the one at buttom of the page
                            If AllRects(1).Bottom>AllRects(WordLocations.WordLocationCount - 1).Bottom Then
                                pInfo.ImageRotation = PageRotation.UpsideDown
                                pInfo.Save()
                            End If

                        Else

                            'Read all rectangles and check the trend
                                For Each Rect As Laserfiche.RepositoryAccess.Common.LfRectangle In AllRects
                                    'Change of line
                                    If not ((Rect.X < iTop+3) and (Rect.X > iTop-3)) Then
                                        If StartY > EndY then
                                            GoingUp = GoingUp + 1
                                        Else
                                            GoingDown = GoingDown + 1
                                        End if
                                        StartY=Rect.Y
                                    
                                    End if
                                    iTop = Rect.X
                                    EndY = Rect.Y
                                Next
                              
                                If GoingUp < GoingDown Then
                                    pInfo.ImageRotation = PageRotation.Counterclockwise
                                    pInfo.Save()
                                Else
                                    pInfo.ImageRotation = PageRotation.Clockwise
                                    pInfo.Save()
                                End If
                        End IF
                    End Using
                    End IF
                    Next
                End Using
                End If
            End If

Laserfiche store the rectangle left to right then top to bottom, so the first rectangle always is at the top of the page and the last rectangle at the bottom of the page. If the page is rotated 180 then the first rectangle bottom position will be greather and the last rectangle bottom. If the page is rotated 90 or 270 then it's a bit more complicated because of the line of text (rectangle in line), it check the trend line by line. I got good result on a 100+ pages documents with random page orientation.

You are not allowed to follow up in this post.

Sign in to reply to this post.