使用OpenCV多线程读取图像不起作用

时间:2017-05-03 16:05:08

标签: python multithreading opencv

我有一个Python代码,可以并行读取硬盘中的图像。每个线程将读取不同的图像。我得到一个包含所有路径的向量。路径是正确的,因为我可以在未完成线程时加载图像。但是,并非所有图像都被读取,读取多线程图像的结果看起来相当稀疏。

要从线程获取结果,我会初始化一个填充了None的向量,如RGBS_of_Threads = [None]*len(rgb_imgs_path),其中使用len(rgb_imgs_path)描述了预期图像的数量。但是在等待线程加入之后我只获得了一些图像而不是全部。

import cv2
from threading import Thread

#Threading Target
def parallelRead(RGBS,path,i):
   RGBS[i] = cv2.imread(path[i])

#Some Setup where the path is from  
rgb_imgs_path = getPaths()

###Simple Program loop
RGBS_of_loop = [None]*len(rgb_imgs_path)
for i in tqdm(range(0,len(rgb_imgs_path))):
    RGBS_of_loop[i] = cv2.imread(rgb_imgs_path[i])


#Check if it worked
if None not in RGBS_of_loop and RGBS_of_loop is not None:
    print("reading from loop was successfull!")
else:
    print "amount of Nones" + str(RGBS_of_loop.count(None))
    print "total length" + str(len(RGBS_of_loop))


#Now with threading
RGBS_of_Threads = [None]*len(rgb_imgs_path)
threads = []
for i in tqdm(range(0,len(rgb_imgs_path))):
    threads.append(Thread(target=parallelRead,args=[RGBS_of_Threads,rgb_imgs_path,i]))
for i in tqdm(range(0,len(rgb_imgs_path))):
    threads[i].start()
for i in tqdm(range(0,len(rgb_imgs_path))):
    threads[i].join()

#check if it worked
if None not in RGBS_of_Threads and RGBS_of_Threads is not None:
    print("reading from thread was successfull!")
else:
    print "amount of Nones" + str(RGBS_of_Threads.count(None))
    print "total length" + str(len(RGBS_of_Threads))

0 个答案:

没有答案