-
Notifications
You must be signed in to change notification settings - Fork 940
Closed
Description
findContours
can only be run in worker processes until the main process runs findContours
, after which point if any worker process tries to call the function again, it hangs indefinitely. This only applies to processes which are forked from the main process.
For example the following code snippet:
import multiprocessing
import cv2 as cv
import numpy as np
def get_contours(image: np.ndarray, name: str):
print(f'before {name}')
print(cv.findContours(image.copy(), mode=cv.RETR_CCOMP, method=cv.CHAIN_APPROX_SIMPLE))
print(f'after {name}')
if __name__ == '__main__':
multiprocessing.set_start_method('fork')
image = np.zeros((1000, 1000), dtype=np.uint8)
image[1, 1] = 1
p = multiprocessing.Process(target=get_contours, args=(image, 'parallel 1'))
p.start()
p.join()
p = multiprocessing.Process(target=get_contours, args=(image, 'parallel 2'))
p.start()
p.join()
get_contours(image, 'serial 1')
get_contours(image, 'serial 2')
p = multiprocessing.Process(target=get_contours, args=(image, 'parallel 3'))
p.start()
p.join()
print('finished')
The output is
before parallel 1
([array([[[1, 1]]], dtype=int32)], array([[[-1, -1, -1, -1]]], dtype=int32))
after parallel 1
before parallel 2
([array([[[1, 1]]], dtype=int32)], array([[[-1, -1, -1, -1]]], dtype=int32))
after parallel 2
before serial 1
([array([[[1, 1]]], dtype=int32)], array([[[-1, -1, -1, -1]]], dtype=int32))
after serial 1
before serial 2
([array([[[1, 1]]], dtype=int32)], array([[[-1, -1, -1, -1]]], dtype=int32))
after serial 2
before parallel 3
then it hangs
Notes
- if
multiprocessing.set_start_method('spawn')
is used, or threads are used instead then the program does not hang. - if the image is sufficiently small (e.g.
(10, 10)
) then even if fork is used, the program does not hang (on my machine at least)
Expected behaviour
findContours
is called in the main process and each worker process then exits.
Actual behaviour
After findContours
has been run from the main process, at the next call to findContours
in a worker process, the program hangs
Steps to reproduce
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic
$ python
Python 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2; cv2.__version__
'4.1.1'
>>> import numpy as np; np.__version__
'1.16.4'
$ conda list|grep opencv
opencv 4.1.1 py37ha799480_1
Metadata
Metadata
Assignees
Labels
No labels