From f8c62c6defa5c8c5ac06450bbcf1dc558b6e1a32 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Wed, 13 Nov 2024 23:42:28 +0300 Subject: [PATCH] Implement IOcrClient --- OcrClient/Models/OcrResponse.cs | 25 +++++- OcrClient/OcrClient.csproj | 1 + OcrClient/Services/IOcrClient.cs | 2 +- OcrClient/Services/OcrClient.cs | 19 ----- OcrClient/Services/YandexOcrClient.cs | 70 +++++++++++++++++ .../Controllers/RibbonController.cs | 9 ++- RhSolutions.AddIn/Properties/AssemblyInfo.cs | 2 + RhSolutions.AddIn/RhSolutions.AddIn.csproj | 16 ++-- .../Services/AddInConfiguration.cs | 77 +++++++++++-------- .../Services/IAddInConfiguration.cs | 19 +++-- RhSolutions.AddIn/Tools/EventsUtil.cs | 65 ++++++++-------- RhSolutions.AddIn/Tools/OcrTool.cs | 48 +++++++++++- 12 files changed, 251 insertions(+), 102 deletions(-) delete mode 100644 OcrClient/Services/OcrClient.cs create mode 100644 OcrClient/Services/YandexOcrClient.cs diff --git a/OcrClient/Models/OcrResponse.cs b/OcrClient/Models/OcrResponse.cs index 2afc041..2b26ab7 100644 --- a/OcrClient/Models/OcrResponse.cs +++ b/OcrClient/Models/OcrResponse.cs @@ -2,4 +2,27 @@ namespace OcrClient.Models; public class OcrResponse { -} \ No newline at end of file + public Result? Result { get; set; } +} +public class Result +{ + public TextAnnotation? TextAnnotation { get; set; } +} +public class TextAnnotation +{ + public List? Tables { get; set; } +} + +public class Table +{ + public string? RowCount { get; set; } + public string? ColumnCount { get; set; } + public List? Cells { get; set; } +} + +public class Cell +{ + public string? RowIndex { get; set; } + public string? ColumnIndex { get; set; } + public string? Text { get; set; } +} diff --git a/OcrClient/OcrClient.csproj b/OcrClient/OcrClient.csproj index 4f78021..cdd654f 100644 --- a/OcrClient/OcrClient.csproj +++ b/OcrClient/OcrClient.csproj @@ -8,6 +8,7 @@ + diff --git a/OcrClient/Services/IOcrClient.cs b/OcrClient/Services/IOcrClient.cs index c8b1a86..9b281c9 100644 --- a/OcrClient/Services/IOcrClient.cs +++ b/OcrClient/Services/IOcrClient.cs @@ -4,5 +4,5 @@ namespace OcrClient.Services; public interface IOcrClient { - public Task ProcessImage(string base64Image); + public Task> ProcessImage(string base64Image, string xFolderId, string apiKey); } diff --git a/OcrClient/Services/OcrClient.cs b/OcrClient/Services/OcrClient.cs deleted file mode 100644 index e4cfef3..0000000 --- a/OcrClient/Services/OcrClient.cs +++ /dev/null @@ -1,19 +0,0 @@ -using OcrClient.Models; -using System.Net.Http; - -namespace OcrClient.Services; - -public class YandexOcrClient : IOcrClient -{ - private readonly HttpClient _httpClient; - - public YandexOcrClient(HttpClient httpClient) - { - _httpClient = httpClient; - } - - public Task ProcessImage(string base64Image) - { - throw new NotImplementedException(); - } -} \ No newline at end of file diff --git a/OcrClient/Services/YandexOcrClient.cs b/OcrClient/Services/YandexOcrClient.cs new file mode 100644 index 0000000..8568e38 --- /dev/null +++ b/OcrClient/Services/YandexOcrClient.cs @@ -0,0 +1,70 @@ +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using Newtonsoft.Json; +using OcrClient.Models; + +namespace OcrClient.Services; + +public class YandexOcrClient : IOcrClient +{ + private readonly HttpClient _httpClient; + public YandexOcrClient(HttpClient httpClient) + { + _httpClient = httpClient; + _httpClient.BaseAddress = new Uri("https://ocr.api.cloud.yandex.net/ocr/v1/"); + } + + public async Task> ProcessImage(string base64Image, string xFolderId, string apiKey) + { + using StringContent jsonContent = new( + JsonConvert.SerializeObject(new + { + mimeType = "PNG", + languageCodes = new string[] { "ru", "en" }, + model = "table", + content = base64Image + }), + Encoding.UTF8, + "application/json"); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Api-Key", apiKey); + _httpClient.DefaultRequestHeaders.Add("x-folder-id", xFolderId); + _httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true"); + + using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent); + response.EnsureSuccessStatusCode(); + + string jsonResponse = await response.Content.ReadAsStringAsync(); + OcrResponse? deserialized = JsonConvert.DeserializeObject(jsonResponse); + + if (deserialized != null) + { + var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty
(); + if (tables.Any()) + { + List result = new(); + foreach (var table in tables) + { + if (table.Cells == null || table.Cells.Count == 0) + { + continue; + } + int columnCount = int.Parse(table.ColumnCount); + int rowCount = int.Parse(table.RowCount); + object[,] cells = new object[rowCount, columnCount]; + + foreach (Cell cell in table.Cells) + { + int rowIndex = int.Parse(cell.RowIndex); + int columnIndex = int.Parse(cell.ColumnIndex); + cells[rowIndex, columnIndex] = double.TryParse(cell.Text, out double v) ? + v : cell.Text ?? string.Empty; + } + result.Add(cells); + } + return result; + } + } + return null; + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index 310f0fe..cc58586 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -33,7 +33,7 @@ public class RibbonController : ExcelRibbon