-
-
Notifications
You must be signed in to change notification settings - Fork 582
MR for PdfToTextConverter #109
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
Merged
Oceania2018
merged 5 commits into
SciSharp:master
from
evan-cao-wb:add-PdfToTextConverter
Aug 28, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/Infrastructure/BotSharp.Abstraction/Knowledges/IPaddleOcrConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace BotSharp.Abstraction.Knowledges | ||
| { | ||
| public interface IPaddleOcrConverter | ||
| { | ||
| // void LoadModel(); | ||
| Task<string> ConvertImageToText(string loadPath); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Infrastructure/BotSharp.Abstraction/Knowledges/IPdf2TextConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace BotSharp.Abstraction.Knowledges | ||
| { | ||
| public interface IPdf2TextConverter | ||
| { | ||
| Task<string> ConvertPdfToText(IFormFile formFile, int? startPageNum, int? endPageNum); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/PigPdf2TextConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text; | ||
| using Microsoft.AspNetCore.Http; | ||
| using UglyToad.PdfPig; | ||
| using UglyToad.PdfPig.Content; | ||
|
|
||
| namespace BotSharp.Core.Plugins.Knowledges.Services; | ||
|
|
||
| public class PigPdf2TextConverter : IPdf2TextConverter | ||
| { | ||
| public async Task<string> ConvertPdfToText(IFormFile formFile, int? startPageNum, int? endPageNum) | ||
| { | ||
| return await OpenPdfDocumentAsync(formFile, startPageNum, endPageNum); | ||
| } | ||
|
|
||
| private async Task<string> OpenPdfDocumentAsync(IFormFile formFile, int? startPageNum, int? endPageNum) | ||
| { | ||
| if (formFile.Length <= 0) | ||
| { | ||
| return await Task.FromResult(string.Empty); | ||
| } | ||
|
|
||
| var filePath = Path.GetTempFileName(); | ||
|
|
||
| using (var stream = System.IO.File.Create(filePath)) | ||
| { | ||
| await formFile.CopyToAsync(stream); | ||
| } | ||
|
|
||
| var document = PdfDocument.Open(filePath); | ||
| var content = ""; | ||
| foreach (Page page in document.GetPages()) | ||
| { | ||
| if (startPageNum.HasValue && page.Number < startPageNum.Value) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (endPageNum.HasValue && page.Number > endPageNum.Value) | ||
| { | ||
| continue; | ||
| } | ||
| content += page.Text; | ||
| } | ||
| return content; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Plugins/BotSharp.Plugin.PaddleSharp/Providers/PaddleOcrConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using Sdcb.PaddleOCR; | ||
| using Sdcb.PaddleOCR.Models; | ||
| using Sdcb.PaddleInference; | ||
| using Sdcb.PaddleOCR.Models.LocalV3; | ||
| using OpenCvSharp; | ||
| using System.Threading.Tasks; | ||
| using BotSharp.Abstraction.Knowledges; | ||
| using BotSharp.Plugin.PaddleSharp.Settings; | ||
|
|
||
| namespace BotSharp.Plugin.PaddleSharp.Providers; | ||
|
|
||
| public class PaddleOcrConverter : IPaddleOcrConverter | ||
| { | ||
| private FullOcrModel _paddleFullOcrmodel; | ||
| private QueuedPaddleOcrAll _allModel; | ||
| private readonly PaddleSharpSettings _paddleSharpSettings; | ||
|
|
||
| public PaddleOcrConverter(FullOcrModel paddleFullOcrmodel, QueuedPaddleOcrAll allModel, PaddleSharpSettings paddleSharpSettings) | ||
| { | ||
| _paddleFullOcrmodel = paddleFullOcrmodel; | ||
| _allModel = allModel; | ||
| _paddleSharpSettings = paddleSharpSettings; | ||
| } | ||
|
|
||
| private void LoadModel() | ||
| { | ||
| _allModel = new(() => new PaddleOcrAll(_paddleFullOcrmodel, _paddleSharpSettings.device) | ||
| { | ||
| AllowRotateDetection = _paddleSharpSettings.allowRotateDetection, | ||
| Enable180Classification = _paddleSharpSettings.enable180Classification, | ||
| }, consumerCount: _paddleSharpSettings.consumerCount, boundedCapacity: _paddleSharpSettings.boundedCapacity); | ||
| } | ||
|
|
||
| private void DisposeModel() | ||
| { | ||
| _allModel.Dispose(); | ||
| } | ||
|
|
||
| public async Task<string> ConvertImageToText(string loadPath) | ||
| { | ||
| _allModel = new(() => new PaddleOcrAll(_paddleFullOcrmodel, _paddleSharpSettings.device) | ||
| { | ||
| AllowRotateDetection = _paddleSharpSettings.allowRotateDetection, | ||
| Enable180Classification = _paddleSharpSettings.enable180Classification, | ||
| }, consumerCount: _paddleSharpSettings.consumerCount, boundedCapacity: _paddleSharpSettings.boundedCapacity); | ||
|
|
||
| var contents = ""; | ||
| using (Mat src = Cv2.ImRead(loadPath)) | ||
| { | ||
| PaddleOcrResult result = await _allModel.Run(src); | ||
|
|
||
| foreach (PaddleOcrResultRegion region in result.Regions) | ||
| { | ||
| if (region.Score > _paddleSharpSettings.acceptScore) | ||
| { | ||
| contents += region.Text + " "; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| _allModel.Dispose(); | ||
| return contents; | ||
| } | ||
| } | ||
| */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep
PdfPigas one of the implementation ofIPdf2TextConverterrather than delete it.