lag in opencv videocapture when i use rtsp camera stream

asked 2017-03-13 09:14:36 -0500

deepmore gravatar image

So i'm currently working on a project that needs to do a facial recognition on rtsp ip cam , i managed to get the rtsp feed with no problems, but when it comes to applying the face recognition the video feed gets too slow and shows a great delay, i even used multithreading to make it better but with no success,here is my code i'm still a beginner in multi threading matters so any help would be appreciated.

 #include <iostream>
#include <thread>
#include "opencv2/opencv.hpp"

#include <vector>
using namespace std;
using namespace cv;
void detect(Mat img, String strCamera) {
    string cascadeName1 = "C:\\ocv3.2\\Build\\install\\etc\\haarcascades\\haarcascade_frontalface_alt.xml";
    CascadeClassifier facedetect;
    bool loaded1 = facedetect.load(cascadeName1);
    Mat original;
    vector<Rect> human;
    cvtColor(img, img, CV_BGR2GRAY);
    equalizeHist(img, img);
    facedetect.detectMultiScale(img, human, 1.1, 2, 0 | 1, Size(40, 80), Size(400, 480));
    if (human.size() > 0)
        for (int gg = 0; gg < human.size(); gg++)
            rectangle(original, human[gg].tl(), human[gg].br(), Scalar(0, 0, 255), 2, 8, 0);
    imshow("Detect " + strCamera, original);
    int key6 = waitKey(40);
    //End of the detect
void stream(String strCamera) {
    VideoCapture cap(strCamera);
    if (cap.isOpened()) {
        while (true) {
            Mat frame;
            cap >> frame;
            resize(frame, frame, Size(640, 480));
            detect(frame, strCamera);
int main() {
    thread cam1(stream, "rtsp://admin:password@ipaddress:554/live2.sdp?tcp");
    thread cam2(stream, "rtsp://admin:password@ipaddress/live2.sdp?tcp");
    return 0;
edit retag flag offensive close merge delete


Just wondering if you have found a solution, im facing the same problem as well.

abdane gravatar imageabdane ( 2019-02-14 18:40:23 -0500 )edit