Weizen
Weizen

Reputation: 263

OpenCV works with threads but not with processes

Hi everyone and thanks for the help. I've got this function to save a video from frames taken by my webcam.

import cv2
import multiprocessing
import threading
def rec():
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret==True:
            out.write(frame)
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()

If i use it with threads, and so with this code, it works well:

s1 = threading.Thread(target=rec)
s1.start()

But if i want to start another process, using the following code, when i open the video it contains only black frames with some noise.

s1 = multiprocessing.Process(target=rec)
s1.start()

I searched all around but couldn't find any solution.

Also, i'm using Python 3.6

Upvotes: 1

Views: 1266

Answers (2)

Weizen
Weizen

Reputation: 263

I solved the problem. I was calling cap = cv2.VideoCapture(0) in my main and also in one of my imported modules, and that conflicted. I solved by calling it once.

Upvotes: 2

Ram
Ram

Reputation: 51

where is cap defined ? Try defining that in the function that you give to multiprocessing. If it is defined in the parent and is passed from the parent to the child, it is being pickled and that probably makes it unusable.

Upvotes: 2

Related Questions