繁体   English   中英

使用多处理来处理图像,但使用8 cpus不能获得性能提升

[英]Using multiprocessing to process image but no performance gain with 8 cpus

我得到了一个名为cartoonize(image_path)的函数,该函数将'path / to / image'作为参数。 该脚本需要花费几分钟才能处理1920x1080尺寸的图像。 我尝试通过multiprocessing 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)) 

问题 :多重处理给出错误“ 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() 返回结果的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM