Extract the most predominant text from an image
Background
In this article, you will learn the details of how I've managed to extract the most predominant text from an image.
I've recently added a Solution package in the Power Apps Gallery for a sample License Plate Reader.
In the Power Automate flow that uses AI Builder's text recognition that is part of that solution, I have a part that processes the text and extracts only the biggest portion of the results.
Get the know-how by reading on!
Resources
YouTube: overview of the solution
GitHub: solution package to install the app and required resources in your environment.
Blog post: the story behind the app and overall solution.
The Recipe
At this point, I'll presume you have read the blog post that explains the app and flow and you understand that this section of the flow requires to only extract what is the most predominant section of text in the image.
To do so, the steps are as follow:
- Initialize the variables
- PlateNumber: the string that will contain the final result
- BiggestHeight: the reference to the largest (highest in height) part of text encountered so far
- VarianceFactor: since this is OCR there could be potential variance factor in size, so this is the level of acceptable tolerance this can be about
- MaximumHeight and MaximumHeight: boundaries calculated when a new BiggestHeight is found and based on the VarianceFactor, allows to simplify understanding of the formulas in the flow and not having to reprocess them (calculations) in every loop
- PlateNumber: the string that will contain the final result
- Loop through the extraction results. In this case, the JSON that is provided by the AI Builder predict action contains 2 sets of arrays we need to process:
- results
- lines
- First condition is to validate if the current text size, based on its height, is bigger than the maximum boundary (MaximumHeight), if so:
- This is now the new base for our extraction, so set the PlateNumber variable as this current text
- This is now the new base for our extraction, so set the PlateNumber variable as this current text
- Otherwise, ensure that the current text size is within the 2 boundaries (MinimumHeight & MaximumHeight), if so:
- Append the current text to the end of the PlateNumber variable
- Append the current text to the end of the PlateNumber variable
- Once those 2 conditions have been evaluated, before looping back to the next line within the set of results, evaluate a new condition: is the current height bigger then the last set BiggestHeight variable, if so:
- Set the BiggestHeight to be as the current height
- Calculate the boundaries (MinimumHeight & MaximumHeight) based on the VarianceFactor using the following formulas:
- sub(variables('BiggestHeight'),mul(variables('BiggestHeight'),variables('VarianceFactor')))add(variables('BiggestHeight'),mul(variables('BiggestHeight'),variables('VarianceFactor')))
- Set the BiggestHeight to be as the current height
Voilà !
This is just another example that even in a low code platform it is possible to perform loops and conditions that allow more complex scenarios to be implemented. Don't hesitate to download the solution from the gallery, edit the flow, adapt it, comment below, etc..
Learn and explore
About me
You can reach me on:
Keep on Diving!
*This post is locked for comments