multiprocessing.pool.RemoteTraceback,TypeError:不正确的self类型(必须为'Feature2D'或其派生类)

时间:2018-10-31 01:31:28

标签: python opencv multiprocessing

我想使用python多重处理使用openCV轻快地计算关键点和描述符。当我运行以下代码时,发生了一些错误。但是,当我使用python线程运行代码时,就可以了。

import cv2
import os
from multiprocessing import Pool


class Test:
    def __init__(self):
        self.brisk = cv2.BRISK_create(thresh=70, octaves=4)

    def apply_brisk(self, test_img_path):
        # brisk = cv2.BRISK_create(thresh=70, octaves=4)

        frame = cv2.imread(test_img_path)
        img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        img_kp = self.brisk.detect(img_gray, None)
        img_kp, img_des = self.brisk.compute(img_gray, img_kp)
        print(img_kp)
        print(img_des)


if __name__ == '__main__':
    # test images directory
    test_images_dir = '/home/limin/pcb/images'
    test_images_name = [f for f in os.listdir(test_images_dir)]

    p = Pool(4)
    for test in test_images_name:
        # test image path
        test_image_path = test_images_dir + '/' + test

        t = Test()
        p.apply(t.apply_brisk, (test_image_path,))
        # p.apply_async(t.apply_brisk, (test_image_path,))

    p.close()
    p.join()

错误:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/home/limin/Desktop/classifier_cv_tf/test.py", line 15, in apply_brisk
    img_kp = self.brisk.detect(img_gray, None)
TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/limin/Desktop/classifier_cv_tf/test.py", line 32, in <module>
    p.apply(t.apply_brisk, (test_image_path,))
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 253, in apply
    return self.apply_async(func, args, kwds).get()
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
    raise self._value
TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)

当我更改“ BRISK”对象的位置时,将丢失错误:

import cv2
import os
from multiprocessing import Pool


class Test:
    def __init__(self):
        # self.brisk = cv2.BRISK_create(thresh=70, octaves=4)
        pass

    def apply_brisk(self, test_img_path):
        brisk = cv2.BRISK_create(thresh=70, octaves=4)

        frame = cv2.imread(test_img_path)
        img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        img_kp = brisk.detect(img_gray, None)
        img_kp, img_des = brisk.compute(img_gray, img_kp)
        print(img_kp)
        print(img_des)


if __name__ == '__main__':
    # test images directory
    test_images_dir = '/home/limin/pcb/images'
    test_images_name = [f for f in os.listdir(test_images_dir)]

    p = Pool(4)
    for test in test_images_name:
        # test image path
        test_image_path = test_images_dir + '/' + test

        t = Test()
        p.apply(t.apply_brisk, (test_image_path,))
        # p.apply_async(t.apply_brisk, (test_image_path,))

    p.close()
    p.join()

环境:

  • Ubuntu16.04
  • python3.5
  • opencv3.4.8

我对此很疑惑,如果我想在代码中使用python多重处理,谁可以帮助我。谢谢!

1 个答案:

答案 0 :(得分:0)

导入cv2 导入操作系统 从多处理导入过程 类测试:     def __init __():         self.brisk = cv2.BRISK_create(阈值= 70,八度= 4)     def apply_brisk(self,test_img_path):         框架= cv2.imread(test_img_path)         img_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)         img_kp = self.brisk.detect(img_gray,无)         img_kp,img_des = self.brisk.compute(img_gray,img_kp)         打印(img_kp)         打印(img_des) 如果__name__ =='__main__':     #测试图像目录     test_images_dir ='/ home / limin / pcb / images'     test_images_name = [f用于os.listdir(test_images_dir)中的f]     #子流程     ps = []     t = Test()     在test_images_name中进行测试:         #测试图像路径         test_image_path = test_images_dir +'/'+测试         p =处理(target = t.apply_brisk,args = {test_image_path,))         ps.append(p)         p.start()     对于ps中的p:         p.join()