使用多处理来处理图像但没有8 cpus的性能提升

时间:2017-04-25 18:22:09

标签: python numpy

我有一个名为cartoonize(image_path)的功能,它采用' path / to / image'作为一个论点。脚本需要花费几分钟来处理1920x1080大小的图像。我尝试使用8 cores使用multiprocessing module,但是我看到以下代码没有性能提升。另一个问题是如何保存图像。它返回一个CV2对象。通常,我会通过see below code将图像保存到磁盘,但是通过多处理,它会给出错误"img is not a numpy array, neither a scalar."我还希望获得性能提升,但我无法弄清楚如何有效地执行此操作。

out_final = cartoonize('path/to/image'))
cv2.imwrite('cartoon.png', out_final)



import multiprocessing

if __name__ == '__main__':
    # mark the start time
    startTime = time.time()

    print "cartoonizing please wait ..."
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    pool_outputs = pool.apply_async(cartoonize, args=(image_path,))
    pool.close()
    pool.join()
    print ('Pool:', pool_outputs)

    # mark the end time
    endTime = time.time()
    print('Took %.3f seconds' % (startTime - endTime)) 

1 个答案:

答案 0 :(得分:0)

  

问题:多处理它给出错误“img不是一个numpy数组,也不是标量。”

pool_outputs = pool.apply_async(cartoonize, args=(image_path,))

您的代码返回AsyncResult

  

Python»3.6.1文档»multiprocessing.pool.AsyncResult
  class multiprocessing.pool.AsyncResult   Pool.apply_async()和Pool.map_async()返回的结果的类。