传递函数和参数到线程

2024-07-05 08:36:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在t3线程中调用函数ReadImages()。这是我的密码。你知道吗

#ReadImages
def ReadImages(path,queue1,queue2,queue3):
    print('Training in thread...')

    # Create a list of images and a list of corresponding names
    (images, lables, names, id) = ([], [], {}, 0)
    namess=[]

    for (subdirs, dirs, files) in os.walk(fn_dir):
        for subdir in dirs:
            names[id] = subdir 
        namess.append(names[id])
        print "Names[id]", names[id]
            subjectpath = os.path.join(fn_dir, subdir)
            for filename in os.listdir(subjectpath):
                path = subjectpath + '/' + filename
                lable = id
                images.append(cv2.imread(path, 0))
                lables.append(int(lable))
            id += 1

    (im_width, im_height) = (112, 92)

    # Create a Numpy array from the two lists above
    (images, lables) = [numpy.array(lis) for lis in [images, lables]]
    print namess
    for i in range(len(images)):
        queue1.put(images[i])
    for l in range(len(lables)):
        queue2.put(lables[l])
    for n in range(len(names)):
        queue3.put(names[n])

    return (images,lables)

以下是我的线程实现:

if __name__ == '__main__':


    images=[] 
    lables=[]
    names=[]
    queue1 = Queue.Queue()
    queue2 = Queue.Queue()
    queue3 = Queue.Queue()

    t3 = threading.Thread(target = ReadImages(path,queue1,queue2,queue3))
    print"train", t3
    t3.start()

    while not queue1.empty():
        images.append(queue1.get())
    while not queue2.empty():
        lables.append(queue2.get())
    while not queue3.empty():
        names.append(queue3.get())

这段代码运行良好,我得到的数据从队列需要,但它导致了一个异常,我不知道是什么原因。我想删除这个例外。我该怎么做?这是个例外 线程训练。。。 列车

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: 'tuple' object is not callable

Tags: pathinidfornamesqueueimagest3