Form Recognizer speed issues

Question:

I’m using a custom model with labels (created with the sample labeling tool) and getting the results with the “Python Form Recognizer Async Analyze” V2 SDK Code from the bottom of this 1 page.
It basicly works but it took over 20 seconds for a single page PDF file to get the results (6 labels used, S0 pricing model). 150 single page pdf files took over one hour.
We also tested with the V1 SDK Preview Version (without labels) of the form recognizer which was significantly faster then V2.

I know V2 is async now but is there anything which could be done to speed up form recognition?
Below is the code i’m basicly using:

########### Python Form Recognizer Async Analyze #############
import json
import time
from requests import get, post

# Endpoint URL
endpoint = r"<endpoint>"
apim_key = "<subsription key>"
model_id = "<model_id>"
post_url = endpoint + "/formrecognizer/v2.0-preview/custom/models/%s/analyze" % model_id
source = r"<file path>"
params = {
    "includeTextDetails": True
}

headers = {
    # Request headers
    'Content-Type': '<file type>',
    'Ocp-Apim-Subscription-Key': apim_key,
}
with open(source, "rb") as f:
    data_bytes = f.read()

try:
    resp = post(url = post_url, data = data_bytes, headers = headers, params = params)
    if resp.status_code != 202:
        print("POST analyze failed:n%s" % json.dumps(resp.json()))
        quit()
    print("POST analyze succeeded:n%s" % resp.headers)
    get_url = resp.headers["operation-location"]
except Exception as e:
    print("POST analyze failed:n%s" % str(e))
    quit() 

n_tries = 15
n_try = 0
wait_sec = 5
max_wait_sec = 60
while n_try < n_tries:
    try:
        resp = get(url = get_url, headers = {"Ocp-Apim-Subscription-Key": apim_key})
        resp_json = resp.json()
        if resp.status_code != 200:
            print("GET analyze results failed:n%s" % json.dumps(resp_json))
            quit()
        status = resp_json["status"]
        if status == "succeeded":
            print("Analysis succeeded:n%s" % json.dumps(resp_json))
            quit()
        if status == "failed":
            print("Analysis failed:n%s" % json.dumps(resp_json))
            quit()
        # Analysis still running. Wait and retry.
        time.sleep(wait_sec)
        n_try += 1
        wait_sec = min(2*wait_sec, max_wait_sec)     
    except Exception as e:
        msg = "GET analyze results failed:n%s" % str(e)
        print(msg)
        quit()
print("Analyze operation did not complete within the allocated time.")
Asked By: Mike246912

||

Answers:

Thanks for the question, we are investigating this issue and will update you shortly. For analyzing 150 single pages you can send all the pages in parallel to Form Recognizer to reduce the time.

Answered By: Neta

A note for benchmarking purposes (because I never found this info when I needed it): Looks like Form Recognizer has improved a lot in last 2 years. For a near real-time scenario, we are averaging about 6 secs for an image running against the Form Recognizer v3.0 custom model which extracts about 160 fields (texts, signatures, checkboxes, dates and times) from handwritten forms using API version 2022-08-31 from a C# REST API service. The accuracy we achieved AFTER data post-processing was 98%. Unfortunately, I couldn’t calculate the accuracy of the Form Recognizer output. The model had an average accuracy of 98% when trained with 12 documents.

Answered By: user605172