如何在图像中搜索多个关键字或模式

如何在图像中搜索多个关键字或模式

在大型图像档案中搜索多个关键字或文本模式对符合性、安全性和数字发现至关重要。

现实世界问题

用于多个术语的图像的手动审查(例如名称、ID、隐私句子)是缓慢而不可靠的,尤其是数以千计的文件。

解决方案概述

自动检测,通过运行多关键词或 regex 搜索图像集。 报告或行动比赛遵守,人力资源,或数字法医使用案例。

原則

  • Visual Studio 2019 或以后
  • .NET 6.0 或更高版本(或 .Net Framework 4.6.2+)
  • Aspose.OCR 为 .NET 从 NuGet
PM> Install-Package Aspose.OCR

步骤实施

步骤 1: 安装和设置 Aspose.OCR

using Aspose.OCR;

步骤2:定义关键词或模式

List<string> keywords = new List<string> { "Confidential", "PII", "Invoice", "2025" };
List<string> regexPatterns = new List<string> { @"\d{3}-\d{2}-\d{4}", @"[A-Z]{2}[0-9]{6}" }; // SSN, Passport

步骤3:为关键词/模式进行搜索图像集成

string[] files = Directory.GetFiles("./input", "*.png");
RecognitionSettings settings = new RecognitionSettings();
settings.Language = Language.English;
AsposeOcr ocr = new AsposeOcr();
foreach (string file in files)
{
    foreach (string keyword in keywords)
    {
        bool found = ocr.ImageHasText(file, keyword, settings);
        if (found) Console.WriteLine($"Keyword '{keyword}' found in {file}");
    }
    foreach (string pattern in regexPatterns)
    {
        bool found = ocr.ImageHasText(file, pattern, settings);
        if (found) Console.WriteLine($"Pattern '{pattern}' found in {file}");
    }
}

步骤4:登录和行动比赛

  • 将结果保存到 CSV,发送警告,或在匹配中启动工作流。
// Example: Append to log file
File.AppendAllText("search_log.csv", $"{file},{keyword or pattern},found\n");

步骤5:错误处理和性能

  • 使用 try/catch 为 robust batch 工作
  • 如果需要,将大型集合平行
try
{
    // Searching logic
}
catch (Exception ex)
{
    File.AppendAllText("search_errors.log", ex.Message + Environment.NewLine);
}

步骤6:完整的例子

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

class Program
{
    static void Main(string[] args)
    {
        List<string> keywords = new List<string> { "Confidential", "PII", "Invoice", "2025" };
        List<string> regexPatterns = new List<string> { @"\d{3}-\d{2}-\d{4}", @"[A-Z]{2}[0-9]{6}" };
        try
        {
            string[] files = Directory.GetFiles("./input", "*.png");
            RecognitionSettings settings = new RecognitionSettings();
            settings.Language = Language.English;
            AsposeOcr ocr = new AsposeOcr();
            foreach (string file in files)
            {
                foreach (string keyword in keywords)
                {
                    bool found = ocr.ImageHasText(file, keyword, settings);
                    if (found)
                        File.AppendAllText("search_log.csv", $"{file},{keyword},found\n");
                }
                foreach (string pattern in regexPatterns)
                {
                    bool found = ocr.ImageHasText(file, pattern, settings);
                    if (found)
                        File.AppendAllText("search_log.csv", $"{file},{pattern},found\n");
                }
            }
        }
        catch (Exception ex)
        {
            File.AppendAllText("search_errors.log", ex.Message + Environment.NewLine);
        }
    }
}

使用案例和应用程序

符合性审计

自动检查扫描文件的黑名单词或敏感模式。

HR、法律和安全

在登机或证据文件中检测到机密表达式、员工名称或PII的存在。

趋势和频率分析

在大文件集中计算和报告关键字的频率。

共同挑战与解决方案

挑战1:错误的积极性

** 解决方案:** 重定向关键字和 regex; 手动审查边缘案例。

挑战2:大包尺寸

** 解决方案:** 使用平行处理和强大的错误处理。

挑战3:多语言

** 解决方案:** 按语言组调整识别设置和关键字列表。

绩效考虑

  • 集成工作可以长时间进行大档案 - 显示器CPU,磁盘和日志
  • 平行,如果需要高通道
  • 登录所有结果进行审查和遵守

最佳实践

  • 定期清理和更新关键字列表
  • 自动错误登录和报告
  • 对代表性档案样本进行测试
  • 安全记录和搜索结果

先进的场景

场景1:搜索和突出结果在输出PDF

以被突出关键词的图像出口(自定义后处理)。

场景2:计划定期包关键字审计

自动工作,夜间或每周进行遵守。

结论

Aspose.OCR Image Text Finder for .NET 可提供强大、自动集合关键字和模式搜索,支持图像档案中的遵守、安全和趋势分析。

See ASPOSE.OCR 为 .NET API 参考 先进的文本搜索例子。

 中文