-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessImage.cs
More file actions
93 lines (77 loc) · 3.4 KB
/
ProcessImage.cs
File metadata and controls
93 lines (77 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Azure;
using Azure.AI.Vision.ImageAnalysis;
using Azure.Data.Tables;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading.Tasks;
namespace ProcessImages;
public class ProcessImage
{
private readonly ILogger<ProcessImage> _logger;
private readonly string _visionEndpoint;
private readonly string _visionKey;
private readonly TableClient _tableClient;
public ProcessImage(ILogger<ProcessImage> logger)
{
_logger = logger;
_visionEndpoint = Environment.GetEnvironmentVariable("ComputerVisionEndpoint");
_visionKey = Environment.GetEnvironmentVariable("ComputerVisionKey");
string tableConnection = Environment.GetEnvironmentVariable("OutputTableConnection");
var serviceClient = new TableServiceClient(tableConnection);
_tableClient = serviceClient.GetTableClient("ImageTextResultsV2");
_tableClient.CreateIfNotExists();
}
[Function(nameof(ProcessImage))]
public async Task Run([BlobTrigger("imageanalysis/{name}", Connection = "StorageConnection")] Stream stream, string name)
{
string imgUrl = $"https://{Environment.GetEnvironmentVariable("StorageAccountName")}.blob.core.windows.net/imageanalysis/{name}";
_logger.LogInformation("C# Blob trigger function Processed blob\n Name: {name} \n Data: {imgUrl}", name, imgUrl);
var credentials = new ApiKeyServiceClientCredentials(_visionKey);
ComputerVisionClient visionClient = new ComputerVisionClient(credentials)
{
Endpoint = _visionEndpoint
};
var textHeaders = await visionClient.ReadAsync(imgUrl);
string operationLocation = textHeaders.OperationLocation;
Thread.Sleep(2000);
const int numberOfCharsInOperationId = 36;
string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
ReadOperationResult results;
do
{
results = await visionClient.GetReadResultAsync(Guid.Parse(operationId));
}
while (results.Status == OperationStatusCodes.Running ||
results.Status == OperationStatusCodes.NotStarted);
var textUrlFileResults = results.AnalyzeResult.ReadResults;
StringBuilder text = new StringBuilder();
foreach (Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.ReadResult page in textUrlFileResults)
{
foreach (Line line in page.Lines)
{
text.AppendLine(line.Text);
}
}
var entity = new TableEntity("ImageResults", Guid.NewGuid().ToString())
{
{ "FileName", name },
{ "TextExtracted", text.ToString() },
{ "ProcessedDate", DateTime.UtcNow }
};
await _tableClient.AddEntityAsync(entity);
_logger.LogInformation($"Image text extraction completed and stored for {name}");
}
public class ImageContet : ITableEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Text { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public ETag ETag { get; set; }
};
}