OcrTool refactoring

This commit is contained in:
Serghei Cebotari 2024-11-09 22:34:58 +03:00
parent cd9c2734f0
commit cc52668afc
11 changed files with 63 additions and 55 deletions

View File

@ -0,0 +1,5 @@
namespace OcrClient.Models;
public class OcrResponse
{
}

View File

@ -0,0 +1,8 @@
using OcrClient.Models;
namespace OcrClient.Services;
public interface IOcrClient
{
public Task<OcrResponse> ProcessImage(string base64Image);
}

View File

@ -0,0 +1,19 @@
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();
}
}

View File

@ -1,5 +1,5 @@
using System.Net;
using VisionClient.Services;
using OcrClient.Services;
namespace RhSolutions.AddIn;
@ -41,7 +41,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
Services.AddTransient<SleevesCalculator>()
.AddTransient<IFittingsCalculator, SleevesCalculator>(s => s.GetService<SleevesCalculator>());
Services.AddTransient<IYandexVisionClient, YandexVisionClient>();
Services.AddTransient<IOcrClient, YandexOcrClient>();
Services.AddSingleton<ToolFactory>();

View File

@ -30,7 +30,7 @@
<ItemGroup>
<ProjectReference Include="..\RhSolutions.ProductSku\RhSolutions.ProductSku.csproj" />
<ProjectReference Include="..\SnippingTool\SnippingTool.csproj" />
<ProjectReference Include="..\VisionClient\VisionClient.csproj" />
<ProjectReference Include="..\OcrClient\OcrClient.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Images\Coupling.png">

View File

@ -1,30 +1,38 @@
using System.Threading.Tasks;
using SnippingTool;
using VisionClient.Services;
using OcrClient.Services;
namespace RhSolutions.Tools;
internal class OcrTool : Tool
{
public Application Application { get; set; }
private IYandexVisionClient client;
public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory, Application application, IYandexVisionClient visionClient) : base(readerFactory, writerFactory)
private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService<IOcrClient>();
public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
Application = application;
client = visionClient;
}
public override void Execute()
{
Application.Visible = false;
try
{
RhSolutionsAddIn.Excel.Visible = false;
Task.Run(async delegate
{
await Task.Delay(100);
await Task.Delay(250);
}).Wait();
string shot = Snipper.SnipBase64();
var result = client.ProcessImage(shot);
Application.Visible = true;
}
catch (Exception)
{
throw;
}
finally
{
RhSolutionsAddIn.Excel.Visible = true;
}
}
}

View File

@ -1,4 +1,4 @@
using VisionClient.Services;
using OcrClient.Services;
namespace RhSolutions.Tools;
@ -7,9 +7,6 @@ internal class ToolFactory
static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>();
static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>();
static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>();
static Application application = RhSolutionsAddIn.ServiceProvider.GetService<Application>();
static IYandexVisionClient yandexClinet = RhSolutionsAddIn.ServiceProvider.GetService<IYandexVisionClient>();
public Tool GetTool(string toolName)
{
Tool tool = toolName switch
@ -21,7 +18,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, yandexClinet),
"ocr" => new OcrTool(readerFactory, writerFactory),
_ => throw new Exception($"Неизвестный инструмент {toolName}"),
};
return tool;

View File

@ -11,7 +11,7 @@ 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}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OcrClient", "OcrClient\OcrClient.csproj", "{53F322B3-F477-4831-8E16-EA76934A0D59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,5 +0,0 @@
namespace VisionClient.Models;
public class VisionResponse
{
}

View File

@ -1,24 +0,0 @@
using VisionClient.Models;
using System.Net.Http;
namespace VisionClient.Services;
public interface IYandexVisionClient
{
public Task<VisionResponse> ProcessImage(string base64Image);
}
public class YandexVisionClient : IYandexVisionClient
{
private readonly HttpClient _httpClient;
public YandexVisionClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public Task<VisionResponse> ProcessImage(string base64Image)
{
throw new NotImplementedException();
}
}