add support for OCR ...
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using xAiApi.Models;
|
||||||
using xAiModels.Models;
|
using xAiModels.Models;
|
||||||
|
|
||||||
namespace xAiApi.Configurations
|
namespace xAiApi.Configurations
|
||||||
@@ -13,5 +14,10 @@ namespace xAiApi.Configurations
|
|||||||
/// Allowed Prompts to Use ...
|
/// Allowed Prompts to Use ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public XAiPromptDescriptor[] Prompts { get; set; } = [];
|
public XAiPromptDescriptor[] Prompts { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OCR Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
public XAiOCRDescriptor OCR { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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 xAiModels.Models;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using xAiApi.Interfaces.Extractors;
|
using xAiApi.Interfaces.Extractors;
|
||||||
|
using xAiApi.Configurations;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using Tesseract;
|
||||||
|
|
||||||
namespace xAiApi.Providers.Extractors
|
namespace xAiApi.Providers.Extractors
|
||||||
{
|
{
|
||||||
@@ -24,16 +28,25 @@ namespace xAiApi.Providers.Extractors
|
|||||||
];
|
];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// آیا OCR فعال است (برای مدلهای غیر Vision) ...
|
/// AI Api Configuration ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly bool _enableOcrFallback;
|
private readonly XAiApiConfiguration configuration;
|
||||||
|
|
||||||
public XImageFileContentExtractor() : this(enableOcrFallback: true)
|
public XImageFileContentExtractor(
|
||||||
{ }
|
XAiApiConfiguration configuration
|
||||||
|
)
|
||||||
public XImageFileContentExtractor(bool enableOcrFallback = true)
|
|
||||||
{
|
{
|
||||||
_enableOcrFallback = enableOcrFallback;
|
//
|
||||||
|
if (configuration.IsNullOrDefault() ||
|
||||||
|
configuration.OCR.IsNullOrDefault() ||
|
||||||
|
configuration.OCR.DataPath.IsNullOrEmpty() ||
|
||||||
|
configuration.OCR.DefaultLanguage.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
this.configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -103,8 +116,9 @@ namespace xAiApi.Providers.Extractors
|
|||||||
Description = $"Attached image: {fileName}"
|
Description = $"Attached image: {fileName}"
|
||||||
});
|
});
|
||||||
|
|
||||||
// OCR Fallback برای مدلهای متنی
|
//
|
||||||
if (_enableOcrFallback)
|
// OCR Fallback ...
|
||||||
|
if (configuration.OCR.EnableOcrFallback)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
try
|
try
|
||||||
@@ -135,8 +149,44 @@ namespace xAiApi.Providers.Extractors
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// TODO: Complete this ...
|
return await Task.Run(() =>
|
||||||
return await Task.FromResult(string.Empty);
|
{
|
||||||
|
//
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,14 +199,6 @@ namespace xAiApi
|
|||||||
|
|
||||||
//
|
//
|
||||||
services.AddSingleton<IXFileContentExtractor, XFileContentExtractor>();
|
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 ...
|
// Register Default Ai Service ...
|
||||||
|
|||||||
@@ -78,12 +78,18 @@
|
|||||||
"Description": "",
|
"Description": "",
|
||||||
"Params": []
|
"Params": []
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"OCR": {
|
||||||
|
"DataPath": "Models",
|
||||||
|
"EngineMode": "LstmOnly",
|
||||||
|
"EnableOcrFallback": true,
|
||||||
|
"DefaultLanguage": "fas+eng"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
|
"Default": "Information",
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user