自动化图像档案的关键词审计确保您的视觉数据是连续标记和易于发现的. 使用 Aspose.OCR for .NET,您可以从图形中阅读内置/可见的文本,并对控制关键字列表进行验证 - 然后报告缺乏什么。

完整的例子

原則

  • ** .NET 8 (或 .Net 6+)** SDK 已安装。
  • NuGet 可安装 Aspose.OCR.
  • 一个图像文件夹进行审计(例如, C:\Path\To\ImageArchive).
  • (可选) 如果您计划超越评估限制,则提供一份 ASPOSE 许可文件。

创建项目 & 添加包

dotnet new console -n ImageArchiveKeywordAudit -f net8.0
cd ImageArchiveKeywordAudit
dotnet add package Aspose.OCR

步骤1 - 准备你的关键字列表

gist 中,关键字为简单而硬编码:

// Exact shape used in the gist
List<string> keywords = new List<string>
{
    "mountains", "beaches", "forests", "landscape"
};

** 提示(可选):** 存储关键字 keywords.txt (每行一行)并将其加载到 List<string> 在工作时间,以避免复制。

步骤 2 - 启动 Aspose.OCR 和扫描档案

相匹配:创建 OCR 发动机,列出图像,每个文件都将进行OCR,并检查关键词的存在。

using System;
using System.Collections.Generic;
using System.IO;
using Aspose.Ocr;

namespace ImageArchiveKeywordAudit
{
    class Program
    {
        static void Main(string[] args)
        {
            // Path to the image archive directory (edit to your folder)
            string imageDirectory = @"C:\Path\To\ImageArchive";

            // Keyword list for auditing (matches the gist approach)
            List<string> keywords = new List<string>
            {
                "mountains", "beaches", "forests", "landscape"
            };

            // Initialize Aspose.OCR API (license is optional)
            // new License().SetLicense("Aspose.Total.lic");
            using (AsposeOcr api = new AsposeOcr())
            {
                // Process each JPG in the directory (same filter style as the gist)
                foreach (string imagePath in Directory.GetFiles(imageDirectory, "*.jpg"))
                {
                    // Extract text from the image
                    string extractedText = api.RecognizeImageFile(imagePath);

                    // Audit the extracted text against the keyword list
                    bool containsKeywords = AuditText(extractedText, keywords);

                    // Output the results
                    Console.WriteLine($"Image: {imagePath} - Contains Keywords: {containsKeywords}");
                }
            }
        }

        // Method to audit extracted text against a list of keywords (as in gist)
        static bool AuditText(string text, List<string> keywords)
        {
            foreach (string keyword in keywords)
            {
                if (text.Contains(keyword, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }
    }
}

步骤 3 - 延长审计(可选但推荐)

你可以提高报告和过滤,同时保持相同的OCR核心。

3.A 过滤器多图像类型

// Replace the single GetFiles with this multi-pattern approach
string[] patterns = new[] { "*.jpg", "*.jpeg", "*.png", "*.tif", "*.tiff", "*.bmp" };
var imageFiles = new List<string>();
foreach (var pattern in patterns)
    imageFiles.AddRange(Directory.GetFiles(imageDirectory, pattern, SearchOption.TopDirectoryOnly));

3.b 捕捉哪个关键字匹配 / 错过

// After OCR:
var matched = new List<string>();
var missing = new List<string>();

foreach (var k in keywords)
    (extractedText.IndexOf(k, StringComparison.OrdinalIgnoreCase) >= 0 ? matched : missing).Add(k);

Console.WriteLine($"Image: {Path.GetFileName(imagePath)} | Matched: [{string.Join(", ", matched)}] | Missing: [{string.Join(", ", missing)}]");

3.c 编写 CSV 报告

string reportPath = Path.Combine(imageDirectory, "audit-report.csv");
bool writeHeader = !File.Exists(reportPath);

using (var sw = new StreamWriter(reportPath, append: true))
{
    if (writeHeader)
        sw.WriteLine("Image,ContainsKeywords,Matched,Missing");

    sw.WriteLine($"\"{Path.GetFileName(imagePath)}\",{matched.Count > 0},\"{string.Join(";", matched)}\",\"{string.Join(";", missing)}\"");
}

步骤 4 - 从 PowerShell 或 Batch 运行

创建一个简单的PowerShell Runner run-audit.ps1:

# Adjust paths as needed
$solutionRoot = "C:\Path\To\ImageArchiveKeywordAudit"
$imageDir     = "C:\Path\To\ImageArchive"

# Build and run
dotnet build "$solutionRoot" -c Release
& "$solutionRoot\bin\Release\net8.0\ImageArchiveKeywordAudit.exe"

可选: 如果您修改程序以接受论点,则将其运行为:ImageArchiveKeywordAudit.exe "C:\Images" "C:\keywords.txt"

步骤 5 - 时间表 重复审计(Windows Task Scheduler)

使用 schtasks 每日下午2点:

schtasks /Create /TN "ImageKeywordAudit" /TR "\"C:\Path\To\ImageArchiveKeywordAudit\bin\Release\net8.0\ImageArchiveKeywordAudit.exe\"" /SC DAILY /ST 02:00

登录输出到文件,通过将命令插入一个 .cmd 此分類上一篇: stdout/stderr:ImageArchiveKeywordAudit.exe >> C:\Path\To\Logs\audit-%DATE%.log 2>&1

最佳实践

  • ** 保持一个可口可乐的关键字来源.** 将您的列表存储在 Git 或 CMDB; 每季度进行审查。
  • ** 正常化 OCR 文本.** 在匹配之前,切割白色空间、统一催眠和 Unicode 视图。
  • Tune性能. 按文件夹进行包装;仅在测量 I/O 和 CPU 后添加平行性。
  • ** 质量在,质量出。** 清洁扫描(Deskw/denoise)显著提高比赛率。
  • ** 审计范围.** 考虑每个集合的单独关键字集(例如“地图”,“产品”、“形式”)。
  • 跟踪性. 保持 CSV 报告与时间表,以便更改历史和快速分散。

Troubleshooting

  • ** 空的 OCR 输出:** 验证图像方向和对比; 尝试另一个格式(*.png, *.tif).
  • 虚假负面: 将多元/语音变量或同义词添加到您的列表中(例如“游泳”,”游行)。
  • ** 漏洞问题:** 限制竞争的运行;避免在缓慢的链接上扫描网络股份。

More in this category