Tự động kiểm toán từ khóa cho lưu trữ hình ảnh đảm bảo dữ liệu thị giác của bạn được đánh dấu liên tục và dễ dàng phát hiện. Với Aspose.OCR cho .NET, bạn có thể đọc văn bản tích hợp/có thể nhìn thấy từ ảnh và xác thực nó chống lại một danh sách từ khoá được kiểm soát – sau đó báo cáo những gì thiếu. hướng dẫn này cải thiện dòng công việc với các bước cụ thể, chạy phù hợp với gist ở cuối cùng, cộng với cải tiến tùy chọn cho lịch trình, Báo cáo và bảo trì.

Một ví dụ đầy đủ

Nguyên tắc

  • *.NET 8 (hoặc .NET 6+) * SDK được cài đặt.
  • NuGet truy cập để cài đặt Aspose.OCR.
  • Một thư mục hình ảnh để kiểm toán (ví dụ: C:\Path\To\ImageArchive).
  • (Tùy chọn) Một tập tin giấy phép Aspose nếu bạn có kế hoạch vượt quá giới hạn đánh giá.

Tạo dự án & thêm gói

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

Bước 1: Chuẩn bị danh sách từ khóa của bạn

Quyết định những từ khóa canonical mà hình ảnh của bạn nên chứa. trong gist, từ khoá được mã hóa cứng vì sự đơn giản:

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

Tip (tùy chọn): Lưu trữ từ khóa trong keywords.txt (một trong mỗi dòng) và tải chúng vào List<string> Thời gian làm việc để tránh tập hợp.

Bước 2 — Initialize Aspose.OCR và Scan the Archive

Tương thích với gạch: tạo một động cơ OCR, liệt kê hình ảnh, mỗi tệp O CR, và kiểm tra sự hiện diện từ khóa.

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;
        }
    }
}

Bước 3 – mở rộng kiểm toán (tùy chọn nhưng khuyến cáo)

Bạn có thể cải thiện báo cáo và lọc trong khi giữ cùng một cốt lõi OCR.

3.a Bộ lọc nhiều loại hình ảnh

// 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 Chụp những từ khóa nào phù hợp / bị bỏ lỡ

// 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 Viết báo cáo 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)}\"");
}

Bước 4 – chạy từ PowerShell hoặc Batch

Tạo một PowerShell Runner đơn giản 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"

Tùy chọn: Nếu bạn thay đổi chương trình để chấp nhận các biện pháp, chạy nó như sau:ImageArchiveKeywordAudit.exe "C:\Images" "C:\keywords.txt"

Bước 5 – Kế hoạch kiểm toán lặp lại (Windows Task Scheduler)

Sử dụng schtasks Mỗi ngày vào lúc 2h:

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

Log output to file by wrapping the command in a .cmd chuyển hướng stdout/stderr:ImageArchiveKeywordAudit.exe >> C:\Path\To\Logs\audit-%DATE%.log 2>&1

Thực hành tốt nhất

  • Giữ một nguồn từ khóa canonical. Giữ danh sách của bạn trong Git hoặc một CMDB; xem xét hàng tháng.
  • Normalize OCR text. Trim white space, unify hyphens và Unicode look-alikes trước khi phù hợp.
  • Tun hiệu suất. Phân phối theo thư mục; thêm sự đồng bộ chỉ sau khi đo I/O và CPU.
  • ** Chất lượng trong, chất lượng ra.** Các quét tinh khiết (deskw/denoise) cải thiện đáng kể tỷ lệ trận đấu.
  • Bộ kiểm toán. Hãy xem xét các tập tin từ khóa riêng biệt cho mỗi bộ sưu tập (ví dụ, “thiên bản”, “một sản phẩm” và “các hình thức”).
  • Traceability. Giữ các báo cáo CSV với dấu giờ để thay đổi lịch sử và phân biệt nhanh chóng.

Troubleshooting

  • Không xuất OCR: Kiểm tra định hướng hình ảnh và tương phản; thử định dạng khác (*.png, *.tif).
  • ** Phản ứng giả:** Thêm nhiều/những biến thể hoặc đồng nghĩa vào danh sách của bạn (ví dụ: “bãi biển”, “thùng rác”).
  • Thách thức thông qua: Giới hạn các hoạt động cạnh tranh; tránh quét các cổ phần mạng trên các liên kết chậm.

More in this category