cancel
Showing results for 
Search instead for 
Did you mean: 
ManishSolanki

Extract text (OCR) AI Builder - loop optimization

Use Case

Recently, we had the requirement to extract data from scanned pdfs or image files. So, we had chosen prebuilt AI model (Extract all the text in photos and PDF documents (OCR)) for extracting the data or content for that purpose. The prebuilt AI models are ready made models that can be used in the solution without training the model.

 

Problem

When we started testing the prebuilt OCR, AI builder model, we noticed that there are two nested loops (Apply to each) actions which degrade the performance. The Power Automate takes few minutes to parse pdf files which are more than 12 pages.

 

Solution

We can optimize the loops of objects array by converting it to XML format and then using the power of XPATH function to query all the text returned by the AI model.

 

Steps

Here the detailed steps for optimizing the flow:

 

  1. Firstly, we will set up the prebuild AI model by selecting the model from the list of available models: ManishSolanki_3-1696301031204.png
  2. Next, select "Use in a flow" option from 'Use prebuild model' dropdown list:ManishSolanki_4-1696301082082.png
  3. The template will ask for the connection for all the connectors used in the flow. Once all connections are set up, click Continue to proceed.

ManishSolanki_5-1696301189110.png

4. A flow will be set up for use if everything goes well. It contains the nested loops for getting the lines which we will optimize in the next steps:

 ManishSolanki_6-1696301511632.png

5. To compare the results, I have added a parallel action and writes the expression in the compose action:

Expression used in Compose action:

ManishSolanki_7-1696301624007.png

join(xpath(xml(json(concat('{"Root":{"Item":',outputs('Recognize_text_in_an_image')?['body/responsev2/predictionOutput/results'],'}}'))),'//Item//lines//text/text()'),' ')

 

Results

When runs the flow for 30+ pages scan pdf file, the results are exceptional. You can see the huge difference in execution time of parallel branches.

 

ManishSolanki_8-1696302344060.png

 

Conclusion

To conclude, we would say that to optimize array looping time, we can convert JSON to XML. Using the power of XPATH, we can query and get the same result as with looping using Apply to each action.

 

Apart from optimization execution time, it also gives benefit of reducing the action used in the flow. With one compose action, we can replace 3 actions (two apply to each & set variable action). Less action used in the flow reduces the actions limits set in the power automate.

 

You can optimize the loops for every use case where array objects are involved using the power of XML/XPATH and FX expressions in Power Automate.