/// <summary> /// Attemps to detect the <see cref="FormContentType"/> of a stream of bytes. The algorithm searches through /// the first set of bytes in the stream and compares it to well-known file signatures. /// </summary> /// <param name="stream">The stream to which the content type detection attempt will be performed.</param> /// <param name="contentType">If the detection is successful, outputs the detected content type. Otherwise, <c>default</c>.</param> /// <returns><c>true</c> if the detection was successful. Otherwise, <c>false</c>.</returns> /// <exception cref="NotSupportedException">Thrown when <paramref name="stream"/> is not seekable or readable.</exception> public static bool TryGetContentType(this Stream stream, out FormContentType contentType) { if (stream.BeginsWithHeader(PdfHeader)) { contentType = FormContentType.Pdf; } else if (stream.BeginsWithHeader(PngHeader)) { contentType = FormContentType.Png; } else if (stream.BeginsWithHeader(JpegHeader)) { contentType = FormContentType.Jpeg; } else if (stream.BeginsWithHeader(TiffLeHeader) || stream.BeginsWithHeader(TiffBeHeader)) { contentType = FormContentType.Tiff; } else if (stream.BeginsWithHeader(BmpHeader)) { contentType = FormContentType.Bmp; } else { contentType = default; return(false); } return(true); }
/// <summary> /// Recognizes pages from one or more forms, using a model trained with custom forms. /// </summary> /// <param name="modelId">The ID of the model to use for recognizing form values.</param> /// <param name="form">The stream containing one or more forms to recognize elements from.</param> /// <param name="recognizeCustomFormsOptions">A set of options available for configuring the recognize request.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> /// <returns>A <see cref="RecognizeCustomFormsOperation"/> to wait on this long-running operation. Its <see cref="RecognizeCustomFormsOperation.Value"/> upon successful /// completion will contain recognized pages from the input document.</returns> public virtual async Task <RecognizeCustomFormsOperation> StartRecognizeCustomFormsAsync(string modelId, Stream form, RecognizeCustomFormsOptions recognizeCustomFormsOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); Argument.AssertNotNull(form, nameof(form)); recognizeCustomFormsOptions ??= new RecognizeCustomFormsOptions(); using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormRecognizerClient)}.{nameof(StartRecognizeCustomForms)}"); scope.Start(); try { Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); FormContentType contentType = recognizeCustomFormsOptions.ContentType ?? DetectContentType(form, nameof(form)); Response response = await ServiceClient.AnalyzeWithCustomModelAsync(guid, contentType, form, includeTextDetails : recognizeCustomFormsOptions.IncludeFieldElements, cancellationToken).ConfigureAwait(false); string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); return(new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, location)); } catch (Exception e) { scope.Failed(e); throw; } }
/// <summary> /// Recognizes values from one or more receipts. /// </summary> /// <param name="receipt">The stream containing the one or more receipts to recognize values from.</param> /// <param name="recognizeReceiptsOptions">A set of options available for configuring the recognize request.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> /// <returns>A <see cref="RecognizeReceiptsOperation"/> to wait on this long-running operation. Its <see cref="RecognizeReceiptsOperation.Value"/> upon successful /// completion will contain the extracted receipt.</returns> public virtual async Task <RecognizeReceiptsOperation> StartRecognizeReceiptsAsync(Stream receipt, RecognizeReceiptsOptions recognizeReceiptsOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(receipt, nameof(receipt)); recognizeReceiptsOptions ??= new RecognizeReceiptsOptions(); using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormRecognizerClient)}.{nameof(StartRecognizeReceipts)}"); scope.Start(); try { FormContentType contentType = recognizeReceiptsOptions.ContentType ?? DetectContentType(receipt, nameof(receipt)); Response response = await ServiceClient.AnalyzeReceiptAsyncAsync(contentType, receipt, includeTextDetails : recognizeReceiptsOptions.IncludeFieldElements, cancellationToken).ConfigureAwait(false); string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); return(new RecognizeReceiptsOperation(ServiceClient, Diagnostics, location)); } catch (Exception e) { scope.Failed(e); throw; } }
internal static string GetContentTypeString(FormContentType contentType) { return(contentType switch { FormContentType.Pdf => "application/pdf", FormContentType.Png => "image/png", FormContentType.Jpeg => "image/jpeg", FormContentType.Tiff => "image/tiff", _ => throw new NotSupportedException($"The content type {contentType} is not supported."), });
/// <summary> /// Converts this instance into an equivalent <see cref="ContentType1"/>. /// </summary> /// <returns>The equivalent <see cref="ContentType1"/>.</returns> internal static ContentType1 ConvertToContentType1(this FormContentType type) { return(type switch { FormContentType.Pdf => ContentType1.ApplicationPdf, FormContentType.Jpeg => ContentType1.ImageJpeg, FormContentType.Png => ContentType1.ImagePng, FormContentType.Tiff => ContentType1.ImageTiff, _ => throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown FormContentType value."), });
public virtual RecognizeReceiptsOperation StartRecognizeReceipts(Stream receipt, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(receipt, nameof(receipt)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(receipt, nameof(receipt)); ResponseWithHeaders <ServiceAnalyzeReceiptAsyncHeaders> response = ServiceClient.AnalyzeReceiptAsync(contentType, receipt, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken); return(new RecognizeReceiptsOperation(ServiceClient, response.Headers.OperationLocation)); }
public virtual RecognizeContentOperation StartRecognizeContent(Stream form, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(form, nameof(form)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(form, nameof(form)); ResponseWithHeaders <ServiceAnalyzeLayoutAsyncHeaders> response = ServiceClient.AnalyzeLayoutAsync(contentType, form, cancellationToken); return(new RecognizeContentOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation)); }
public virtual async Task <RecognizeContentOperation> StartRecognizeContentAsync(Stream form, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(form, nameof(form)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(form, nameof(form)); ResponseWithHeaders <ServiceAnalyzeLayoutAsyncHeaders> response = await ServiceClient.AnalyzeLayoutAsyncAsync(contentType, form, cancellationToken).ConfigureAwait(false); return(new RecognizeContentOperation(ServiceClient, response.Headers.OperationLocation)); }
public virtual RecognizeReceiptsOperation StartRecognizeReceipts(Stream receipt, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(receipt, nameof(receipt)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(receipt, nameof(receipt)); Response response = ServiceClient.AnalyzeReceiptAsync(contentType, receipt, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken); string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); return(new RecognizeReceiptsOperation(ServiceClient, Diagnostics, location)); }
public virtual async Task <RecognizeContentOperation> StartRecognizeContentAsync(Stream form, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(form, nameof(form)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(form, nameof(form)); Response response = await ServiceClient.AnalyzeLayoutAsyncAsync(contentType, form, cancellationToken).ConfigureAwait(false); string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); return(new RecognizeContentOperation(ServiceClient, Diagnostics, location)); }
/// <summary> /// Recognizes values from one or more receipts. /// </summary> /// <param name="receipt">The stream containing the one or more receipts to recognize values from.</param> /// <param name="recognizeReceiptsOptions">A set of options available for configuring the recognize request.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> /// <returns>A <see cref="RecognizeReceiptsOperation"/> to wait on this long-running operation. Its <see cref="RecognizeReceiptsOperation.Value"/> upon successful /// completion will contain the extracted receipt.</returns> public virtual async Task <RecognizeReceiptsOperation> StartRecognizeReceiptsAsync(Stream receipt, RecognizeReceiptsOptions recognizeReceiptsOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(receipt, nameof(receipt)); recognizeReceiptsOptions ??= new RecognizeReceiptsOptions(); using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormRecognizerClient)}.{nameof(StartRecognizeReceipts)}"); scope.Start(); try { FormContentType contentType = recognizeReceiptsOptions.ContentType ?? DetectContentType(receipt, nameof(receipt)); Response response = await ServiceClient.AnalyzeReceiptAsyncAsync(contentType, receipt, includeTextDetails : recognizeReceiptsOptions.IncludeFieldElements, default, cancellationToken).ConfigureAwait(false);
public virtual async Task <RecognizeCustomFormsOperation> StartRecognizeCustomFormsAsync(string modelId, Stream formFileStream, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); Argument.AssertNotNull(formFileStream, nameof(formFileStream)); Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(formFileStream, nameof(formFileStream)); ResponseWithHeaders <ServiceAnalyzeWithCustomModelHeaders> response = await ServiceClient.AnalyzeWithCustomModelAsync(guid, contentType, formFileStream, includeTextDetails : recognizeOptions.IncludeTextContent, cancellationToken).ConfigureAwait(false); return(new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation)); }
public virtual RecognizeCustomFormsOperation StartRecognizeCustomForms(string modelId, Stream formFileStream, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(modelId, nameof(modelId)); Argument.AssertNotNull(formFileStream, nameof(formFileStream)); Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId)); recognizeOptions ??= new RecognizeOptions(); FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(formFileStream, nameof(formFileStream)); Response response = ServiceClient.AnalyzeWithCustomModel(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken); string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); return(new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, location)); }
public virtual Response <ExtractedReceipt> ExtractReceipt(Stream stream, FormContentType contentType, bool includeRawPageExtractions = false, CancellationToken cancellationToken = default) { // TODO: automate content-type detection // https://github.com/Azure/azure-sdk-for-net/issues/10329 ResponseWithHeaders <AnalyzeReceiptAsyncHeaders> response = _operations.AnalyzeReceiptAsync(includeTextDetails: includeRawPageExtractions, stream, contentType, cancellationToken); var operation = new ExtractReceiptOperation(_operations, response.Headers.OperationLocation); ValueTask <Response <ExtractedReceipt> > task = operation.WaitForCompletionAsync(); // TODO: this feels very bad. Better way? // https://github.com/Azure/azure-sdk-for-net/issues/10391 task.AsTask().Wait(); if (!operation.HasValue) { throw new RequestFailedException("Failed to retrieve response from ExtractReceipt Long-Running Operation"); } // TODO: this is also a mess. Reconcile these together. // https://github.com/Azure/azure-sdk-for-net/issues/10391 return(Response.FromValue(operation.Value, task.AsTask().Result.GetRawResponse())); }
/// <summary> /// Recognizes layout elements from one or more passed-in forms. /// </summary> /// <param name="form">The stream containing one or more forms to recognize elements from.</param> /// <param name="recognizeContentOptions">A set of options available for configuring the recognize request.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> /// <returns>A <see cref="RecognizeContentOperation"/> to wait on this long-running operation. Its <see cref="RecognizeContentOperation.Value"/> upon successful /// completion will contain layout elements extracted from the form.</returns> public virtual RecognizeContentOperation StartRecognizeContent(Stream form, RecognizeContentOptions recognizeContentOptions = default, CancellationToken cancellationToken = default) { Argument.AssertNotNull(form, nameof(form)); recognizeContentOptions ??= new RecognizeContentOptions(); using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormRecognizerClient)}.{nameof(StartRecognizeContent)}"); scope.Start(); try { FormContentType contentType = recognizeContentOptions.ContentType ?? DetectContentType(form, nameof(form)); Response response = ServiceClient.AnalyzeLayoutAsync(contentType, form, cancellationToken); string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader); return(new RecognizeContentOperation(ServiceClient, Diagnostics, location)); } catch (Exception e) { scope.Failed(e); throw; } }
public virtual async Task <Operation <IReadOnlyList <ExtractedLayoutPage> > > StartExtractLayoutAsync(Stream stream, FormContentType contentType, bool includeRawPageExtractions = false, CancellationToken cancellationToken = default) { // TODO: automate content-type detection // https://github.com/Azure/azure-sdk-for-net/issues/10329 ResponseWithHeaders <AnalyzeLayoutAsyncHeaders> response = await _operations.AnalyzeLayoutAsyncAsync(stream, contentType, cancellationToken).ConfigureAwait(false); return(new ExtractLayoutOperation(_operations, response.Headers.OperationLocation)); }
internal async ValueTask <ResponseWithHeaders <AnalyzeWithCustomModelHeaders> > AnalyzeWithCustomModelAsync(Guid modelId, bool?includeTextDetails, Stream stream, FormContentType contentType, CancellationToken cancellationToken = default) { using var scope = clientDiagnostics.CreateScope("AllOperations.AnalyzeWithCustomModel"); scope.Start(); try { using var message = RestClient.CreateAnalyzeWithCustomModelRequest(modelId, includeTextDetails, stream, contentType); await pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 202: var headers = new AnalyzeWithCustomModelHeaders(message.Response); return(ResponseWithHeaders.FromValue(headers, message.Response)); default: throw await clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); } } catch (Exception e) { scope.Failed(e); throw; } }
internal ResponseWithHeaders <AnalyzeWithCustomModelHeaders> AnalyzeWithCustomModel(Guid modelId, bool?includeTextDetails, Stream stream, FormContentType contentType, CancellationToken cancellationToken = default) { using var scope = clientDiagnostics.CreateScope("AllOperations.AnalyzeWithCustomModel"); scope.Start(); try { // TODO: Could refactor this so different AnalyzeWithCustomModels overload call the same implementation with different request messages. using var message = RestClient.CreateAnalyzeWithCustomModelRequest(modelId, includeTextDetails, stream, contentType); pipeline.Send(message, cancellationToken); switch (message.Response.Status) { case 202: var headers = new AnalyzeWithCustomModelHeaders(message.Response); return(ResponseWithHeaders.FromValue(headers, message.Response)); default: throw clientDiagnostics.CreateRequestFailedException(message.Response); } } catch (Exception e) { scope.Failed(e); throw; } }
public ResponseWithHeaders <AnalyzeLayoutAsyncHeaders> AnalyzeLayoutAsync(Stream stream, FormContentType contentType, CancellationToken cancellationToken = default) { using var scope = clientDiagnostics.CreateScope("AllOperations.AnalyzeLayoutAsync"); scope.Start(); try { using var message = RestClient.CreateAnalyzeLayoutAsyncRequest(stream, contentType); pipeline.Send(message, cancellationToken); switch (message.Response.Status) { case 202: var headers = new AnalyzeLayoutAsyncHeaders(message.Response); return(ResponseWithHeaders.FromValue(headers, message.Response)); default: throw clientDiagnostics.CreateRequestFailedException(message.Response); } } catch (Exception e) { scope.Failed(e); throw; } }
public virtual async Task <Response <ExtractedReceipt> > ExtractReceiptAsync(Stream stream, FormContentType contentType, bool includeRawPageExtractions = false, CancellationToken cancellationToken = default) { // TODO: automate content-type detection // https://github.com/Azure/azure-sdk-for-net/issues/10329 ResponseWithHeaders <AnalyzeReceiptAsyncHeaders> response = _operations.AnalyzeReceiptAsync(includeTextDetails: includeRawPageExtractions, stream, contentType, cancellationToken); var operation = new ExtractReceiptOperation(_operations, response.Headers.OperationLocation); var operationResponse = await operation.WaitForCompletionAsync().ConfigureAwait(false); if (!operation.HasValue) { throw new RequestFailedException("Failed to retrieve response from ExtractReceipt Long-Running Operation"); } // TODO: Is this the best way? // https://github.com/Azure/azure-sdk-for-net/issues/10391 return(Response.FromValue(operation.Value, operationResponse.GetRawResponse())); }
public virtual async Task <Operation <ExtractedForm> > StartExtractFormAsync(string modelId, Stream stream, FormContentType contentType, bool includeRawPageExtractions = false, CancellationToken cancellationToken = default) { // TODO: automate content-type detection // https://github.com/Azure/azure-sdk-for-net/issues/10329 ResponseWithHeaders <AnalyzeWithCustomModelHeaders> response = await _operations.AnalyzeWithCustomModelAsync(new Guid(modelId), includeTextDetails : includeRawPageExtractions, stream, contentType, cancellationToken).ConfigureAwait(false); return(new ExtractFormOperation(_operations, modelId, response.Headers.OperationLocation)); }
public static string ToSerialString(this FormContentType value) => value switch {