aboutsummaryrefslogtreecommitdiff
path: root/OcrClient/Services
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2024-11-13 23:42:28 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2024-11-13 23:42:28 +0300
commitf8c62c6defa5c8c5ac06450bbcf1dc558b6e1a32 (patch)
treedaa731f1233940c93db603f319bb138d277a84bb /OcrClient/Services
parent3800c961166dbcc4553dd846d0291388e60d7e6f (diff)
Implement IOcrClient
Diffstat (limited to 'OcrClient/Services')
-rw-r--r--OcrClient/Services/IOcrClient.cs2
-rw-r--r--OcrClient/Services/OcrClient.cs19
-rw-r--r--OcrClient/Services/YandexOcrClient.cs70
3 files changed, 71 insertions, 20 deletions
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<OcrResponse> ProcessImage(string base64Image);
+ public Task<IEnumerable<object[,]>> 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<OcrResponse> 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<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 null;
+ }
+} \ No newline at end of file