USB camera encoded data stream

Viewed 260

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();
}
0 Answers
Related