2016-12-15 7 views
1

私はマルチスレッドではとても新しいので、基本的にはごめんなさい。私は画像ファイルをOCRするいくつかの機能を持っており、タスクをマルチスレッド化したい。関数は何も返しませんが、OCRデータセットのテキストのみを保存します。コードは以下の通りである:Pythonマルチプロセッシング:Pool.map()は全く関数を呼び出さないようです

start_time = time.time() 
path = 'C:\\Users\\RNCZF01\\Documents\\Cameron-Fen\\Economics-Projects\\Patent-project\\similarity\\Patents\\OCR-test' 
listfiles = os.listdir(path) 

filterfiles = [p for p in listfiles if p[-4:] == '.tif'] 

pool = Pool(processes=2) 

result = pool.map(OCRimage,filterfiles) 

pool.close() 
pool.join() 

print("--- %s seconds ---" % (time.time() - start_time)) 

私はそれがpool.map()で立ち往生のようにそれはそう、コードを実行すると。私はそれを30分間実行しましたが、これは試行のプロセスよりも長く、単一の出力では生成されませんでした。関数OCRimageをテストしたところ、関数に1回入力したようには見えませんでした(print(1)をOCRimageコードの最初の行として使用)。私は誰かが私を助けることができるかどうか疑問に思っています。おかげで、

キャメロン

EDIT(追加OCRimage機能):

def OCRimage(f): 
    #This runs the magick bash script which splits a multi-image tif into multiple single image tiffs 
    process = subprocess.Popen(["magick", path + "\\" + f, path + "\\temp\\%d.tif"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
    print(process.communicate()[0]) 

    #finds the number of pages for each tiff file (this might not be necassary but the all files in directory python command could access files randomly) 
    max1 = -1 
    for filename in os.listdir(path+'\\temp'):  
     if (max1 < int(filename[0:-4])): 
      max1 = int(filename[0:-4]) 
    max1 = max1 + 1 

    text = "" 
    for each in range(0,max1): 
     im = Image.open(path + "\\temp\\"+ str(each) + ".tif") 
     text = text + pytesseract.image_to_string(im) 
    with open(path + "\\result\\OCR-"+f[0:-4]+".txt", 'w') as file: 
     file.write(text)  

    for f in os.listdir(path+'\\temp'): 
     os.remove(path + '\\temp\\' + f) 

EDIT2:ここでは、すべての輸入

import time 
import subprocess 
import os 
import pytesseract 
from PIL import Image 

from multiprocessing import Pool 
import multiprocessing 
countcpus = multiprocessing.cpu_count() 

がある

OCRimage機能は、次のようになりますEDIT3:

OCRimage(f)を単独で実行すると正常に動作します。

path = 'C:\\Users\\RNCZF01\\Documents\\Cameron-Fen\\Economics-Projects\\Patent-project\\similarity\\Patents\\OCR-test' 
for p in os.listdir(path): 
    OCRimage(p) 
+0

の代わりに、 stdoutに印刷して出力ファイルに印刷してみてください:) – alfasin

+0

stdoへの印刷をお勧めしますか? utは何らかの理由で動作しませんか? – cfen

+0

残りのコードはOCRテキストファイルを出力ファイルに出力しません。 – cfen

答えて

0

これは(本当の問題については、以下ののWindowsの項を参照)問題は、あなたのOCRimage関数内でなければならないことを示しているようだというMinimal, Complete, and Verifiable Exampleです:

代わりに、私はちょうどこれを使用するマルチスレッドコードの
from multiprocessing import Pool 

def OCRimage(file_name): 
    print "file_name = %s" % file_name 

filterfiles = ["image%03d.tif" % n for n in range(5)] 

pool = Pool(processes=2) 
result = pool.map(OCRimage, filterfiles) 

pool.close() 
pool.join() 

出力

file_name = image000.tif 
file_name = image001.tif 
file_name = image002.tif 
file_name = image003.tif 
file_name = image004.tif 
私はrecomme

ND OCRimageの先頭にこれらの変更:

def OCRimage(file_name): 
    print "file_name = %s" % file_name 
    src = os.path.join([path, file_name]) 
    dst = os.path.join([path, 'temp', '%d.tif']) 
    command_list = ['magick', src, dst] 
    # This runs the magick bash script which splits a multi-image tif into 
    # multiple single image tiffs 
    process = subprocess.Popen(command_list, 
           shell=True, 
           stdout=subprocess.PIPE, 
           stderr=subprocess.PIPE) 
    output, errors = process.communicate() 
    if process.returncode != 0: 
     print "Image processing failed for %s: %s" % (file_name, errors) 
     return 
    # The rest of your code goes here 

サブプロセスからの戻りコードがゼロであることを確認することが重要です。ゼロでない場合は、実際にerrors文字列を見たいと思っています。私はWindows上でmcveを走った

のWindows

が、私はこの例外だ:私はこれにmcveを変更した場合

RuntimeError: 
      Attempt to start a new process before the current process 
      has finished its bootstrapping phase. 

      This probably means that you are on Windows and you have 
      forgotten to use the proper idiom in the main module: 

       if __name__ == '__main__': 
        freeze_support() 
        ... 

      The "freeze_support()" line can be omitted if the program 
      is not going to be frozen to produce a Windows executable. 
Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "C:\Python27\lib\multiprocessing\forking.py", line 380, in main 

を、それが働いた:

from multiprocessing import Pool 

def OCRimage(file_name): 
    print "file_name = %s" % file_name 

def main(): 
    filterfiles = ["image%03d.tif" % n for n in range(5)] 
    pool = Pool(processes=2) 
    result = pool.map(OCRimage, filterfiles) 
    pool.close() 
    pool.join() 

if __name__ == '__main__': 
    main() 
+0

問題は、私がマルチスレッドではないとき、OCRimageはうまく動作します – cfen

+0

少なくとも私の問題は、 'result = pool.map(OCRimage、filterfiles)'は動作しないということです。私が 'OCRimage(f):return f ** 2'を作ったとしても。私はPythonを使用する2.7 – cfen

+0

あなたは私の答えの先頭に[mcve]を実行しましたか?期待される出力が得られますか? –

関連する問題