-
Notifications
You must be signed in to change notification settings - Fork 2
feat: split OCR functionality into independent project #172
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
Open
ModerRAS
wants to merge
7
commits into
master
Choose a base branch
from
feature/split-ocr-project
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
82db8c5
feat: split OCR functionality into independent project
ModerRAS cafd597
feat: update OCR split to maintain original fork behavior
ModerRAS 6744d64
feat: split OCR functionality into independent library project
ModerRAS e935e8a
修复PaddleOCRService中的命名空间错误,确保OCR拆分项目能够正确编译
ModerRAS bd04d25
修正OCR拆分项目中的错误,保持PaddleOCRService逻辑不变
ModerRAS 3e11c62
修正PaddleOCRService,保持原始简单逻辑不变
ModerRAS 4f703d8
简化OCR项目逻辑,保持与原始代码完全一致
ModerRAS 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
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,41 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using StackExchange.Redis; | ||
| using TelegramSearchBot.OCR; | ||
|
|
||
| namespace TelegramSearchBot.OCR { | ||
| public class OCRBootstrap { | ||
| public static void Startup(string[] args) { | ||
| ConnectionMultiplexer redis = ConnectionMultiplexer.Connect($"localhost:{args[1]}"); | ||
| IDatabase db = redis.GetDatabase(); | ||
| var ocr = new PaddleOCR(); | ||
| var before = DateTime.UtcNow; | ||
| while (DateTime.UtcNow - before < TimeSpan.FromMinutes(10) || | ||
| db.ListLength("OCRTasks") > 0) { | ||
| if (db.ListLength("OCRTasks") == 0) { | ||
| Task.Delay(1000).Wait(); | ||
| continue; | ||
| } | ||
| var task = db.ListLeftPop("OCRTasks").ToString(); | ||
| var photoBase64 = db.StringGetDelete($"OCRPost-{task}").ToString(); | ||
| var response = ocr.Execute(new List<string>() { photoBase64 }); | ||
| int status; | ||
| if (int.TryParse(response.Status, out status) && status == 0) { | ||
| var StringList = new List<string>(); | ||
| foreach (var e in response.Results) { | ||
| foreach (var f in e) { | ||
| StringList.Add(f.Text); | ||
| } | ||
| } | ||
| db.StringSet($"OCRResult-{task}", string.Join(" ", StringList)); | ||
| } else { | ||
| db.StringSet($"OCRResult-{task}", ""); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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,70 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using OpenCvSharp; | ||
| using Sdcb.PaddleInference; | ||
| using Sdcb.PaddleOCR; | ||
| using Sdcb.PaddleOCR.Models; | ||
| using Sdcb.PaddleOCR.Models.Local; | ||
| using TelegramSearchBot.Common.Model; | ||
| using TelegramSearchBot.Common.Model.DO; | ||
|
|
||
| namespace TelegramSearchBot.OCR { | ||
| public class PaddleOCR { | ||
| public PaddleOcrAll all { get; set; } | ||
| private static SemaphoreSlim semaphore = new SemaphoreSlim(1); | ||
| public PaddleOCR() { | ||
| FullOcrModel model = LocalFullModels.ChineseV3; | ||
|
|
||
| all = new PaddleOcrAll(model, | ||
| PaddleDevice.Mkldnn() | ||
| ) { | ||
| AllowRotateDetection = true, /* 允许识别有角度的文字 */ | ||
| Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */ | ||
| }; | ||
| } | ||
|
|
||
| public PaddleOcrResult GetOcrResult(byte[] image) { | ||
| using (Mat src = Cv2.ImDecode(image, ImreadModes.Color)) { | ||
| PaddleOcrResult result = all.Run(src); | ||
| return result; | ||
| } | ||
| } | ||
| public List<Result> ConvertToResults(PaddleOcrResult paddleOcrResult) { | ||
| var results = new List<Result>(); | ||
| foreach (var region in paddleOcrResult.Regions) { | ||
| results.Add(new Result { | ||
| Text = region.Text, | ||
| TextRegion = region.Rect.Points().Select(point => { | ||
| return new List<int>() { ( int ) point.X, ( int ) point.Y }; | ||
| }).ToList(), | ||
| Confidence = float.IsNaN(region.Score) ? 0 : region.Score, | ||
| }); | ||
| } | ||
| return results; | ||
| } | ||
| public PaddleOCRResult Execute(List<string> images) { | ||
| var results = images | ||
| .Select(Convert.FromBase64String) | ||
| .Select(GetOcrResult) | ||
| .Select(ConvertToResults) | ||
| .ToList(); | ||
| return new PaddleOCRResult() { | ||
| Results = results, | ||
| Status = "0", | ||
| Message = "", | ||
| }; | ||
| } | ||
| public async Task<PaddleOCRResult> ExecuteAsync(List<string> images) { | ||
| await semaphore.WaitAsync().ConfigureAwait(false); | ||
| var results = await Task.Run<PaddleOCRResult>(() => Execute(images)); | ||
| semaphore.Release(); | ||
| return results; | ||
| } | ||
| } | ||
| } |
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,28 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.9" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.9" /> | ||
| <PackageReference Include="StackExchange.Redis" Version="2.9.17" /> | ||
| <PackageReference Include="Sdcb.PaddleInference" Version="3.0.1" /> | ||
| <PackageReference Include="Sdcb.PaddleOCR" Version="3.0.1" /> | ||
| <PackageReference Include="Sdcb.PaddleOCR.Models.Local" Version="3.0.1" /> | ||
| <PackageReference Include="OpenCvSharp4.Extensions" Version="4.11.0.20250507" /> | ||
| <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" /> | ||
| <PackageReference Include="OpenCvSharp4.Windows" Version="4.11.0.20250507" /> | ||
| <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
| <PackageReference Include="SkiaSharp" Version="3.119.0" /> | ||
| <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.119.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\TelegramSearchBot.Common\TelegramSearchBot.Common.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
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
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.
The project targets
net9.0but all Microsoft.Extensions packages are using version 7.0.x (lines 11-16). This version mismatch can cause compatibility issues. For .NET 9.0, you should use Microsoft.Extensions.* packages version 9.0.0 to match the target framework and align with the main project's dependencies.