我想使用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()
环境:
我对此很疑惑,如果我想在代码中使用python多重处理,谁可以帮助我。谢谢!
答案 0 :(得分:0)