using Microsoft.Windows.AI;

using Microsoft.Windows.AI.Imaging;

using Microsoft.Graphics.Imaging;

using Windows.Graphics.Imaging;

using Windows.Storage;

using Windows.Storage.Streams;

 

static async Task<TextRecognizer> EnsureRecognizerAsync()

{

    if (TextRecognizer.GetReadyState() == AIFeatureReadyState.NotReady)

    {

        var result = await TextRecognizer.EnsureReadyAsync();

 

        if (result.Status != AIFeatureReadyResultState.Success)

        {

            throw new Exception(result.ExtendedError().Message);

        }

    }

 

    return await TextRecognizer.CreateAsync();

}

 

static async Task<string> OcrImageAsync(string imagePath)

{

    StorageFile file = await StorageFile.GetFileFromPathAsync(imagePath);

    IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);

 

    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

    SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();

 

    ImageBuffer buffer = ImageBuffer.CreateBufferAttachedToBitmap(bitmap);

 

    using TextRecognizer recognizer = await EnsureRecognizerAsync();

    RecognizedText result = recognizer.RecognizeTextFromImage(buffer);

 

    return string.Join(

        Environment.NewLine,

        result.Lines.Select(line => line.Text)

    );

}

# 仮想環境を作成

py -m venv .venv

 

# 仮想環境を有効化

.\.venv\Scripts\activate

 

# pip更新

python -m pip install -U pip

 

# CPU版 PaddlePaddle

pip install paddlepaddle

 

# PaddleOCR、PDF操作、配列処理

pip install "paddleocr>=3.7.0" PyMuPDF numpy