Converting from cv::VideoCapture to cs::VideoSource

Right now I’m attempting to use a pi 0 for some very lightweight vision processing and I have a camera connected to it using a ribbon cable. I am able to read frames from the camera but I want to be able to use camera server so I can easily display it on the smart dashboard

#include "cameraserver/CameraServer.h"
#include "opencv2/core.hpp"
#include "opencv2/videoio.hpp"

int main() {
    cv::VideoCapture cam(0);
    cv::Mat frame;
    cam >> frame;

    cs::MjpegServer srv("serve_USB Camera 0", 1706);
    srv.SetSource(cam);
}

but SetSource() only accepts cs::VideoSource objects

I would recommend you not use OpenCV’s VideoCapture, use cs::UsbCamera (it will work for the Pi camera too) and cs::CvSink to get the frames as OpenCV Mat’s.

Regarding cs::MjpegServer, you need to create a cs::CvSource and provide the Mat to it.

This documentation will probably help (especially the example at the very end of the page).

Your code above will also just immediately exit. You’ll need to put in a loop to continuously process frames, similar to this example:

1 Like

I know I’m missing the loop, I meant it as an example. so my only option i to refactor if I want to use wpilib’s mjpeg server? or is there a way I can display my own mjpeg source on shuffleboard?

To send OpenCV images, you just need to create a CvSource to feed your cv::Mat to (in addition to the MjpegServer), e.g.

cs::CvSource cvsource("cv", VideoMode::kBGR, 320, 240, 30);
cs::MjpegServer srv("serve_cv", 1706);
srv.SetSource(cvsource);

...

cvsource.PutFrame(frame);

If you want to have it send the original camera stream directly, you will either need to pass through OpenCV as above, or use cs::UsbCamera.

Any mjpeg source can be shown on Shuffleboard using ShuffleboardContainer.addCamera().

1 Like

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