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.AI.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;
Comment on lines +3 to +5
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.

Several using directives are unused: System.Linq, System.Net, and System.Text. These should be removed to keep the code clean.

Suggested change
using System.Linq;
using System.Net;
using System.Text;

Copilot uses AI. Check for mistakes.
using System.Threading.Tasks;
using StackExchange.Redis;
using TelegramSearchBot.AI.OCR;
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 using TelegramSearchBot.AI.OCR; directive is redundant since this file is already in the TelegramSearchBot.AI.OCR namespace. This self-referencing import should be removed.

Suggested change
using TelegramSearchBot.AI.OCR;

Copilot uses AI. Check for mistakes.

namespace TelegramSearchBot.AI.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}", "");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using TelegramSearchBot.Common.Model;
using TelegramSearchBot.Common.Model.DO;

namespace TelegramSearchBot.Manager {
namespace TelegramSearchBot.AI.OCR {
public class PaddleOCR {
public PaddleOcrAll all { get; set; }
private static SemaphoreSlim semaphore = new SemaphoreSlim(1);
Expand Down
21 changes: 21 additions & 0 deletions TelegramSearchBot.AI.OCR/TelegramSearchBot.AI.OCR.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\TelegramSearchBot.Common\TelegramSearchBot.Common.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.11.0.20250507" />
<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="StackExchange.Redis" Version="2.9.17" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
58 changes: 58 additions & 0 deletions TelegramSearchBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,90 @@ 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.AI.OCR", "TelegramSearchBot.AI.OCR\TelegramSearchBot.AI.OCR.csproj", "{66FE375A-7036-4738-987E-96A5C609DAD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Debug|x64.ActiveCfg = Debug|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Debug|x64.Build.0 = Debug|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Debug|x86.ActiveCfg = Debug|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Debug|x86.Build.0 = Debug|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Release|Any CPU.Build.0 = Release|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Release|x64.ActiveCfg = Release|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Release|x64.Build.0 = Release|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Release|x86.ActiveCfg = Release|Any CPU
{85931FBE-F0AF-4EC9-B67F-B5D2E409421A}.Release|x86.Build.0 = Release|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Debug|x64.ActiveCfg = Debug|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Debug|x64.Build.0 = Debug|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Debug|x86.ActiveCfg = Debug|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Debug|x86.Build.0 = Debug|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Release|Any CPU.Build.0 = Release|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Release|x64.ActiveCfg = Release|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Release|x64.Build.0 = Release|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Release|x86.ActiveCfg = Release|Any CPU
{902F87DC-F692-4A49-8F18-DF42A1FB351D}.Release|x86.Build.0 = Release|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Debug|x64.ActiveCfg = Debug|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Debug|x64.Build.0 = Debug|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Debug|x86.ActiveCfg = Debug|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Debug|x86.Build.0 = Debug|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Release|Any CPU.Build.0 = Release|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Release|x64.ActiveCfg = Release|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Release|x64.Build.0 = Release|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Release|x86.ActiveCfg = Release|Any CPU
{B0569DC1-B927-41C8-B888-05513A97EE81}.Release|x86.Build.0 = Release|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Debug|x64.ActiveCfg = Debug|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Debug|x64.Build.0 = Debug|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Debug|x86.ActiveCfg = Debug|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Debug|x86.Build.0 = Debug|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Release|Any CPU.Build.0 = Release|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Release|x64.ActiveCfg = Release|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Release|x64.Build.0 = Release|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Release|x86.ActiveCfg = Release|Any CPU
{DBFD8C03-8128-428C-A2B1-47A24198FEF1}.Release|x86.Build.0 = Release|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|x64.ActiveCfg = Debug|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|x64.Build.0 = Debug|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|x86.ActiveCfg = Debug|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Debug|x86.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
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Release|x64.ActiveCfg = Release|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Release|x64.Build.0 = Release|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Release|x86.ActiveCfg = Release|Any CPU
{A17FCB3D-FF05-46CD-A60E-6E43470A5AB3}.Release|x86.Build.0 = Release|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Debug|x64.ActiveCfg = Debug|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Debug|x64.Build.0 = Debug|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Debug|x86.ActiveCfg = Debug|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Debug|x86.Build.0 = Debug|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Release|Any CPU.Build.0 = Release|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Release|x64.ActiveCfg = Release|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Release|x64.Build.0 = Release|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Release|x86.ActiveCfg = Release|Any CPU
{66FE375A-7036-4738-987E-96A5C609DAD8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
30 changes: 3 additions & 27 deletions TelegramSearchBot/AppBootstrap/OCRBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,13 @@
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
using TelegramSearchBot.Manager;
using TelegramSearchBot.AI.OCR;

namespace TelegramSearchBot.AppBootstrap {
public class OCRBootstrap : AppBootstrap {
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}", "");
}
}
// 转发到AI.OCR项目的OCRBootstrap
TelegramSearchBot.AI.OCR.OCRBootstrap.Startup(args);
}
}
}
1 change: 0 additions & 1 deletion TelegramSearchBot/Extension/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static IServiceCollection AddCoreServices(this IServiceCollection service
.AddHostedService<SendMessage>()
.AddSingleton<Func<string, Task>>(sp => sp.GetRequiredService<SendMessage>().Log)
.AddSingleton<LuceneManager>()
.AddSingleton<PaddleOCR>()
.AddSingleton<WhisperManager>();
}

Expand Down
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.AI.OCR\TelegramSearchBot.AI.OCR.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
Expand Down
Loading