|
| 1 | +# Analyze with the prebuilt document model |
| 2 | + |
| 3 | +This sample demonstrates how to analyze entities, key-value pairs, tables, and selection marks from documents using the general prebuilt document model. |
| 4 | + |
| 5 | +To get started you'll need a Cognitive Services resource or a Form Recognizer resource. See [README][README] for prerequisites and instructions. |
| 6 | + |
| 7 | +## Creating a `DocumentAnalysisClient` |
| 8 | + |
| 9 | +To create a new `DocumentAnalysisClient` you need the endpoint and credentials from your resource. In the sample below you'll use a Form Recognizer API key credential by creating an `AzureKeyCredential` object, that if needed, will allow you to update the API key without creating a new client. |
| 10 | + |
| 11 | +You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. |
| 12 | + |
| 13 | +```C# Snippet:CreateDocumentAnalysisClient |
| 14 | +string endpoint = "<endpoint>"; |
| 15 | +string apiKey = "<apiKey>"; |
| 16 | +var credential = new AzureKeyCredential(apiKey); |
| 17 | +var client = new DocumentAnalysisClient(new Uri(endpoint), credential); |
| 18 | +``` |
| 19 | + |
| 20 | +## Use the prebuilt document model to analyze a document from a URI |
| 21 | + |
| 22 | +To analyze a given file at a URI, use the `StartAnalyzeDocumentFromUri` method and pass `prebuilt-document` as the model ID. The returned value is an `AnalyzeResult` object containing data about the submitted document. |
| 23 | + |
| 24 | +```C# Snippet:FormRecognizerAnalyzePrebuiltDocumentFromUriAsync |
| 25 | +string fileUri = "<fileUri>"; |
| 26 | + |
| 27 | +AnalyzeDocumentOperation operation = await client.StartAnalyzeDocumentFromUriAsync("prebuilt-document", fileUri); |
| 28 | + |
| 29 | +await operation.WaitForCompletionAsync(); |
| 30 | + |
| 31 | +AnalyzeResult result = operation.Value; |
| 32 | + |
| 33 | +foreach (DocumentPage page in result.Pages) |
| 34 | +{ |
| 35 | + Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),"); |
| 36 | + Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s)."); |
| 37 | + |
| 38 | + for (int i = 0; i < page.Lines.Count; i++) |
| 39 | + { |
| 40 | + DocumentLine line = page.Lines[i]; |
| 41 | + Console.WriteLine($" Line {i} has content: '{line.Content}'."); |
| 42 | + |
| 43 | + Console.WriteLine($" Its bounding box is:"); |
| 44 | + Console.WriteLine($" Upper left => X: {line.BoundingBox[0].X}, Y= {line.BoundingBox[0].Y}"); |
| 45 | + Console.WriteLine($" Upper right => X: {line.BoundingBox[1].X}, Y= {line.BoundingBox[1].Y}"); |
| 46 | + Console.WriteLine($" Lower right => X: {line.BoundingBox[2].X}, Y= {line.BoundingBox[2].Y}"); |
| 47 | + Console.WriteLine($" Lower left => X: {line.BoundingBox[3].X}, Y= {line.BoundingBox[3].Y}"); |
| 48 | + } |
| 49 | + |
| 50 | + for (int i = 0; i < page.SelectionMarks.Count; i++) |
| 51 | + { |
| 52 | + DocumentSelectionMark selectionMark = page.SelectionMarks[i]; |
| 53 | + |
| 54 | + Console.WriteLine($" Selection Mark {i} is {selectionMark.State}."); |
| 55 | + Console.WriteLine($" Its bounding box is:"); |
| 56 | + Console.WriteLine($" Upper left => X: {selectionMark.BoundingBox[0].X}, Y= {selectionMark.BoundingBox[0].Y}"); |
| 57 | + Console.WriteLine($" Upper right => X: {selectionMark.BoundingBox[1].X}, Y= {selectionMark.BoundingBox[1].Y}"); |
| 58 | + Console.WriteLine($" Lower right => X: {selectionMark.BoundingBox[2].X}, Y= {selectionMark.BoundingBox[2].Y}"); |
| 59 | + Console.WriteLine($" Lower left => X: {selectionMark.BoundingBox[3].X}, Y= {selectionMark.BoundingBox[3].Y}"); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +foreach (DocumentStyle style in result.Styles) |
| 64 | +{ |
| 65 | + // Check the style and style confidence to see if text is handwritten. |
| 66 | + // Note that value '0.8' is used as an example. |
| 67 | +
|
| 68 | + bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true; |
| 69 | + |
| 70 | + if (isHandwritten && style.Confidence > 0.8) |
| 71 | + { |
| 72 | + Console.WriteLine($"Handwritten content found:"); |
| 73 | + |
| 74 | + foreach (DocumentSpan span in style.Spans) |
| 75 | + { |
| 76 | + Console.WriteLine($" Content: {result.Content.Substring(span.Offset, span.Length)}"); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +Console.WriteLine("The following tables were extracted:"); |
| 82 | + |
| 83 | +for (int i = 0; i < result.Tables.Count; i++) |
| 84 | +{ |
| 85 | + DocumentTable table = result.Tables[i]; |
| 86 | + Console.WriteLine($" Table {i} has {table.RowCount} rows and {table.ColumnCount} columns."); |
| 87 | + |
| 88 | + foreach (DocumentTableCell cell in table.Cells) |
| 89 | + { |
| 90 | + Console.WriteLine($" Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'."); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +Console.WriteLine("Detected entities:"); |
| 95 | + |
| 96 | +foreach (DocumentEntity entity in result.Entities) |
| 97 | +{ |
| 98 | + if (entity.SubCategory == null) |
| 99 | + { |
| 100 | + Console.WriteLine($" Found entity '{entity.Content}' with category '{entity.Category}'."); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + Console.WriteLine($" Found entity '{entity.Content}' with category '{entity.Category}' and sub-category '{entity.SubCategory}'."); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +Console.WriteLine("Detected key-value pairs:"); |
| 109 | + |
| 110 | +foreach (DocumentKeyValuePair kvp in result.KeyValuePairs) |
| 111 | +{ |
| 112 | + if (kvp.Value.Content == null) |
| 113 | + { |
| 114 | + Console.WriteLine($" Found key with no value: '{kvp.Key.Content}'"); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + Console.WriteLine($" Found key-value pair: '{kvp.Key.Content}' and '{kvp.Value.Content}'"); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Use the prebuilt document model to analyze a document from a file stream |
| 124 | + |
| 125 | +To analyze a given file at a file stream, use the `StartAnalyzeDocument` method and pass `prebuilt-document` as the model ID. The returned value is an `AnalyzeResult` object containing data about the submitted document. |
| 126 | + |
| 127 | +```C# Snippet:FormRecognizerAnalyzePrebuiltDocumentFromFileAsync |
| 128 | +string filePath = "filePath"; |
| 129 | +using var stream = new FileStream(filePath, FileMode.Open); |
| 130 | + |
| 131 | +AnalyzeDocumentOperation operation = await client.StartAnalyzeDocumentAsync("prebuilt-document", stream); |
| 132 | + |
| 133 | +await operation.WaitForCompletionAsync(); |
| 134 | + |
| 135 | +AnalyzeResult result = operation.Value; |
| 136 | + |
| 137 | +foreach (DocumentPage page in result.Pages) |
| 138 | +{ |
| 139 | + Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),"); |
| 140 | + Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s)."); |
| 141 | + |
| 142 | + for (int i = 0; i < page.Lines.Count; i++) |
| 143 | + { |
| 144 | + DocumentLine line = page.Lines[i]; |
| 145 | + Console.WriteLine($" Line {i} has content: '{line.Content}'."); |
| 146 | + |
| 147 | + Console.WriteLine($" Its bounding box is:"); |
| 148 | + Console.WriteLine($" Upper left => X: {line.BoundingBox[0].X}, Y= {line.BoundingBox[0].Y}"); |
| 149 | + Console.WriteLine($" Upper right => X: {line.BoundingBox[1].X}, Y= {line.BoundingBox[1].Y}"); |
| 150 | + Console.WriteLine($" Lower right => X: {line.BoundingBox[2].X}, Y= {line.BoundingBox[2].Y}"); |
| 151 | + Console.WriteLine($" Lower left => X: {line.BoundingBox[3].X}, Y= {line.BoundingBox[3].Y}"); |
| 152 | + } |
| 153 | + |
| 154 | + for (int i = 0; i < page.SelectionMarks.Count; i++) |
| 155 | + { |
| 156 | + DocumentSelectionMark selectionMark = page.SelectionMarks[i]; |
| 157 | + |
| 158 | + Console.WriteLine($" Selection Mark {i} is {selectionMark.State}."); |
| 159 | + Console.WriteLine($" Its bounding box is:"); |
| 160 | + Console.WriteLine($" Upper left => X: {selectionMark.BoundingBox[0].X}, Y= {selectionMark.BoundingBox[0].Y}"); |
| 161 | + Console.WriteLine($" Upper right => X: {selectionMark.BoundingBox[1].X}, Y= {selectionMark.BoundingBox[1].Y}"); |
| 162 | + Console.WriteLine($" Lower right => X: {selectionMark.BoundingBox[2].X}, Y= {selectionMark.BoundingBox[2].Y}"); |
| 163 | + Console.WriteLine($" Lower left => X: {selectionMark.BoundingBox[3].X}, Y= {selectionMark.BoundingBox[3].Y}"); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +foreach (DocumentStyle style in result.Styles) |
| 168 | +{ |
| 169 | + // Check the style and style confidence to see if text is handwritten. |
| 170 | + // Note that value '0.8' is used as an example. |
| 171 | +
|
| 172 | + bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true; |
| 173 | + |
| 174 | + if (isHandwritten && style.Confidence > 0.8) |
| 175 | + { |
| 176 | + Console.WriteLine($"Handwritten content found:"); |
| 177 | + |
| 178 | + foreach (DocumentSpan span in style.Spans) |
| 179 | + { |
| 180 | + Console.WriteLine($" Content: {result.Content.Substring(span.Offset, span.Length)}"); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +Console.WriteLine("The following tables were extracted:"); |
| 186 | + |
| 187 | +for (int i = 0; i < result.Tables.Count; i++) |
| 188 | +{ |
| 189 | + DocumentTable table = result.Tables[i]; |
| 190 | + Console.WriteLine($" Table {i} has {table.RowCount} rows and {table.ColumnCount} columns."); |
| 191 | + |
| 192 | + foreach (DocumentTableCell cell in table.Cells) |
| 193 | + { |
| 194 | + Console.WriteLine($" Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'."); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +Console.WriteLine("Detected entities:"); |
| 199 | + |
| 200 | +foreach (DocumentEntity entity in result.Entities) |
| 201 | +{ |
| 202 | + if (entity.SubCategory == null) |
| 203 | + { |
| 204 | + Console.WriteLine($" Found entity '{entity.Content}' with category '{entity.Category}'."); |
| 205 | + } |
| 206 | + else |
| 207 | + { |
| 208 | + Console.WriteLine($" Found entity '{entity.Content}' with category '{entity.Category}' and sub-category '{entity.SubCategory}'."); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +Console.WriteLine("Detected key-value pairs:"); |
| 213 | + |
| 214 | +foreach (DocumentKeyValuePair kvp in result.KeyValuePairs) |
| 215 | +{ |
| 216 | + if (kvp.Value.Content == null) |
| 217 | + { |
| 218 | + Console.WriteLine($" Found key with no value: '{kvp.Key.Content}'"); |
| 219 | + } |
| 220 | + else |
| 221 | + { |
| 222 | + Console.WriteLine($" Found key-value pair: '{kvp.Key.Content}' and '{kvp.Value.Content}'"); |
| 223 | + } |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +To see the full example source files, see: |
| 228 | + |
| 229 | +* [Analyze with prebuilt document from URI](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample_AnalyzePrebuiltDocumentFromUriAsync.cs) |
| 230 | +* [Analyze with prebuilt document from file](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample_AnalyzePrebuiltDocumentFromFileAsync.cs) |
| 231 | + |
| 232 | +[README]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/formrecognizer/Azure.AI.FormRecognizer#getting-started |
0 commit comments