FormsRecognizer how to create ocr.json files programmatically for custom classification model

Tim Bates 20 Reputation points
2023-10-01T13:59:25.55+00:00

In order to automate the creation of a new custom classification model programmatically it appears we need to create ocr.json files for each file before we call the BuildDocumentClassifierAsync method via the DocumentModelAdministrationClient SDK. These files appear to contain an object with some outer details and an embedded AnalyzeResult. The documentation describes running prebuilt-layout against each file in the set before trying to train the model. However, when programmatically performing a prebuilt-layout extraction the AnalyzeResult returned and serialized is in a slightly different shape to that seen when Studio creates the ocr files. Some fields have different names (polygon vs boundingpolygon etc) and some object fields also differ - even though the api version returned is the same.
The analyzeResult is also embedded in the json as though an outer object has been serialized, but it is not clear what that object might be.
If the shape does not match the expected json structure (obtained from Studio when clicking "Train"), the subsequent call to BuildDocumentClassiferAsync fails.
Is it possible to generate the same ocr.json shape programmatically? And if so, how do we do this please?

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,396 questions
{count} votes

Accepted answer
  1. dupammi 6,815 Reputation points Microsoft Vendor
    2023-10-05T07:10:04.2866667+00:00

    Hi @Tim Bates ,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer.

    Question: Is there any way to create ocr.json files programmatically for custom classification model using FormsRecognizer ?

    Solution: Using the SDK, the operation object's response returned has a GetRawResponse() method. Calling that, and then taking the value of the raw response ". Content" gives a BinaryData object representing the layout JSON in the same shape as Studio produces in the ocr.json files.
    Uploading the BinaryData as a new blob file with the right name, can then programmatically build the model successfully using SDK (or in Studio).

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information.

    I hope this helps!

    If you have any other questions, please let me know. Thank you again for your time and patience throughout this issue.


    Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful