2

Currently, I am working on a streaming project. I need to grab frames from USB camera and send them over TCP.

To open USB camera video stream I'm using cv::VideoCapture. This allows me to read already decoded frames. According to this question I understood that there is no way to get encoded frame data using cv::VideoCapture and I need to encode each frame again and send it whatever I need using cv::imencode. The problem is that I can encode frames to some specific format listed here, and, in case, I use either .jpg or .png the file size still quite big and on receiving side frame rate very poor.

My question is: Is there any way to get mjpeg or h264 encoded data directly or maybe you can suggest a better way to encode frames.

OpenCV 3.4.3, camera RICOH THETA V, language C++.

My code:

void Streamer::start()
{
    cv::Mat img;
    cv::VideoCapture cap(0);
    cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('H', '2', '6', '4'));

    if (!cap.isOpened())
        throw std::invalid_argument("No device found.");

    std::vector<int> format_params;
    format_params.push_back(CV_LOAD_IMAGE_COLOR);
    format_params.push_back(CV_IMWRITE_PNG_STRATEGY);

    for (;;)
    {
        cap.read(img);
        cv::imencode(".png", img, buffer_, format_params);

        std::string strbuf(buffer_.begin(), buffer_.end());
        server_->sendString(socket, strbuf);
    }

    cap.release();
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    I think that if you are not going to do any kind of image processing and only stream a video... maybe it is better to use a library/app that does that directly... for example gstreamer or ffmpeg using something like rtsp which in the client side you can connect using videocapture of OpenCV – api55 Nov 07 '18 at 08:48
  • @api55 Thanks for your reply. Yes, in my case I am not doing any image processing. I have now tried to use ffmpeg to stream video using rtp but could not make it work. It would be great if I could use ffmpeg instead. – Yevhenii Veretennikov Nov 09 '18 at 06:41

0 Answers0