Creating an MjpegServer cscore python

I’m trying to stream compressed images to the driver station from a raspberry pi.

After reading through this, https://robotpy.readthedocs.io/projects/cscore/en/stable/objects.html#videosink

In terms of compression, the only way I can see to do it is to create a MjpegServer and use the setCompression method.

Currently, I am using ChickenVision which returns the cameras and cameraservers in an array.

    cameras = []
    streams = []
    for cameraConfig in cameraConfigs:
        cs, cameraCapture = startCamera(cameraConfig)
        streams.append(cs)
        cameras.append(cameraCapture)

How would I go about creating an MjpegServer based on the created CameraServer?

Right now I am creating a CvSink and CvSource to output the images.

    liftCamera = cameras[2]
    liftCameraServer = streams[2]
    liftCameraServer.enableLogging()
    liftSink = liftCameraServer.getVideo()
    liftStream = liftCameraServer.putVideo("Pylon", 640,480)

It should be noted that this only used for clients that do not explicitly ask for a specific compression themselves (as part of the URL). Shuffleboard and the default LabVIEW dashboard allow for setting the compression.

The CameraServer class has a getServer method which will return the corresponding MjpegServer for a given source name: https://robotpy.readthedocs.io/projects/cscore/en/stable/cameraserver.html#cscore.CameraServer.getServer

So if I were to keep the same code and set the compression on the dashboard, it would do the same thing?

Yes, setCompression() on the MjpegServer object and setting the compression on the dashboard will have the same result (the software will decompress and recompress the image with the requested quality).

Note if you’re going to be compressing with software, you should see if your camera supports a YUYV video mode for the same resolution (instead of MJPEG). This avoids the need for software decompression and will reduce CPU / improve frame rate.

How does using YUYV avoid the need for software decompression?

YUYV is a uncompressed data stream from the camera, so it only needs to perform a jpeg compression to stream the image. If receiving an mjpeg stream from the camera, the image must be decompressed, then recompressed with a different compression level. So YUYV saves the decompression by already having an uncompressed image. There is no way to change the compression level on a jpeg image without decompressing it.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.