私はマルチスレッドではとても新しいので、基本的にはごめんなさい。私は画像ファイルを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)
の代わりに、 stdoutに印刷して出力ファイルに印刷してみてください:) – alfasin
stdoへの印刷をお勧めしますか? utは何らかの理由で動作しませんか? – cfen
残りのコードはOCRテキストファイルを出力ファイルに出力しません。 – cfen