【问题标题】:multiprocessing.pool.RemoteTraceback, TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)multiprocessing.pool.RemoteTraceback,TypeError:自我类型不正确(必须是“Feature2D”或其派生词)
【发布时间】:2019-04-04 03:06:44
【问题描述】:

我想使用 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 多处理,谁能帮助我。谢谢!

【问题讨论】:

  • 你运行这个遥控器?具体如何?我想知道为什么您还没有在父级中获得TypeError: can't pickle cv2.BRISK objects,因为 BRISK 对象不可腌制。用pickle.dumps(Test())检查这个。
  • 暂时在本地机器上运行。 Test() 可以腌制垃圾场。我使用 Process 而不是 Pool,它可以工作。
  • 它适用于Process 而不是Pool,因为您的操作系统使用fork 启动新进程,因此不涉及酸洗。但是,当您使用Pool 时,酸洗总是会发生,因为参数将通过队列发送。奇怪的是,酸洗会在您的情况下默默地创建一个浅拷贝,其中不包括轻快的实例。您使用哪个 IDE 来运行它?
  • @7wdeepin 我在一个 Flask 应用程序中遇到了同样的错误 - 你找到它了吗?

标签: python opencv multiprocessing


【解决方案1】:
import cv2
import os
from multiprocessing import Process


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

    def apply_brisk(self, test_img_path):
        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)]

    # subprocess
    ps = []
    t = Test()

    for test in test_images_name:
        # test image path
        test_image_path = test_images_dir + '/' + test

        p = Process(target=t.apply_brisk, args=(test_image_path,))
        ps.append(p)
        p.start()

    for p in ps:
        p.join()

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 2019-02-16
    • 2016-05-03
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多