From cd9c2734f08f45d19e92f39c5c2c0b1344652b58 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Fri, 8 Nov 2024 09:23:36 +0300 Subject: [PATCH] Add Yandex Vision OCR client --- RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 3 +++ RhSolutions.AddIn/RhSolutions.AddIn.csproj | 3 ++- RhSolutions.AddIn/Tools/OcrTool.cs | 7 +++++- RhSolutions.AddIn/Tools/ToolFactory.cs | 7 ++++-- RhSolutions.sln | 6 +++++ VisionClient/Models/VisionResponse.cs | 5 ++++ VisionClient/Services/IYandexVisionClient.cs | 24 ++++++++++++++++++++ VisionClient/VisionClient.csproj | 14 ++++++++++++ 8 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 VisionClient/Models/VisionResponse.cs create mode 100644 VisionClient/Services/IYandexVisionClient.cs create mode 100644 VisionClient/VisionClient.csproj diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 1666df4..ac83200 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -1,4 +1,5 @@ using System.Net; +using VisionClient.Services; namespace RhSolutions.AddIn; @@ -39,6 +40,8 @@ public sealed class RhSolutionsAddIn : IExcelAddIn .AddTransient(s => s.GetService()); Services.AddTransient() .AddTransient(s => s.GetService()); + + Services.AddTransient(); Services.AddSingleton(); diff --git a/RhSolutions.AddIn/RhSolutions.AddIn.csproj b/RhSolutions.AddIn/RhSolutions.AddIn.csproj index 4589a49..6e34066 100644 --- a/RhSolutions.AddIn/RhSolutions.AddIn.csproj +++ b/RhSolutions.AddIn/RhSolutions.AddIn.csproj @@ -1,4 +1,4 @@ - + net472;net6.0-windows 10 @@ -30,6 +30,7 @@ + diff --git a/RhSolutions.AddIn/Tools/OcrTool.cs b/RhSolutions.AddIn/Tools/OcrTool.cs index f407db8..b717bba 100644 --- a/RhSolutions.AddIn/Tools/OcrTool.cs +++ b/RhSolutions.AddIn/Tools/OcrTool.cs @@ -1,14 +1,17 @@ using System.Threading.Tasks; using SnippingTool; +using VisionClient.Services; namespace RhSolutions.Tools; internal class OcrTool : Tool { public Application Application { get; set; } - public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory, Application application) : base(readerFactory, writerFactory) + private IYandexVisionClient client; + public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory, Application application, IYandexVisionClient visionClient) : base(readerFactory, writerFactory) { Application = application; + client = visionClient; } public override void Execute() @@ -20,6 +23,8 @@ internal class OcrTool : Tool }).Wait(); string shot = Snipper.SnipBase64(); + var result = client.ProcessImage(shot); + Application.Visible = true; } } \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/ToolFactory.cs b/RhSolutions.AddIn/Tools/ToolFactory.cs index 045e893..0cb740c 100644 --- a/RhSolutions.AddIn/Tools/ToolFactory.cs +++ b/RhSolutions.AddIn/Tools/ToolFactory.cs @@ -1,4 +1,6 @@ -namespace RhSolutions.Tools; +using VisionClient.Services; + +namespace RhSolutions.Tools; internal class ToolFactory { @@ -6,6 +8,7 @@ internal class ToolFactory static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService(); static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService(); static Application application = RhSolutionsAddIn.ServiceProvider.GetService(); + static IYandexVisionClient yandexClinet = RhSolutionsAddIn.ServiceProvider.GetService(); public Tool GetTool(string toolName) { @@ -18,7 +21,7 @@ internal class ToolFactory "guess" => new GuessTool(readerFactory, writerFactory), "fillsleeves" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Sleeves"), "fillcouplings" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Couplings"), - "ocr" => new OcrTool(readerFactory, writerFactory, application), + "ocr" => new OcrTool(readerFactory, writerFactory, application, yandexClinet), _ => throw new Exception($"Неизвестный инструмент {toolName}"), }; return tool; diff --git a/RhSolutions.sln b/RhSolutions.sln index a47ff74..ccbd318 100644 --- a/RhSolutions.sln +++ b/RhSolutions.sln @@ -11,6 +11,8 @@ 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}") = "VisionClient", "VisionClient\VisionClient.csproj", "{53F322B3-F477-4831-8E16-EA76934A0D59}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {DDB517C7-DF61-4C26-B691-956D0E5906C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {DDB517C7-DF61-4C26-B691-956D0E5906C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {DDB517C7-DF61-4C26-B691-956D0E5906C3}.Release|Any CPU.Build.0 = Release|Any CPU + {53F322B3-F477-4831-8E16-EA76934A0D59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53F322B3-F477-4831-8E16-EA76934A0D59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53F322B3-F477-4831-8E16-EA76934A0D59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53F322B3-F477-4831-8E16-EA76934A0D59}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VisionClient/Models/VisionResponse.cs b/VisionClient/Models/VisionResponse.cs new file mode 100644 index 0000000..23a32fd --- /dev/null +++ b/VisionClient/Models/VisionResponse.cs @@ -0,0 +1,5 @@ +namespace VisionClient.Models; + +public class VisionResponse +{ +} \ No newline at end of file diff --git a/VisionClient/Services/IYandexVisionClient.cs b/VisionClient/Services/IYandexVisionClient.cs new file mode 100644 index 0000000..77105be --- /dev/null +++ b/VisionClient/Services/IYandexVisionClient.cs @@ -0,0 +1,24 @@ +using VisionClient.Models; +using System.Net.Http; + +namespace VisionClient.Services; + +public interface IYandexVisionClient +{ + public Task ProcessImage(string base64Image); +} + +public class YandexVisionClient : IYandexVisionClient +{ + private readonly HttpClient _httpClient; + + public YandexVisionClient(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public Task ProcessImage(string base64Image) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/VisionClient/VisionClient.csproj b/VisionClient/VisionClient.csproj new file mode 100644 index 0000000..4f78021 --- /dev/null +++ b/VisionClient/VisionClient.csproj @@ -0,0 +1,14 @@ + + + + net472;net6.0-windows + 10 + enable + enable + + + + + + +