我想对多行代码进行多线程处理,但代码无法在def

时间:2019-04-19 19:41:12

标签: python multithreading video

所以我的文件夹中有大约500张图像,我想让它慢慢过。我知道大多数编辑软件已经可以执行dis了,但是我想要编程方面的挑战。

我发现了如何做的How to make a movie out of images in python。但是散文的速度变慢了。所以现在我正在尝试对其进行多线程处理。问题在于执行渲染部分(渲染部分很慢)的代码不能在def中运行。我还将本教程用于多线程https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba

代码:

import cv2
import os
import multiprocessing



def render(video, image):
    print("renderd: " + image)
    video.write(cv2.imread(os.path.join(image_folder, image)))




if __name__ == "__main__":

    image_folder = 'E:/time laps/'
    video_name = 'video.avi'

    images = [img for img in os.listdir(image_folder) if img.endswith(".JPG")]
    frame = cv2.imread(os.path.join(image_folder, images[0]))
    height, width, layers = frame.shape



    video = cv2.VideoWriter(video_name, 0, 1, (width,height))


    print("rendering")

    processes = []
    for image in images:
        p = multiprocessing.Process(target=render, args=(video, image))
        processes.append(p)
        p.start()

    for process in processes:
        process.join()



        print(image)

    cv2.destroyAllWindows()
    video.release()

当我运行它时,我得到此错误消息:

Traceback (most recent call last):
  File "E:/timelaps.py", line 34, in <module>
    p.start()
  File "C:\Users\TimKo\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\TimKo\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\TimKo\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\TimKo\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\TimKo\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle cv2.VideoWriter objects

0 个答案:

没有答案