Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9517

C#/WinRT - OCR

$
0
0

例によってcsc.exeでコンパイル
※WinRTはデフォルトだと入ってない。

試してみたが、
はっきり言って精度は期待できない。

参考にしたサイトはあとで追記します

コンパイルバッチ

ダブルクオートつかった行は 「^」 で改行つなげないようなので1行にまとめた。

compile.bat
csc/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime.WindowsRuntime\v4.0_4.0.0.0__b77a5c561934e089\system.runtime.windowsruntime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime.InteropServices.WindowsRuntime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.InteropServices.WindowsRuntime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll ^
"/r:C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd""/r:C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd"%*

ソースコード

usingSystem;usingSystem.IO;usingSystem.Runtime.CompilerServices;usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingWindows.Graphics.Imaging;usingWindows.Media.Ocr;usingWindows.Storage;usingWindows.Storage.Streams;usingWindows.Foundation;classOcrTest{[System.STAThread]staticvoidMain(string[]args){if(args.Length==0){return;}Task<string>ret=LoadImageAndOcr(args[0]);Console.WriteLine(ret.Result);}staticasyncTask<OcrResult>detect(SoftwareBitmapbitmap){varocrEngine=OcrEngine.TryCreateFromUserProfileLanguages();varocrResult=awaitMyWaitUtil<OcrResult>.GetResultWithWaiting(ocrEngine.RecognizeAsync(bitmap));// https://docs.microsoft.com/ja-jp/uwp/api/windows.media.ocr.ocrresult.lines?view=winrt-19041returnocrResult;}staticasyncTask<string>LoadImageAndOcr(stringpath){varinputFile=awaitMyWaitUtil<StorageFile>.GetResultWithWaiting(StorageFile.GetFileFromPathAsync(Path.GetFullPath(path)));SoftwareBitmapsoftwareBitmap;// https://docs.microsoft.com/ja-jp/windows/uwp/audio-video-camera/imagingusing(varstream=awaitMyWaitUtil<IRandomAccessStream>.GetResultWithWaiting(inputFile.OpenAsync(Windows.Storage.FileAccessMode.Read))){// Create the decoder from the streamvardecoder=awaitMyWaitUtil<BitmapDecoder>.GetResultWithWaiting(BitmapDecoder.CreateAsync(stream));// Get the SoftwareBitmap representation of the filesoftwareBitmap=awaitMyWaitUtil<SoftwareBitmap>.GetResultWithWaiting((decoder.GetSoftwareBitmapAsync()));}OcrResultt=awaitdetect(softwareBitmap);returnt.Text;}}// IAsyncOperationのGetAwaiterがないとか謎のコンパイルエラーがでたので、その対策として実装したpublicstaticclassMyWaitUtil<T>{publicstaticasyncTask<T>GetResultWithWaiting(IAsyncOperation<T>task){while(task.Status!=AsyncStatus.Completed){if(task.Status==AsyncStatus.Error||task.Status==AsyncStatus.Canceled){Console.WriteLine("Error or Canceled");returndefault(T);}awaitTask.Delay(1);};returntask.GetResults();}}// https://www.moonmile.net/blog/archives/8584

Viewing all articles
Browse latest Browse all 9517

Trending Articles