Showing posts with label OCR. Show all posts
Showing posts with label OCR. Show all posts

Thursday, May 13, 2021

Cognitive Services: OCR vs Analyze Layout vs Analyze Invoice vs Analyze Forms

Recently I was required to do some analysis on some of the APIs offered within the Azure Cognitive Services suite.  Initially the APIs that were going to be analyzed were the following listed below.  However one of them was later dropped due to the reasons that will be provided later.

  • OCR
  • Analyze Layout (v2.0)
  • Analyze Layout (v2.1 preview)
  • Analyze Invoice (v2.1 preview)
  • Analyze Forms (v2.0)
  • Analyze Forms (v2.1 preview)

When performing this analyses, a set of 8 different documents was used.

OCR vs Analyze Layout (v2.0)
  • The OCR engine used within Analyze Layout is different from the one offered by the OCR API.  This conclusion was made as on one particular instance some specific text was OCRed differently
  • When using scanned documents, Analyze Layout seemed to pick-up noise which adds no value
  • On a couple of documents the coordinates for data extracted between the two APIs were completely different
  • On both APIs, text which spans on multiples lines gets extracted as different lines
  • OCR doesn't produce any structure to define table data
  • The JSON result produced by the Analyze Layout includes a new section pageResults.  This is being used to define table structured data
  • On some occasions, Analyze Layout did extract table structured data but which do not map to any table structure
  • On some occasions, Analyze Layout did extract only parts of a table
  • When used a document with 2 pages, a table structure was identified on page 2 but not on page 1

OCR vs Analyze Layout (v2.1)
  • The OCR engine used within Analyze Layout is different from the one offered by the OCR API.  This conclusion was made as on one particular instance some specific text was OCRed differently.  In fact the value produced was also different from the value produced via v2.0
  • On both APIs, text which spans on multiples lines gets extracted as different lines
  • OCR doesn't produce any structure to define table data
  • The JSON result produced by the Analyze Layout includes a new section pageResults.  This is being used to define table structured data
  • On different number of occasions, Analyze Layout was capable of extracting table structure related data
  • When used a document with multiple pages, table structure data was extracted on both pages
  • On one specific document, a particular symbol was identified as a selection mark (looks to be the identification of a checkbox)
  • On some instances within the table structure data, related text on multiple lines was being amalgamated
  • There seems to be the introduction of appearance metadata related to the data being extracted.  However, this looked to be static as the same style was observed across all the data

Analyze Invoice (v2.1)
  • The JSON result produced excludes the lines section that is included in the OCR and Analyze Layout APIs
  • The JSON result produced includes a new section pageResults.  This is being used to define table structured data.  Compared to the Analyze Layout APIs, this is excluding references to lines information
  • The  JSON result produced includes a new section documentResults to help classifying content within the related document
  • The classification data consists of a key value pair for single matches, but is capable of classifying table structure data

Analyze Forms (v2.1)
To analyze this API, the use of the FOTT tool was made use.  This simplifies the generation of JSON messages to be used for the respective APIs.  To understand how this tool work, the steps within this video were followed: Steps to use FOTT tool.

It is being assumed / concluded that Analyze Invoice is making use of Analyze Forms, but with already pre-trained data.  Similarly there are some already other existing APIs to cater for Business Cards, etc.

Analyze Forms (v2.0)
This API wasn't analyzed as the FOTT tool doesn't support this version.  Hence, the generation of JSON messages manually would have been a complex task.  Also, there would have been a chance that the results would be similar or worse to the results produced within v2.1
 

Monday, March 29, 2021

Brute force for best fit cell alignment

Problem Definition
After doing text extraction by using an OCR library, we try to find and highlight useful information.  In the following diagram, the useful information is being highlighted with a green border.  As it can be seen in the diagram the date highlighted exists more than once, but it is not aligned with the other information that were are interested into and hence producing a false positive match.

The above problem has been simplified as shown in the next diagram.  In this problem, we need to find the row which contains the most different number of colors.  Should a cell color exist in the document but not on the final row to be identified, the first occurrence of that cell color would be considered as the correct value.  Should the final row have two or more cells of the same color, only the first occurrence of that cell color on this row would be considered as the correct value. All other cells would be ignored.

The expected result would be the following:

It's important to note that these diagrams simplify the original problem shown initially.  In reality each cell would define a rectangle with its corner coordinates (top, bottom, left, right) having different values.  Each rectangle would also have different dimensions, though in general each rectangle on the same row would almost have the same height.  To determine whether two cells are on the same row or not, we make use of the top-right corner of the two cells being compared. To get the corner coordinate to fit its target, a margin of error of 1% would be added to suffice the height differentiation deficiency.

Problem Resolution
The approach taken to address this problem is via brute force.

The first step taken is that of grouping by a color all the cell coordinates.  For the same example shown above, the coordinates are shown in the format (x, y).

The next step taken was that of comparing the y-coordinate of a cell color with all other y-coordinate of all other cells colors.  When they have the same y-coordinate, we keep track that they are on the same row and move to the next color.

Iteration 1: Yellow cell (2,  1) compare with Brown cell (5, 1) - same y-coordinate and hence matched
Iteration 2: Yellow cell (2, 1)  compare with Violet cells - no matches
Iteration 3: Yellow cell (2, 1)  compare with Grey cells - no matches
Iteration 4: Yellow cell (2, 1)  compare with Green cells - no matches
Iteration 5: Yellow cell (2, 1)  compare with Orange cells - no matches
Iteration 6: Yellow cell (2, 1)  compare with Blue cells - no matches
Iteration 7: Yellow cell (2, 4)  compare with Yellow cells - no matches
Iteration etc

foreach (sourceCellColor in cellColors)
{
    foreach (sourceCellCoordinate in sourceCellColor.Coordinates)
    {
        foreach (targetCellColor in cellColors)
        {
            foreach (targetCellCoordinate in targetCellColor.Coordinates)
            {
                if (sourceCellCoordinate = targetCellCoordinate)
                {
                    sourceCellColor has a match on targetCellColor with targetCellCoordinate;
                    break loop for targetCellColor.Coordinates;
                }
            }
        }
    }
}

Optimization 1
Reducing the number of iterations.  
  • On the first loop there is no need to make use of the last color as it would have already been processed and compared via one of the inner loops.
  • On the inner loop there is no need to compare to currently color being processed by outside loop.  Also, already processed colors from the outside loop would have already been processed. If I already compared Yellow to Brown, there is no need to compare again Brown to Yellow.
foreach (sourceCellColor in cellColors - lastColor)
{
    foreach (sourceCellCoordinate in sourceCellColor.Coordinates)
    {
        foreach (targetCellColor in (cellColors - already or currently being processed sourceCellColor))
        {
            foreach (targetCellCoordinate in targetCellColor.Coordinates)
            {
                etc
            }
        }
    }
}

Optimization 2
Ranking colors by minimum number of occurrences.

This would be beneficial especially if one of the initial cells being compared is a best fit, as we reduce the number of iterations.  Also, eliminating processed colors would continue reduce the number of iterations.

Optimization 3
During the processing of a colors, we might be in a position that we have already found the best fit and hence there is no need for additional brute force processing.  To determine this, we need to know the number of remaining colors to be matched.  Should within the current iteration we have a total match as the renaming number of colors to be matched, then we can terminate processing.

Examples:
Iteration 1: Current color = Violet; Number of colors to be matched = 7
Iteration 2: Current color = Grey; Number of colors to be matched = 6
Iteration 3: Current color = Orange; Number of colors to be matched = 5; Using the sample data above, at this stage we would have found the best fit and processing can stop

Should the colors Grey and Violet have been inverted, we would have been able to find the best fit in the 2nd iteration.

Other notes
The logic for comparing cells isn't included here.  This involves more processing than just comparing two values.  The additional processing required is to adjust the cell coordinates due to image rotation because of imperfect document scanning.  This rotation processing consists of additional mathematical transformation that incur a performance charge.