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/Services/IOcrClient.cs | 8 ---- OcrClient/Services/YandexOcrClient.cs | 70 ----------------------------------- 2 files changed, 78 deletions(-) delete mode 100644 OcrClient/Services/IOcrClient.cs delete mode 100644 OcrClient/Services/YandexOcrClient.cs (limited to 'OcrClient/Services') 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 -- cgit v1.2.3