Skip to content
Advertisement

How can I record a video with MJPG instead of YUYV on a raspberry PI 4b with opencv-python code

Good day to you dear developers from stackoverflow. I am currently recording live-video with my usb webcam on my raspberry Pi with the following code:

import cv2 as cv
import time

class CameraInst():
    # Constructor...
    def __init__(self):
                                   
        cap = cv.VideoCapture(0)  # Capture Video...
          
        cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc('M','J','P','G'))
        
        cap.set(cv.CAP_PROP_FPS, 15)

        cap.set(cv.CAP_PROP_FRAME_WIDTH, 1920)
        
        cap.set(cv.CAP_PROP_FRAME_HEIGHT, 1080)
       
       
        self.cap=cap
                
        print("Aufnahme wird vorbereitet")
        time.sleep(1)

    def captureVideo(self):
        # Capture
        ret, self.frame = self.cap.read()
        cv.moveWindow('frame',0,0)
        cv.imshow('frame',self.frame)
       
        
       
def main():
    cam1 = CameraInst()

    while(True):
        # Display the resulting frames...
        cam1.captureVideo()    # Live stream of video on screen...
        if cv.waitKey(1) & 0xFF == ord('q'):
            break

if __name__=='__main__':
    main()


The code doesn’t work anymore, when I use the cap.set(cv.CAP_PROP_FPS, 15) function and I don’t know what I’ve done wrong.

I get the following errors:

[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (961) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1214) setProperty OpenCV | GStreamer warning: GStreamer: unhandled property
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1824) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (536) startPipeline OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1085) setProperty OpenCV | GStreamer warning: no pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1085) setProperty OpenCV | GStreamer warning: no pipeline
Aufnahme wird vorbereitet
Traceback (most recent call last):
  File "/home/pi/Documents/RPV_Video_V8.py", line 45, in <module>
    main()
  File "/home/pi/Documents/RPV_Video_V8.py", line 40, in main
    cam1.captureVideo()    # Live stream of video on screen...
  File "/home/pi/Documents/RPV_Video_V8.py", line 31, in captureVideo
    cv.imshow('frame',self.frame)
cv2.error: OpenCV(4.5.1) ../modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

Not the thing is, if I remove the cap.set(cv.CAP_PROP_FPS, 15) then the code works fine, but then the camera record with YUYV and not MJPG.

Advertisement

Answer

Set the CAP_PROP_FOURCC property to MJPG.

Look at VideoWriter (yes) documentation for details on handling fourcc values.

cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*"MJPG"))

Order of .set() method calls matters. Try to put this first. And perhaps also set CAP_PROP_FPS to be sure.

Also make sure to do proper error handling.

cap = ...
assert cap.isOpened()
ret, self.frame = self.cap.read()
if not ret:
    ... # handle this

And if the backend is gstreamer, perhaps change that.

# Windows: CAP_DSHOW or CAP_MSMF
VideoCapture(..., apiPreference=cv.CAP_DSHOW)
Advertisement