title: Python multiprocessing with openCV
body: <p>this is my first question on here and i am currently struggling with the usage of processes with python. Basically, in my main im starting two processes, one is a writing one that waits for 4 frames in order to write them together in one video continuously. The other one is the main process, which loads a YOLO Model via openCV (cv2.readNetFromDarkent(yoloConfig, yoloWeights)). So actually i am loading 4 models because i am starting 4 processes here where each process works with this model, since i cant use one model in 4 processes (i think). Each of those 4 processes gets their own video to read and detect objects in it and some other logic also happens inside with the found objects.</p>
  <p>Before i switched to multiprocessing though it worked without a problem but now that i switched to multiprocessing in order to make it faster im suddenly getting the following error when trying to run my code. I am kinda hung up on the part that says:</p>
  <pre><code>TypeError: cannot pickle 'cv2.dnn_Net' object
  </code></pre>
  <p>I havent found anything regarding this error but my guess is that processes do not share memory while threads do, that would be the reason why it worked before. But i can't wrap my head around what could be the problem now. Would i need to make global variables, which i have learned is a bad thing but i just want to make a proof of concept that i can simultanously work with multiple videos and detect objects from those. But now i am quite lost since i cant get it running this way.</p>
  <p>Does anyone know if it is possible to be running 4 videos through an object recognition model like YOLO simultanously? I am missing experience with python to find a solution myself..</p>
  <pre><code>[INFO] loading YOLO from disk...
  [INFO] YOLO loaded - took 45.8315 seconds
  Process Process-1:
  Traceback (most recent call last):
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py&quot;, line 315, in _bootstrap
      self.run()
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py&quot;, line 108, in run
      self._target(*self._args, **self._kwargs)
    File &quot;F:\My Drive\BAK\implementation\yolo-opencv-objectdetection\yolo_video.py&quot;, line 413, in main
      front.start()
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py&quot;, line 121, in start
      self._popen = self._Popen(self)
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\context.py&quot;, line 224, in _Popen
      return _default_context.get_context().Process._Popen(process_obj)
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\context.py&quot;, line 327, in _Popen
      return Popen(process_obj)
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\popen_spawn_win32.py&quot;, line 93, in __init__
      reduction.dump(process_obj, to_child)
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\reduction.py&quot;, line 60, in dump
      ForkingPickler(file, protocol).dump(obj)
  TypeError: cannot pickle 'cv2.dnn_Net' object
  Traceback (most recent call last):
    File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\spawn.py&quot;, line 107, in spawn_main
      new_handle = reduction.duplicate(pipe_handle,
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\reduction.py&quot;, line 79, in duplicate
      return _winapi.DuplicateHandle(
  PermissionError: [WinError 5] Access is denied
  Process Process-2:
  Traceback (most recent call last):
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py&quot;, line 315, in _bootstrap
      self.run()
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py&quot;, line 108, in run
      self._target(*self._args, **self._kwargs)
    File &quot;F:\My Drive\BAK\implementation\yolo-opencv-objectdetection\yolo_video.py&quot;, line 354, in writer
      while not (eventFront.is_set() and eventBack.is_set() and eventLeft.is_set() and eventRight.is_set()):
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\synchronize.py&quot;, line 329, in is_set
      if self._flag.acquire(False):
  KeyboardInterrupt
  Error in atexit._run_exitfuncs:
  Traceback (most recent call last):
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\util.py&quot;, line 357, in _exit_function
      p.join()
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py&quot;, line 149, in join
      res = self._popen.wait(timeout)
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\popen_spawn_win32.py&quot;, line 108, in wait
      res = _winapi.WaitForSingleObject(int(self._handle), msecs)
  KeyboardInterrupt
  
  Process finished with exit code 0
  </code></pre>