1

I want to run some mp4 videos inside a process but only camera feed works. The software gets stuck with no error message. I already tried both and I found this doesn't run. The print is where the code gets stuck.

import cv2
import multiprocessing

dispW=640
dispH=480

# Camera inputs 

cap=cv2.VideoCapture('/home/kc/Downloads/darknet/1.mp4')
cap.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)

#cv2.namedWindow("Window")

def c1():
    global cap
    while True:
        print('here')
        success, img = cap.read()
        #ret, frame1 = cap1.read()
        #frame2 = numpy.hstack((frame,frame1))
        print('here')
        cv2.imshow("Window2", img)

        #This breaks on 'q' key
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

tCap1 = multiprocessing.Process(target=c1)
tCap1.start()

this runs.

import cv2
import multiprocessing

dispW=640
dispH=480

# Camera inputs 

cap=cv2.VideoCapture('/dev/video0')
cap.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)


#cv2.namedWindow("Window")
def c1():
    global cap
    while True:
        success, img = cap.read()
        #ret, frame1 = cap1.read()
        #frame2 = numpy.hstack((frame,frame1))
        print('here')
        cv2.imshow("Window2", img)

        #This breaks on 'q' key
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

tCap1 = multiprocessing.Process(target=c1)
tCap1.start()

I need an example of mp4 file run in a multiprocessing way.

nathancy
  • 42,661
  • 14
  • 115
  • 137
KCC
  • 21
  • 1
  • 4
  • 1
    you **can't set** the width/height of a video that you **read** -- always check: `assert cap.isOpened()` once after creating the VideoCapture object – Christoph Rackwitz May 05 '22 at 00:40
  • 1
    code works for me (on Linux Mint) if I create `cap` inside function. Multiprocesses don't share memory so creating `cap` in main process and later use `global` is not good idea. Other problem is that when it creates new process then main thread saves all in file using pickle and new thread has to read it - and for file it may not work as expected. – furas May 05 '22 at 00:48
  • Code works for me using raspberry pi 4 (linux) – toyota Supra May 11 '22 at 02:28

1 Answers1

1

Here's a minimal working example with optional FPS control. If you need to extract frames from your process back to the main program, you can use multiprocessing.Queue() to transfer frames since multiprocesses have an independent memory stack.

import multiprocessing as mp
import cv2, time

def capture_frames():
    src = 'test.mp4'
    capture = cv2.VideoCapture(src)
    capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)

    # FPS = 1/X, X = desired FPS
    FPS = 1/120
    FPS_MS = int(FPS * 1000)

    while True:
        # Ensure camera is connected
        if capture.isOpened():
            (status, frame) = capture.read()
            
            # Ensure valid frame
            if status:
                cv2.imshow('frame', frame)
            else:
                break
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            time.sleep(FPS)

    capture.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    print('Starting video stream')
    capture_process = mp.Process(target=capture_frames, args=())
    capture_process.start()

Related camera/IP/RTSP/streaming, FPS, video, threading, and multiprocessing posts

  1. Python OpenCV streaming from camera - multithreading, timestamps

  2. Video Streaming from IP Camera in Python Using OpenCV cv2.VideoCapture

  3. How to capture multiple camera streams with OpenCV?

  4. OpenCV real time streaming video capture is slow. How to drop frames or get synced with real time?

  5. Storing RTSP stream as video file with OpenCV VideoWriter

  6. OpenCV video saving

  7. Python OpenCV multiprocessing cv2.VideoCapture mp4

nathancy
  • 42,661
  • 14
  • 115
  • 137