diff options
Diffstat (limited to 'OcrClient')
-rw-r--r-- | OcrClient/Models/OcrResponse.cs | 28 | ||||
-rw-r--r-- | OcrClient/OcrClient.csproj | 15 | ||||
-rw-r--r-- | OcrClient/Services/IOcrClient.cs | 8 | ||||
-rw-r--r-- | OcrClient/Services/YandexOcrClient.cs | 70 |
4 files changed, 0 insertions, 121 deletions
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<Table>? Tables { get; set; } -} - -public class Table -{ - public string? RowCount { get; set; } - public string? ColumnCount { get; set; } - public List<Cell>? 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 @@ -<Project Sdk="Microsoft.NET.Sdk"> - - <PropertyGroup> - <TargetFrameworks>net472;net6.0-windows</TargetFrameworks> - <LangVersion>10</LangVersion> - <ImplicitUsings>enable</ImplicitUsings> - <Nullable>enable</Nullable> - </PropertyGroup> - - <ItemGroup> - <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> - <PackageReference Include="System.Net.Http" Version="4.3.4" /> - </ItemGroup> - -</Project> 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<IEnumerable<object[,]>> 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<IEnumerable<object[,]>> 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<OcrResponse>(jsonResponse); - - if (deserialized != null) - { - var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty<Table>(); - if (tables.Any()) - { - List<object[,]> 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<object[,]>(); - } -}
\ No newline at end of file |