POS-24 Dodanie prostego rozpoznawania tekstu

This commit is contained in:
s426226 2020-12-12 18:11:56 +01:00
parent 4447d72032
commit dd74f059da
12 changed files with 73 additions and 9 deletions

View File

@ -29,9 +29,9 @@ namespace Serwer.Api.Controllers
using (var memoryStream = new MemoryStream())
{
await image.CopyToAsync(memoryStream);
await _imageService.Process(image.Name, image.ContentType, memoryStream.ToArray());
}
return Ok();
var response = await _imageService.Process(image.Name, image.ContentType, memoryStream.ToArray());
return Ok(response);
}
}
}
}

View File

@ -4,6 +4,10 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Remove="wwwroot\tessdata\eng.traineddata" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
@ -15,4 +19,8 @@
<ProjectReference Include="..\Serwer.Infrastructure\Serwer.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="wwwroot\tessdata\eng.traineddata" />
</ItemGroup>
</Project>

View File

@ -25,12 +25,14 @@ namespace Serwer.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
WebRootPath = env.WebRootPath;
}
public IConfiguration Configuration { get; }
protected string WebRootPath { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
@ -41,6 +43,7 @@ namespace Serwer.Api
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Serwer.Api", Version = "v1" });
});
var hostEnviroment = new HostEnviroment { RootPath = WebRootPath};
var jwtSettings = new JwtSettings()
{
Issuer = "PoszukiwaczInc",
@ -60,6 +63,7 @@ namespace Serwer.Api
};
});
services.AddSingleton<IHostEnviroment>(hostEnviroment);
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
services.AddScoped<IUserRepository, UserRepository>();
@ -82,8 +86,6 @@ namespace Serwer.Api
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Serwer.Api v1"));
}
app.UseRouting();
app.UseAuthentication();

Binary file not shown.

After

Width:  |  Height:  |  Size: 932 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,6 +9,6 @@ namespace Serwer.Infrastructure.Services
{
public interface IImageService
{
Task Process(string name, string contentType, byte[] bytes);
Task<string> Process(string name, string contentType, byte[] bytes);
}
}

View File

@ -1,19 +1,30 @@
using Serwer.Core.Domain;
using Serwer.Infrastructure.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;
namespace Serwer.Infrastructure.Services
{
public class ImageService : IImageService
{
private readonly ISet<File> _files = new HashSet<File>();
public async Task Process(string name, string contentType, byte[] bytes)
private readonly string _env;
public ImageService(IHostEnviroment hostEnviroment)
{
var file = File.Create(name, contentType, bytes);
await Task.FromResult(_files.Add(file));
_env = hostEnviroment.RootPath;
}
public async Task<string> Process(string name, string contentType, byte[] bytes)
{
var engine = new TesseractEngine(System.IO.Path.Combine(_env, "tessdata"),"eng+equ", EngineMode.Default);
var img = Pix.LoadFromMemory(bytes);
var res = engine.Process(img);
var txt = res.GetText();
return await Task.FromResult(txt);
}
}
}

View File

@ -8,10 +8,27 @@
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
<PackageReference Include="Tesseract" Version="4.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Serwer.Core\Serwer.Core.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="C:\Users\ppopo\.nuget\packages\tesseract\4.1.1\build\\..\x64\leptonica-1.80.0.dll" />
</ItemGroup>
<ItemGroup>
<None Remove="C:\Users\ppopo\.nuget\packages\tesseract\4.1.1\build\\..\x64\tesseract41.dll" />
</ItemGroup>
<ItemGroup>
<None Remove="C:\Users\ppopo\.nuget\packages\tesseract\4.1.1\build\\..\x86\leptonica-1.80.0.dll" />
</ItemGroup>
<ItemGroup>
<None Remove="C:\Users\ppopo\.nuget\packages\tesseract\4.1.1\build\\..\x86\tesseract41.dll" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serwer.Infrastructure.Settings
{
public class HostEnviroment: IHostEnviroment
{
public string RootPath { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serwer.Infrastructure.Settings
{
public interface IHostEnviroment
{
string RootPath { get; set; }
}
}