2017-08-14 5 views
1

私はTessnetライブラリを使ってリアルタイムOCRアプリケーションを作ろうとしています。この考え方は、ビットマップ配列に5つのイメージを格納し、それらをOCR(2秒ごとに実行)することです。コードは正常に動作しますが、多くのメモリを消費するようです。アプリケーションは約30分(約2GBのメモリを消費する)の間実行され、メモリ不足例外がスローされます。これを修正するためのアイデアですか?おかげTessnetが全メモリを使い切っています

は、これは私のOCR機能である:

private void makeOCR() 
{ 
    //Perform OCR in the image array 

    x1 = string.Empty; 

    //initialize OCR 
    Tesseract ocr = new tessnet2.Tesseract(); 
    ocr.Init(null, "eng", true); 

    //loop through 5 bitmaps 
    for (int i = 0; i < 5; i++) 
    { 
    var result = ocr.DoOCR(imageArray[i], Rectangle.Empty); 
    foreach (tessnet2.Word word in result) 
    { 
     x1 = x1 + word.Text; 
    } 
    x1 = x1 + Environment.NewLine; 
    } 

    ocr.Dispose(); 
    GC.Collect(); 

} 

そして、私は新しいスレッドでタイマーチックイベント(2秒ごと)に、この関数を呼び出すしようとしています。

private void timer1_Tick(object sender, EventArgs e) 
{ 
    System.Threading.Thread t1 = new System.Threading.Thread(makeOCR); 
    t1.Start(); 
    textBox1.Text = x1;    
} 

答えて

1

私は、TessaractのコードのためにTessnetを使用している間、メモリリークの多くの報告があったことがわかりました。人々が別々の実行可能ファイルにTessnetコードをロードした例がありました。私は同じことをするために私のコードを共有しています。次のコードで別のコンソールアプリケーションを作成する(今のメモリ使用量は本体のみのアプリのために45メガバイトで一定になっている)

まず:

static void Main(string[] args) 
{ 
    // Runtime arguments: 
    // [0] Folder Path 
    // [1] N for numeral, A for all 
    // [2] separator string 
    // [3] echo (N for app closes silently, Y app waits for user input to close 

    //Initialize 
    string path = args[0]; 
    string num = args[1]; 
    string sep = args[2]; 
    string ech = args[3]; 
    string ocrval = String.Empty; 
    bool numeral = false; 

    if (num == "N") 
     numeral = true; 

    //Start TESSNET initialization 
    Tesseract ocr = new tessnet2.Tesseract(); 
    ocr.Init(null, "eng", numeral); 

    //Generate string array to read filenames in the path directory 
    string[] allFiles = Directory.GetFiles(path,"*",SearchOption.TopDirectoryOnly); 

    //Run OCR code 
    foreach (string fn in allFiles) 
    { 
     Bitmap bm = new Bitmap(fn); 
     var result = ocr.DoOCR(bm, Rectangle.Empty); 
     foreach (tessnet2.Word word in result) 
     { 
      ocrval = ocrval + word.Text; 
     } 
     ocrval = ocrval + sep; 
     bm.Dispose(); 
    } 

    //Write result to textfile 
    File.WriteAllText(path+"/result/result.txt", ocrval); 

    //echo output 
    if (ech == "Y") 
    { 
     Console.WriteLine(ocrval); 
     Console.WriteLine("Process Completed. Press any key to close"); 
     Console.ReadLine(); 
    } 


} 

だから、このアプリケーションは、引数として、いくつかの基本的なパラメータを取ります。 (コードをテストするには、[プロパティ]> [デバッグ]> [コマンドライン引数]を使用します)。フォルダパスは、すべての画像が格納されているフォルダをアプリケーションに指示する最初の引数です。そして、全ての画像がTessnet OCRで処理され、その結果が今で(/結果フォルダがユーザーによって作成される)

/path/result/result.txt でテキストファイルに書かれています画像が処理される主なアプリケーションでは、次のコードが配置されます。

まず、ビットマップは Bitmap.Saveメソッドを使用して同じ作業ディレクトリ(workPath)に保存する必要があります。

第二に、コンソールアプリは次のコードでメインアプリケーションによって呼び出されます。

Process process = new Process(); 
process.StartInfo.FileName = "C:/temp/toe/Tessnet OCR Engine.exe"; 
process.StartInfo.Arguments = workPath + " N && N"; 

// workPath is the directory where all images are stored 
// N -> Numeral Only, A if all 
// && -> Separator to define each the termination of every image's text 
// N -> No Echo of results Y-> Show results on console and wait for user input. 

process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; 
process.Start(); 
process.WaitForExit(); 

string res = File.ReadAllText(workPath.Text+"/result/result.txt"); 
string[] result; 
string[] stringSeparators = new string[] { "&&" }; 
result = res.Split(stringSeparators, StringSplitOptions.None); 

最後result文字列配列はworkPathディレクトリ内のすべての画像のテキストを保持しています。コードには例外処理が必要です。 2〜3秒ごとに画像を処理するタスクは、第2のコードセットをタイマーティックイベントに置くことによって行われる。

関連する問題