代码:
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/objdetect.hpp"
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
void face_detect()
{
Mat frame;
VideoCapture cap;
cap.open("http://ivi.bupt.edu.cn/hls/cctv2.m3u8");
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open video\n";
return;
}
double fps = cap.get(cv::CAP_PROP_FPS);
cout << "fps = "<< fps <<endl;
//获取cap视频流的每帧大小
int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
cout<<"width = "<<width <<", height = "<<height<<endl;
Size S(width, height);
Scalar blue(255, 0, 0);
CascadeClassifier classifier;
classifier.load("./xml/haarcascade_frontalface_alt.xml");
std::vector<Rect> detected_objects;
int cnt = 0;
for (;;)
{
cap.read(frame);
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
if(cnt++ % 5 == 0)
{
classifier.detectMultiScale( frame, detected_objects);
for ( size_t i = 0; i < detected_objects.size(); i++ )
{
rectangle(frame, detected_objects[i], blue, 3);
}
}
imshow("Live", frame);
if (waitKey(38) >= 0)
break;
}
}
运行结果:
./opencv-demo OpenCV4 Example.
OpenCV Version = 4.5.2
[NULL @ 0x556838ec4680] non-existing SPS 0 referenced in buffering period
[NULL @ 0x556838ec4680] SPS unavailable in decode_picture_timing
[h264 @ 0x556838eed000] non-existing SPS 0 referenced in buffering period
[h264 @ 0x556838eed000] SPS unavailable in decode_picture_timing
fps = 25
width = 720, height = 576
[h264 @ 0x55683916bfc0] error while decoding MB 30 33, bytestream -5
[h264 @ 0x556838f67040] error while decoding MB 23 1, bytestream -15
[h264 @ 0x556838fcca00] error while decoding MB 30 14, bytestream -11
[h264 @ 0x55683916bfc0] error while decoding MB 9 27, bytestream -9
运行效果: