使用8个CPU进行多处理以处理图像但没有性能增益

2024-10-01 02:27:44 发布

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

我得到了一个名为cartoonize(image_path)的函数,它以'path/to/image'作为参数。这个脚本有点大,需要几分钟来处理一个1920x1080大小的图像。我试着用8 coresmultiprocessing module来使用我所有的8 cores,但是在下面的代码中没有看到性能提升。另一个问题是如何保存图像。它返回一个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)) 

Tags: thetopath图像imagetimeout性能
1条回答
网友
1楼 · 发布于 2024-10-01 02:27:44

Question: multiprocessing it is giving error "img is not a numpy array, neither a scalar."

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

您的代码返回AsyncResult

Python » 3.6.1 Documentation » multiprocessing.pool.AsyncResult
class multiprocessing.pool.AsyncResult The class of the result returned by Pool.apply_async() and Pool.map_async().

相关问题 更多 >