Skip to content
Open
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
41 changes: 41 additions & 0 deletions TelegramSearchBot.OCR/OCRBootstrap.cs
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}", "");
}
}
}
}
}
70 changes: 70 additions & 0 deletions TelegramSearchBot.OCR/PaddleOCR.cs
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;
}
}
}
28 changes: 28 additions & 0 deletions TelegramSearchBot.OCR/TelegramSearchBot.OCR.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project targets net9.0 but 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.

Copilot uses AI. Check for mistakes.
<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>
6 changes: 6 additions & 0 deletions TelegramSearchBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramSearchBot.Search",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramSearchBot.Search.Test", "TelegramSearchBot.Search.Test\TelegramSearchBot.Search.Test.csproj", "{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramSearchBot.OCR", "TelegramSearchBot.OCR\TelegramSearchBot.OCR.csproj", "{B8C4D5E6-F7A8-4B9C-8D1E-2F3A4B5C6D7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -51,6 +53,10 @@ Global
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Release|Any CPU.Build.0 = Release|Any CPU
{B8C4D5E6-F7A8-4B9C-8D1E-2F3A4B5C6D7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8C4D5E6-F7A8-4B9C-8D1E-2F3A4B5C6D7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8C4D5E6-F7A8-4B9C-8D1E-2F3A4B5C6D7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8C4D5E6-F7A8-4B9C-8D1E-2F3A4B5C6D7E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion TelegramSearchBot/Service/Abstract/SubProcessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public async Task<string> RunRpc(string payload) {
return await db.StringWaitGetDeleteAsync($"{ForkName}Result-{guid}");
}
}
}
}
1 change: 1 addition & 0 deletions TelegramSearchBot/TelegramSearchBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<ItemGroup>
<ProjectReference Include="..\TelegramSearchBot.Common\TelegramSearchBot.Common.csproj" />
<ProjectReference Include="..\TelegramSearchBot.Search\TelegramSearchBot.Search.csproj" />
<ProjectReference Include="..\TelegramSearchBot.OCR\TelegramSearchBot.OCR.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
Expand Down
Loading