add support for OCR ...

This commit is contained in:
2026-10-03 22:26:03 +03:30
parent 7cad889cd0
commit 70c2d5a649
5 changed files with 102 additions and 21 deletions
+6
View File
@@ -1,3 +1,4 @@
using xAiApi.Models;
using xAiModels.Models;
namespace xAiApi.Configurations
@@ -13,5 +14,10 @@ namespace xAiApi.Configurations
/// Allowed Prompts to Use ...
/// </summary>
public XAiPromptDescriptor[] Prompts { get; set; } = [];
/// <summary>
/// OCR Configuration ...
/// </summary>
public XAiOCRDescriptor OCR { get; set; }
}
}
+27
View File
@@ -0,0 +1,27 @@
using Tesseract;
namespace xAiApi.Models
{
public class XAiOCRDescriptor
{
/// <summary>
/// Provides Trained Data Path ...
/// </summary>
public string DataPath { get; set; }
/// <summary>
/// The Engine for Doing OCR ...
/// </summary>
public EngineMode EngineMode { get; set; }
/// <summary>
/// Enable OCR Fallback using Model ...
/// </summary>
public bool EnableOcrFallback { get; set; }
/// <summary>
/// Default OCR Language ...
/// </summary>
public string DefaultLanguage { get; set; }
}
}
@@ -5,6 +5,10 @@ using System.Threading;
using xAiModels.Models;
using System.Threading.Tasks;
using xAiApi.Interfaces.Extractors;
using xAiApi.Configurations;
using xCommons.Extensions;
using xExceptions.Constants;
using Tesseract;
namespace xAiApi.Providers.Extractors
{
@@ -24,16 +28,25 @@ namespace xAiApi.Providers.Extractors
];
/// <summary>
/// آیا OCR فعال است (برای مدل‌های غیر Vision) ...
/// AI Api Configuration ...
/// </summary>
private readonly bool _enableOcrFallback;
private readonly XAiApiConfiguration configuration;
public XImageFileContentExtractor() : this(enableOcrFallback: true)
{ }
public XImageFileContentExtractor(bool enableOcrFallback = true)
public XImageFileContentExtractor(
XAiApiConfiguration configuration
)
{
_enableOcrFallback = enableOcrFallback;
//
if (configuration.IsNullOrDefault() ||
configuration.OCR.IsNullOrDefault() ||
configuration.OCR.DataPath.IsNullOrEmpty() ||
configuration.OCR.DefaultLanguage.IsNullOrEmpty())
{
XException.InvalidConfiguration.Throw();
}
//
this.configuration = configuration;
}
/// <summary>
@@ -103,8 +116,9 @@ namespace xAiApi.Providers.Extractors
Description = $"Attached image: {fileName}"
});
// OCR Fallback برای مدل‌های متنی
if (_enableOcrFallback)
//
// OCR Fallback ...
if (configuration.OCR.EnableOcrFallback)
{
//
try
@@ -135,8 +149,44 @@ namespace xAiApi.Providers.Extractors
)
{
//
// TODO: Complete this ...
return await Task.FromResult(string.Empty);
return await Task.Run(() =>
{
//
// Validate Path ...
if (!Directory.Exists(configuration.OCR.DataPath))
{
//
throw new DirectoryNotFoundException(
$"TessData directory not found at: {configuration.OCR.DataPath}. " +
"Please download traineddata files from https://github.com/tesseract-ocr/tessdata"
);
}
//
using var engine = new TesseractEngine(
datapath: configuration.OCR.DataPath,
language: configuration.OCR.DefaultLanguage,
engineMode: configuration.OCR.EngineMode
);
//
using var pix = Pix.LoadFromMemory(imageBytes);
//
cancellationToken.ThrowIfCancellationRequested();
//
using var page = engine.Process(pix);
//
var text = page.GetText();
//
text = text?.Trim();
//
return text ?? string.Empty;
}, cancellationToken);
}
}
}
-8
View File
@@ -199,14 +199,6 @@ namespace xAiApi
//
services.AddSingleton<IXFileContentExtractor, XFileContentExtractor>();
// services.AddSingleton(sp =>
// {
// //
// var extractors = sp.GetServices<IXFileContentExtractor>();
// return new XFileContentExtractor(extractors);
// });
// services.AddSingleton<IXFileContentExtractor>(sp =>
// sp.GetRequiredService<XFileContentExtractor>());
//
// Register Default Ai Service ...
+8 -2
View File
@@ -78,12 +78,18 @@
"Description": "",
"Params": []
}
]
],
"OCR": {
"DataPath": "Models",
"EngineMode": "LstmOnly",
"EnableOcrFallback": true,
"DefaultLanguage": "fas+eng"
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},