AI Builder Power Automate
Manish Solanki  

Extract all the text in photos and PDF documents (OCR) – AI Builder template 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 model 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:

2. Next, select “Use in a flow” option from ‘Use prebuild model’ dropdown list:

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.

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:

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

Expression used in Compose action:

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.

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.

Leave A Comment