-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Devis Lucato <[email protected]>
- Loading branch information
Showing
17 changed files
with
310 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
dotnet/CoreLib/DataFormats/Image/AzureFormRecognizer/AzureFormRecognizerConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticMemory.DataFormats.Image.AzureFormRecognizer; | ||
|
||
public class AzureFormRecognizerConfig | ||
{ | ||
[JsonConverter(typeof(JsonStringEnumConverter))] | ||
public enum AuthTypes | ||
{ | ||
Unknown = -1, | ||
AzureIdentity, | ||
APIKey, | ||
} | ||
|
||
public AuthTypes Auth { get; set; } = AuthTypes.Unknown; | ||
|
||
public string Endpoint { get; set; } = string.Empty; | ||
|
||
public string APIKey { get; set; } = string.Empty; | ||
} |
65 changes: 65 additions & 0 deletions
65
dotnet/CoreLib/DataFormats/Image/AzureFormRecognizer/AzureFormRecognizerEngine.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.AI.FormRecognizer.DocumentAnalysis; | ||
using Azure.Identity; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticMemory.Configuration; | ||
|
||
namespace Microsoft.SemanticMemory.DataFormats.Image.AzureFormRecognizer; | ||
|
||
/// <summary> | ||
/// OCR engine based on Azure.AI.FormRecognizer. | ||
/// </summary> | ||
public class AzureFormRecognizerEngine : IOcrEngine | ||
{ | ||
private readonly DocumentAnalysisClient _recognizerClient; | ||
private readonly ILogger<AzureFormRecognizerEngine> _log; | ||
|
||
/// <summary> | ||
/// Creates a new instance of the AzureFormRecognizerOcrEngine passing in the Form Recognizer endpoint and key. | ||
/// </summary> | ||
/// <param name="endpoint">The endpoint for accessing a provisioned Azure Form Recognizer instance</param> | ||
/// <param name="config">The AzureFormRecognizerConfig config for this service.</param> | ||
public AzureFormRecognizerEngine(string endpoint, AzureFormRecognizerConfig config, ILogger<AzureFormRecognizerEngine> log) | ||
{ | ||
this._log = log; | ||
|
||
switch (config.Auth) | ||
{ | ||
case AzureFormRecognizerConfig.AuthTypes.AzureIdentity: | ||
this._recognizerClient = new DocumentAnalysisClient(new Uri(endpoint), new DefaultAzureCredential()); | ||
break; | ||
|
||
case AzureFormRecognizerConfig.AuthTypes.APIKey: | ||
if (string.IsNullOrEmpty(config.APIKey)) | ||
{ | ||
this._log.LogCritical("Azure Form Recognizer API key is empty"); | ||
throw new ConfigurationException("Azure Form Recognizer API key is empty"); | ||
} | ||
|
||
this._recognizerClient = new DocumentAnalysisClient(new Uri(endpoint), new AzureKeyCredential(config.APIKey)); | ||
break; | ||
|
||
default: | ||
this._log.LogCritical("Azure Form Recognizer authentication type '{0}' undefined or not supported", config.Auth); | ||
throw new ConfigurationException($"Azure Form Recognizer authentication type '{config.Auth}' undefined or not supported"); | ||
} | ||
} | ||
|
||
///<inheritdoc/> | ||
public async Task<string> ExtractTextFromImageAsync(Stream imageContent, CancellationToken cancellationToken = default) | ||
{ | ||
// Start the OCR operation | ||
var operation = await this._recognizerClient.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-read", imageContent, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
||
// Wait for the result | ||
Response<AnalyzeResult> operationResponse = await operation.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
return operationResponse.Value.Content; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
dotnet/CoreLib/DataFormats/Image/AzureFormRecognizer/DependencyInjection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticMemory.DataFormats.Image; | ||
using Microsoft.SemanticMemory.DataFormats.Image.AzureFormRecognizer; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.SemanticMemory; | ||
|
||
public static partial class MemoryClientBuilderExtensions | ||
{ | ||
public static MemoryClientBuilder WithAzureFormRecognizer(this MemoryClientBuilder builder, AzureFormRecognizerConfig config) | ||
{ | ||
builder.Services.AddAzureFormRecognizer(config); | ||
|
||
return builder; | ||
} | ||
} | ||
|
||
public static partial class DependencyInjection | ||
{ | ||
public static IServiceCollection AddAzureFormRecognizer(this IServiceCollection services, AzureFormRecognizerConfig config) | ||
{ | ||
return services | ||
.AddSingleton<AzureFormRecognizerConfig>(config) | ||
.AddTransient<IOcrEngine, AzureFormRecognizerEngine>(); | ||
} | ||
|
||
public static IServiceCollection AddAzureFormRecognizer(this IServiceCollection services, string endpoint, string apiKey) | ||
{ | ||
var config = new AzureFormRecognizerConfig { Endpoint = endpoint, APIKey = apiKey, Auth = AzureFormRecognizerConfig.AuthTypes.APIKey }; | ||
return services.AddAzureFormRecognizer(config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.SemanticMemory.DataFormats.Image; | ||
|
||
/// <summary> | ||
/// An OCR engine that can read in text from image files. | ||
/// </summary> | ||
public interface IOcrEngine | ||
{ | ||
/// <summary> | ||
/// Reads all text from the image. | ||
/// </summary> | ||
/// <param name="imageContent">The image content stream.</param> | ||
Task<string> ExtractTextFromImageAsync(Stream imageContent, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.SemanticMemory.DataFormats.Image; | ||
|
||
public class ImageDecoder | ||
{ | ||
public async Task<string> ImageToTextAsync(IOcrEngine engine, string filename, CancellationToken cancellationToken = default) | ||
{ | ||
using var stream = File.OpenRead(filename); | ||
return await this.ImageToTextAsync(engine, stream, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<string> ImageToTextAsync(IOcrEngine engine, BinaryData data, CancellationToken cancellationToken = default) | ||
{ | ||
using var stream = data.ToStream(); | ||
return await this.ImageToTextAsync(engine, stream, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
public Task<string> ImageToTextAsync(IOcrEngine engine, Stream data, CancellationToken cancellationToken = default) | ||
{ | ||
return engine.ExtractTextFromImageAsync(data, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.