Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍒 improve document OCR performance #491

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Application/Features/AuditTrails/DTOs/AuditTrailDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CleanArchitecture.Blazor.Application.Features.AuditTrails.DTOs;
[Description("Audit Trails")]
public class AuditTrailDto
{
[Description("Id")] public string Id { get; set; } = Guid.NewGuid().ToString();
[Description("Id")] public int Id { get; set; }

[Description("User Id")] public string? UserId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public async Task<Result<int>> Handle(UploadDocumentCommand request, Cancellatio
var url = await _uploadService.UploadAsync(uploadRequest);
var document = new Document
{
Title = fileName, URL = url, Status = JobStatus.Queueing, IsPublic = true,
DocumentType = DocumentType.Image
Title = fileName,
URL = url,
Status = JobStatus.Queueing,
IsPublic = true,
DocumentType = DocumentType.Image,

};
document.AddDomainEvent(new CreatedEvent<Document>(document));
list.Add(document);
Expand All @@ -55,5 +59,6 @@ public async Task<Result<int>> Handle(UploadDocumentCommand request, Cancellatio
await _context.Documents.AddRangeAsync(list, cancellationToken);
var result = await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result);

}
}
3 changes: 1 addition & 2 deletions src/Blazor.Server.UI/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
</LoadingScreen>

@code{

[Inject]
private LayoutService LayoutService { get; set; }
private LayoutService LayoutService { get; set; } = default!;

protected override async Task OnInitializedAsync()
{
Expand Down
5 changes: 2 additions & 3 deletions src/Blazor.Server.UI/Components/Shared/LoadingScreen.razor
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ else
}

@code {

[Parameter]
public RenderFragment ChildContent { get; set; }
public RenderFragment ChildContent { get; set; } = null!;

[Parameter]
public LayoutService LayoutService { get; set; }
public LayoutService LayoutService { get; set; } = null!;

[Parameter]
public int LoadingDelayMs { get; set; }
Expand Down
8 changes: 4 additions & 4 deletions src/Blazor.Server.UI/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"UseInMemoryDatabase": false,
"DatabaseSettings": {
"DBProvider": "sqlite",
"ConnectionString": "Data Source=BlazorDashboardDb.db"
//"DBProvider": "mssql",
//"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=BlazorDashboardDb;Trusted_Connection=True;MultipleActiveResultSets=true;"
//"DBProvider": "sqlite",
//"ConnectionString": "Data Source=BlazorDashboardDb.db"
"DBProvider": "mssql",
"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=BlazorDashboardDb;Trusted_Connection=True;MultipleActiveResultSets=true;"
//"DBProvider": "postgresql",
//"ConnectionString": "Server=127.0.0.1;Database=BlazorDashboardDb;User Id=postgres;Password=postgrespw;Port=32768"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Entities/Audit/AuditTrail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace CleanArchitecture.Blazor.Domain.Entities.Audit;

public class AuditTrail : IEntity<string>
public class AuditTrail : IEntity<int>
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public int Id { get; set; }
public string? UserId { get; set; }
public virtual ApplicationUser? Owner { get; set; }
public AuditType AuditType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class HttpClientServiceCollectionExtensions
public static void AddHttpClientService(this IServiceCollection services)
=> services.AddHttpClient("ocr", c =>
{
c.BaseAddress = new Uri("https://paddleocr.i247365.net/predict/ocr_system");
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
c.BaseAddress = new Uri("https://paddleocr.blazorserver.com/uploadocr");
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
}).AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(30)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class AuditableEntityInterceptor : SaveChangesInterceptor
private readonly ICurrentUserService _currentUserService;
private readonly IDateTime _dateTime;
private List<AuditTrail> _temporaryAuditTrailList = new();
private List<DomainEvent> _deletingDomainEvents = new();
public AuditableEntityInterceptor(
ITenantProvider tenantProvider,
ICurrentUserService currentUserService,
Expand Down
32 changes: 14 additions & 18 deletions src/Infrastructure/Services/PaddleOCR/DocumentOcrJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,25 @@ public async Task Recognition(int id, CancellationToken cancellationToken)
if (string.IsNullOrEmpty(doc.URL)) return;
var imgFile = Path.Combine(Directory.GetCurrentDirectory(), doc.URL);
if (!File.Exists(imgFile)) return;
string base64String = ReadBase64String(imgFile);

var response = client.PostAsJsonAsync<dynamic>("", new { images = new string[] { base64String } }).Result;
// Create multipart/form-data content
using var form = new MultipartFormDataContent();
using var fileStream = new FileStream(imgFile, FileMode.Open);
using var fileContent = new StreamContent(fileStream);

form.Add(fileContent, "file", Path.GetFileName(imgFile)); // "image" is the form parameter name for the file

var response = await client.PostAsync("", form);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var result = await response.Content.ReadAsStringAsync();
var ocrResult = JsonSerializer.Deserialize<OcrResult>(result);
var ocrStatus = ocrResult!.status;
doc.Status = JobStatus.Done;
doc.Description = "recognize the result: " + ocrStatus;
if (ocrResult.status == "000")

if (ocrResult is not null)
{
var content = _serializer.Serialize(ocrResult.results);
doc!.Content = content;

var content = string.Join(',', ocrResult.data);
doc.Description = $"recognize the result: success";
doc.Content = content;
}
await _context.SaveChangesAsync(cancellationToken);
await _hubContext.Clients.All.Completed(doc.Title!);
Expand All @@ -108,17 +112,9 @@ public async Task Recognition(int id, CancellationToken cancellationToken)

}
#pragma warning disable CS8981
class result
{
public decimal confidence { get; set; }
public string text { get; set; } = String.Empty;
public List<int[]> text_region { get; set; } = new();
}
class OcrResult
{
public string msg { get; set; } = String.Empty;
public List<result[]> results { get; set; } = new();
public string status { get; set; }=String.Empty;
public string[] data { get; set; } = Array.Empty<string>();
}
#pragma warning restore CS8981

Loading