From 2026e2a4e242f1d865928e660d37ac83fd9da1e2 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Thu, 14 Nov 2024 00:16:58 +0300 Subject: Move OcrClient to AddIn project --- OcrClient/Models/OcrResponse.cs | 28 ---------- OcrClient/OcrClient.csproj | 15 ----- OcrClient/Services/IOcrClient.cs | 8 --- OcrClient/Services/YandexOcrClient.cs | 70 ------------------------ RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 1 - RhSolutions.AddIn/Models/OcrResponse.cs | 28 ++++++++++ RhSolutions.AddIn/RhSolutions.AddIn.csproj | 3 +- RhSolutions.AddIn/Services/AddInConfiguration.cs | 4 -- RhSolutions.AddIn/Services/IOcrClient.cs | 8 +++ RhSolutions.AddIn/Services/YandexOcrClient.cs | 70 ++++++++++++++++++++++++ RhSolutions.AddIn/Tools/OcrTool.cs | 1 - RhSolutions.sln | 2 - 12 files changed, 107 insertions(+), 131 deletions(-) delete mode 100644 OcrClient/Models/OcrResponse.cs delete mode 100644 OcrClient/OcrClient.csproj delete mode 100644 OcrClient/Services/IOcrClient.cs delete mode 100644 OcrClient/Services/YandexOcrClient.cs create mode 100644 RhSolutions.AddIn/Models/OcrResponse.cs create mode 100644 RhSolutions.AddIn/Services/IOcrClient.cs create mode 100644 RhSolutions.AddIn/Services/YandexOcrClient.cs diff --git a/OcrClient/Models/OcrResponse.cs b/OcrClient/Models/OcrResponse.cs deleted file mode 100644 index 2b26ab7..0000000 --- a/OcrClient/Models/OcrResponse.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace OcrClient.Models; - -public class OcrResponse -{ - 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 deleted file mode 100644 index cdd654f..0000000 --- a/OcrClient/OcrClient.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - net472;net6.0-windows - 10 - enable - enable - - - - - - - - diff --git a/OcrClient/Services/IOcrClient.cs b/OcrClient/Services/IOcrClient.cs deleted file mode 100644 index 9b281c9..0000000 --- a/OcrClient/Services/IOcrClient.cs +++ /dev/null @@ -1,8 +0,0 @@ -using OcrClient.Models; - -namespace OcrClient.Services; - -public interface IOcrClient -{ - public Task> ProcessImage(string base64Image, string xFolderId, string apiKey); -} diff --git a/OcrClient/Services/YandexOcrClient.cs b/OcrClient/Services/YandexOcrClient.cs deleted file mode 100644 index 3146e84..0000000 --- a/OcrClient/Services/YandexOcrClient.cs +++ /dev/null @@ -1,70 +0,0 @@ -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 Enumerable.Empty(); - } -} \ No newline at end of file diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 58ffdf2..0615da1 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -1,5 +1,4 @@ using System.Net; -using OcrClient.Services; namespace RhSolutions.AddIn; diff --git a/RhSolutions.AddIn/Models/OcrResponse.cs b/RhSolutions.AddIn/Models/OcrResponse.cs new file mode 100644 index 0000000..9b6f294 --- /dev/null +++ b/RhSolutions.AddIn/Models/OcrResponse.cs @@ -0,0 +1,28 @@ +namespace RhSolutions.Models; + +public class OcrResponse +{ + 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/RhSolutions.AddIn/RhSolutions.AddIn.csproj b/RhSolutions.AddIn/RhSolutions.AddIn.csproj index 9ba0622..67ef543 100644 --- a/RhSolutions.AddIn/RhSolutions.AddIn.csproj +++ b/RhSolutions.AddIn/RhSolutions.AddIn.csproj @@ -22,7 +22,7 @@ - + @@ -32,7 +32,6 @@ - diff --git a/RhSolutions.AddIn/Services/AddInConfiguration.cs b/RhSolutions.AddIn/Services/AddInConfiguration.cs index 860b6c1..5fa4c1a 100644 --- a/RhSolutions.AddIn/Services/AddInConfiguration.cs +++ b/RhSolutions.AddIn/Services/AddInConfiguration.cs @@ -20,12 +20,8 @@ public class AddInConfiguration : IAddInConfiguration public AddInConfiguration() { - // EmbeddedFileProvider embeddedProvider = new (typeof(RhSolutionsAddIn).Assembly); - // using Stream stream = embeddedProvider.GetFileInfo("appsettings.json").CreateReadStream(); - _configuration = new ConfigurationBuilder() .AddUserSecrets() - // .AddJsonStream(stream) .Build(); _rootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\RhSolutions\RhSolutions-AddIn"); diff --git a/RhSolutions.AddIn/Services/IOcrClient.cs b/RhSolutions.AddIn/Services/IOcrClient.cs new file mode 100644 index 0000000..0233d14 --- /dev/null +++ b/RhSolutions.AddIn/Services/IOcrClient.cs @@ -0,0 +1,8 @@ +using System.Threading.Tasks; + +namespace RhSolutions.Services; + +public interface IOcrClient +{ + public Task> ProcessImage(string base64Image, string xFolderId, string apiKey); +} diff --git a/RhSolutions.AddIn/Services/YandexOcrClient.cs b/RhSolutions.AddIn/Services/YandexOcrClient.cs new file mode 100644 index 0000000..c79dda4 --- /dev/null +++ b/RhSolutions.AddIn/Services/YandexOcrClient.cs @@ -0,0 +1,70 @@ +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace RhSolutions.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 Enumerable.Empty(); + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/OcrTool.cs b/RhSolutions.AddIn/Tools/OcrTool.cs index 89a6fee..2869537 100644 --- a/RhSolutions.AddIn/Tools/OcrTool.cs +++ b/RhSolutions.AddIn/Tools/OcrTool.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using SnippingTool; -using OcrClient.Services; using System.Windows.Forms; using Application = Microsoft.Office.Interop.Excel.Application; diff --git a/RhSolutions.sln b/RhSolutions.sln index 54c4ab1..aa534ab 100644 --- a/RhSolutions.sln +++ b/RhSolutions.sln @@ -11,8 +11,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.ProductSku", "R EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnippingTool", "SnippingTool\SnippingTool.csproj", "{DDB517C7-DF61-4C26-B691-956D0E5906C3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OcrClient", "OcrClient\OcrClient.csproj", "{53F322B3-F477-4831-8E16-EA76934A0D59}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU -- cgit v1.2.3