0

So I have this folder with around 500 images in it and I'm trying to make a time lapse of it. I know most editing software can already do dis but I wanted a programming challenge.

I found this on how to do it How to make a movie out of images in python. But the proses was to slow. So now I'm trying to multithread it. The problem is that the code that does the rendering part (the rendering part is slow) cant be run in a def. I also used this tutorial for the multithreading https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba

Code:

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()

when I run it I get this error mesage:

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
  • `multiprocessing` doesn't use threads, under windows is runs a new instance of Python in a separate process. maybe you want to look at the `threading` module instead – Sam Mason Apr 19 '19 at 20:08
  • note, that whether this would make any difference relates to whether the `cv2` `VideoWriter` releases the ["GIL"](https://stackoverflow.com/q/1294382/1358308) when writing frames – Sam Mason Apr 19 '19 at 20:10

0 Answers0