Ask Your Question

Revision history [back]

Silent failure when multiprocessing and OpenCL used on MacOS

I have been running a long OpenCV pipeline and, in attempt to reduce dropped frames, I decided to implement it with multiprocessing. The reason I chose multiprocessing over Python threads is because with long pipelines the Global Interpreter Lock (GIL) still tends to get in the way.

The following code was mostly stolen from the following StackOverflow thread, and it seems to work fine when OpenCL is not used. However, I have always had the statement cv2.ocl.setUseOpenCL(cv2.ocl.haveOpenCL()) at the top of my code in the blind belief that it would lead to some acceleration.

It seems that when it is used in conjunction with multiprocessing, the whole OpenCL support breaks and the pipeline is stuck at [ INFO:0] Initialize OpenCL runtime....

Is this known behaviour of OpenCV on MacOS?

I am running MacOS 10.12 with manually compiled OpenCV for Gstreamer and other support, Python 3.5.2.

import multiprocessing
import cv2
import time

print("OpenCL is available: {}".format(cv2.ocl.haveOpenCL()))
cv2.ocl.setUseOpenCL(cv2.ocl.haveOpenCL()) # Change to False to test without OpenCL
time.sleep(10)
queue_from_cam = multiprocessing.Queue(maxsize=2048)

def cam_loop(queue_from_cam):
    cap = cv2.VideoCapture(0,
                           cv2.CAP_GSTREAMER)
    while True:
        hello, img = cap.read()
        if hello:
            queue_from_cam.put(img)

cam_process = multiprocessing.Process(target=cam_loop, args=(queue_from_cam,))
cam_process.start()

while True:
    if not queue_from_cam.empty():
        from_queue = queue_from_cam.get()
        cv2.imshow('camera', from_queue)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break