0

I have five RTSP streams to read, and the image size is 2160x3840. In order to improve the speed, I use multithreading to improve the speed of image reading, but I find that the speed is the same as that of serial. Here is my test code. Thank you for your reply! This question has bothered me for a few days. Thank you again for your answer!

#if 1
#include <iostream>
#include <thread>
#include <vector>
#include <opencv2/opencv.hpp>
#include <time.h>
#include <unistd.h>

using namespace std;
using namespace cv;

#define LOGLN(msg) std::cout << msg << std::endl

//void detect(Mat img, String strCamera);
void stream(String strCamera, int i);


int main()
{
    thread cam1(stream, "../video/11.mp4", 1);
    thread cam2(stream, "../video/21.mp4", 2);
    thread cam3(stream, "../video/31.mp4", 3);
    thread cam4(stream, "../video/41.mp4", 4);
    thread cam5(stream, "../video/51.mp4", 5);
//  thread cam5(stream, "rtsp://ip:host/video5", 5);

    LOGLN("1111111111");
    cam1.join();
    cam2.join();
    cam3.join();
    cam4.join();
    cam5.join();
    LOGLN("22222222");
    return 0;
}

void stream(String strCamera, int i) {
    VideoCapture cap(strCamera);
    if (cap.isOpened()) {
        while (true) {
            clock_t t0 = clock();
            Mat frame;
            cap >> frame;
            LOGLN("decode video "<< i << " cpu : " << (clock()-t0) /1e6 << " sec");
        }
    }
//    while(1)
//    {
//    int b=0;
//    for (int i=0; i< 5000; i++)
//    {
//    b += i;
//    }
//    LOGLN("decode video "<< i << " sec");
//    sleep(2);
//    }
}
#endif


#if 0
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
//#include <opencv2/core/core.hpp>
#include <thread>
#include <time.h>
#define LOGLN(msg) std::cout << msg << std::endl

using namespace std;
using namespace cv;


int main()
{
    VideoCapture cap1;
    VideoCapture cap2;
    VideoCapture cap3;
    VideoCapture cap4;
    VideoCapture cap5;


    //cap1.open(0);
    cap1.open("../video/11.mp4");
    cap2.open("../video/21.mp4");
    cap3.open("../video/31.mp4");
    cap4.open("../video/41.mp4");
    cap5.open("../video/51.mp4");


    /*
    cap1.set(CAP_PROP_FRAME_WIDTH, 640);
    cap1.set(CAP_PROP_FRAME_HEIGHT, 480);
    cap2.set(CAP_PROP_FRAME_WIDTH, 640);
    cap2.set(CAP_PROP_FRAME_HEIGHT, 480);
    cap3.set(CAP_PROP_FRAME_WIDTH, 640);
    cap3.set(CAP_PROP_FRAME_HEIGHT, 480);
    cap4.set(CAP_PROP_FRAME_WIDTH, 640);
    cap4.set(CAP_PROP_FRAME_HEIGHT, 480);
    */


    if (!cap1.isOpened() || !cap2.isOpened()) { //|| !cap3.isOpened() || !cap4.isOpened()) {
        cout << "sorry try again" << endl;
        return -1;
    }

    Mat frame1, frame2, frame3, frame4, frame5;// , frame3, frame4;

    while (true)
    {
        clock_t t0 = clock();
        bool s1 = cap1.read(frame1);
        bool s2 = cap2.read(frame2);
        bool s3 = cap3.read(frame3);
        bool s4 = cap4.read(frame4);
        bool s5 = cap5.read(frame5);
        LOGLN("decode video "<< 1 << " cpu : " << (clock()-t0) /1e6 << " sec");


//      if (!s1 || !s2) {
//          cout << "sorry I can't read" << endl;
//          break;
//      }
    }
    return 0;
}
#endif
  • There may be a bottleneck writing the data to the disk drive. It will be limited by how much it can parallelize writes - having to skip the r/w head(s) between different files. – Galik Jan 29 '21 at 04:38
  • Thank you for your reply. Could you tell me more about it. – Shili Chen Jan 29 '21 at 06:23
  • It is just that your parallel running threads may have to queue up in line to write their data to the disk drive which can only write to one file at a time. Your parallelization is lost because the disk can only write in a serial manner.. – Galik Jan 29 '21 at 08:04
  • @Galik But here OP only reads in parallel, he does not write anything to the disk, as far as I can see. Perhaps the problem is that OpenCV functions used here are already parallelized by OpenCV? (I do not know OpenCV, just a wild guess). See also: https://stackoverflow.com/questions/54466572/how-to-properly-multithread-in-opencv-in-2019 – zkoza Jan 29 '21 at 08:27
  • @zkoza You make a good point, I didn't notice the data wasn't being sent to the files. However, it is being read from the files, so the same potential bottleneck would apply - but for reading the data rather than writing it. – Galik Jan 29 '21 at 11:22
  • Also, every single frame is being logged. This also is a potential bottleneck. Writing to a logfile can be time-consuming. Even writing to a terminal can slow a process down considerably - a terminal is also a blocking, serialized output device. – Galik Jan 29 '21 at 11:31

0 Answers0