代码备份
#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> #include <opencv2/opencv.hpp> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <dlib/gui_widgets.h> #include <opencv2\imgproc\imgproc.hpp> #include <windows.h> #include <opencv2/opencv.hpp> #include <cmath> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> /*#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> #include <opencv2/opencv.hpp> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <dlib/gui_widgets.h> using namespace dlib; using namespace std; int main() { frontal_face_detector detector = get_frontal_face_detector(); frontal_face_detector detector1 = get_frontal_face_detector(); shape_predictor pose_mode1; deserialize("D:/data/shape_predictor_68_face_landmarks.dat") >> pose_mode1;//文件地址 image_window win; //调用window图像处理opencv array2d<unsigned char> img; string path = "D:/0001.png"; load_image(img, "D:/0001.png"); cv::Mat temp = cv::imread(path); cv::Mat temp1 = cv::imread(path); cv_image<bgr_pixel> cimg(temp); //cv_image<bgr_pixel> cimg(temp1); std::vector<rectangle> faces = detector(cimg); //pyramid_up(img); std::vector<rectangle> dets = detector(img); cout << "识别到的人脸数 :" << dets.size() << endl; win.clear_overlay(); win.set_title("facedemo"); win.set_image(img); win.add_overlay(dets, rgb_pixel(255, 0, 0)); cout <<"识别到的人脸数"<< faces.size() << endl; std::vector<full_object_detection> shapes; for (unsigned long i = 0; i < faces.size(); ++i) shapes.push_back(pose_mode1(cimg, faces[i])); if (!shapes.empty()) { for (int i = 0; i < 68; i++) { circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1); shapes[0].part(i).x();//68个 } } imshow("Dlib面部识别", temp); cv::imwrite("D:/pp.png", temp); cv::waitKey(0); cout << "再输入下一张图片" << endl; cin.get(); system("pause"); return 0; }*/ /*#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> #include <opencv2/opencv.hpp> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <dlib/gui_widgets.h> using namespace dlib; using namespace std; int main() { auto path = "D:\\data\\dlib - 19.24\\dlib - 19.24\\examples\\video_frames"; // Get the list of video frames. std::vector<file> files = get_files_in_directory_tree(path, match_ending(".jpg")); std::sort(files.begin(), files.end()); array2d<unsigned char> img; load_image(img, files[0]); //加载第一帧 correlation_tracker tracker; //创建tracker //下面所创建的矩形,对果汁盒进行了框选 tracker.start_track(img, centered_rect(point(93, 110), 38, 86)); image_window win; for (unsigned long i = 1; i < files.size(); ++i) { load_image(img, files[i]); tracker.update(img); //根据图像,对tracker进行更新 win.set_image(img); win.clear_overlay(); win.add_overlay(tracker.get_position()); Sleep(100); } }*/ //using namespace dlib; using namespace std; using namespace cv; int main() { VideoCapture cap("D:/viedio/002.mp4"); if (!cap.isOpened()) { cout << "video not exist!" << endl; return -1; } //frontal_face_detector detector = get_frontal_face_detector(); //std::vector<rectangle> faces = detector(cap); long FRAMECNT = cap.get(CAP_PROP_FRAME_COUNT); Mat frame, mask, maskCp; vector<vector<Point>> cnts; Rect maxRect; const double RECT_HW_RATIO = 1.25; // 人体长宽比阈值 const double RECT_AREA_RATIO = 0.08; // 人体占整个图像最小比例阈值 const double RECT_AREA_RATIO2 = 0.2; // 人体占整体图像最大比例阈值 Ptr<BackgroundSubtractorMOG2> bgsubtractor = createBackgroundSubtractorMOG2(); bgsubtractor->setHistory(20); bgsubtractor->setVarThreshold(100); bgsubtractor->setDetectShadows(true); bool hasPeople = true; // 是否有人 int count = 0; // 帧数 int hasPeopleFrameCnt = 0; // 每K帧统计到的有人帧数 int spaceFrames = 0; // 每隔125帧统计一次 const int SPACE_FRAME =5; while (++count < FRAMECNT - 10) { cap >> frame; resize(frame, frame, Size(frame.cols / 4, frame.rows / 4)); // 背景更新 bgsubtractor->apply(frame, mask, 0.002); // 中值滤波 medianBlur(mask, mask, 3); // 阈值分割,去阴影 threshold(mask, mask, 200, 255,THRESH_BINARY); // 找轮廓 maskCp = mask.clone(); findContours(maskCp, cnts, 0, CHAIN_APPROX_SIMPLE); vector<Point> maxCnt; for (int i = 0; i < cnts.size(); ++i) { maxCnt = maxCnt.size() > cnts[i].size() ? maxCnt : cnts[i]; } // 画最大外接矩形 if (maxCnt.size() > 0) { maxRect = boundingRect(maxCnt); double rectAreaRatio = (double)maxRect.area() / (frame.cols * frame.rows); if ((double)maxRect.height / maxRect.width > RECT_HW_RATIO && rectAreaRatio > RECT_AREA_RATIO && rectAreaRatio < RECT_AREA_RATIO2) { rectangle(frame, maxRect.tl(), maxRect.br(), Scalar(0, 255, 0), 2); ++hasPeopleFrameCnt; } } ++spaceFrames; if (spaceFrames >= SPACE_FRAME) { if (hasPeopleFrameCnt > SPACE_FRAME / 8) { hasPeople = true; cout << count << ":有人" << endl; } else { hasPeople = false; cout << count << ":无人" << endl; } hasPeopleFrameCnt = 0; spaceFrames = 0; } imshow("frame", frame); imshow("mask", mask); if (waitKey(10) == 29) { break; } } return 0; };
#include <opencv2\opencv.hpp> using namespace cv; using namespace std; int main() { //读取视频或摄像头 VideoCapture capture(0); while (true) { Mat frame; capture >> frame; imshow("读取视频", frame); waitKey(30); //延时30 } return 0; }
5 月28备份
#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> #include <opencv2/opencv.hpp> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <opencv2\imgproc\imgproc.hpp> #include <windows.h> #include <cmath> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_io.h> #include <opencv2\opencv.hpp> using namespace dlib; using namespace std; //using namespace cv; /*#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> #include <opencv2/opencv.hpp> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <dlib/gui_widgets.h> using namespace dlib; using namespace std; /*int main() { VideoCapture camputer(0); VideoCapture cap; cap = camputer; if (!cap.isOpened()) { cout << "video not exist!" << endl; return -1; } //frontal_face_detector detector = get_frontal_face_detector(); //std::vector<rectangle> faces = detector(cap); long FRAMECNT = cap.get(CAP_PROP_FRAME_COUNT); Mat frame, mask, maskCp; vector<vector<Point>> cnts; Rect maxRect; const double RECT_HW_RATIO = 1.25; // 人体长宽比阈值 const double RECT_AREA_RATIO = 0.08; // 人体占整个图像最小比例阈值 const double RECT_AREA_RATIO2 = 0.2; // 人体占整体图像最大比例阈值 Ptr<BackgroundSubtractorMOG2> bgsubtractor = createBackgroundSubtractorMOG2(); bgsubtractor->setHistory(20); bgsubtractor->setVarThreshold(100); bgsubtractor->setDetectShadows(true); bool hasPeople = true; // 是否有人 int count = 0; // 帧数 int hasPeopleFrameCnt = 0; // 每K帧统计到的有人帧数 int spaceFrames = 0; // 每隔125帧统计一次 const int SPACE_FRAME =5; while (++count < FRAMECNT - 10) { cap >> frame; resize(frame, frame, Size(frame.cols / 4, frame.rows / 4)); // 背景更新 bgsubtractor->apply(frame, mask, 0.002); // 中值滤波 medianBlur(mask, mask, 3); // 阈值分割,去阴影 threshold(mask, mask, 200, 255,THRESH_BINARY); // 找轮廓 maskCp = mask.clone(); findContours(maskCp, cnts, 0, CHAIN_APPROX_SIMPLE); vector<Point> maxCnt; for (int i = 0; i < cnts.size(); ++i) { maxCnt = maxCnt.size() > cnts[i].size() ? maxCnt : cnts[i]; } // 画最大外接矩形 if (maxCnt.size() > 0) { maxRect = boundingRect(maxCnt); double rectAreaRatio = (double)maxRect.area() / (frame.cols * frame.rows); if ((double)maxRect.height / maxRect.width > RECT_HW_RATIO && rectAreaRatio > RECT_AREA_RATIO && rectAreaRatio < RECT_AREA_RATIO2) { rectangle(frame, maxRect.tl(), maxRect.br(), Scalar(0, 255, 0), 2); ++hasPeopleFrameCnt; } } ++spaceFrames; if (spaceFrames >= SPACE_FRAME) { if (hasPeopleFrameCnt > SPACE_FRAME / 8) { hasPeople = true; cout << count << ":有人" << endl; } else { hasPeople = false; cout << count << ":无人" << endl; } hasPeopleFrameCnt = 0; spaceFrames = 0; } imshow("frame",frame); imshow("mask", mask); if (waitKey(10) == 29) { break; } } return 0; };*/ int main() { cv::VideoCapture campture(0);//获取硬件摄像头读取视频或摄像头 shape_predictor pose_mode1; deserialize("D:/data/shape_predictor_68_face_landmarks.dat") >> pose_mode1;//采用64个人脸特征 image_window win;//调用window图片窗口 while (true) { frontal_face_detector detector1 = get_frontal_face_detector();//创建一个脸的对象 cv::Mat farme; campture >> farme; cv_image<bgr_pixel> cimg(farme); /* 这里可以添加cnn卷积池化技术以及对图片的修复以及图像处理 通过while循环实现图片的不断更行*/ std::vector<rectangle> dets = detector1(cimg); win.clear_overlay(); win.set_title("facedemo"); win.set_image(cimg); win.add_overlay(dets, rgb_pixel(0, 255, 0)); std::vector<full_object_detection> shapes; for (unsigned long i = 0; i < dets.size(); ++i) shapes.push_back(pose_mode1(cimg, dets[i])); if (!shapes.empty()) { for (int i = 0; i < 68; i++) { circle(farme, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1); shapes[0].part(i).x();//68个 } } cv::imshow("Dlib面部识别", farme); cv::waitKey(15); cout << "检测人脸数" << dets.size() << endl; } return 0; }
5月31日
#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <iostream> #include <dlib/opencv.h> #include <opencv2/opencv.hpp> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <opencv2\imgproc\imgproc.hpp> #include <windows.h> #include <cmath> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_io.h> #include <opencv2\opencv.hpp> using namespace dlib; using namespace std; int main() { cv::VideoCapture campture(0);//获取硬件摄像头读取视频或摄像头 shape_predictor pose_mode1;//加载设定一个模型 deserialize("D:/data/shape_predictor_68_face_landmarks.dat") >> pose_mode1;//采用68个人脸特征定位点 image_window win;//调用window图片窗口 cv::namedWindow("Dlib面部识别", cv::WindowFlags::WINDOW_AUTOSIZE);//对cv::imshow创建窗体对象 while (true) { frontal_face_detector detector1 = get_frontal_face_detector();//创建一个脸的对象 cv::Mat video; campture >> video; cv_image<bgr_pixel> cimg(video);//将视频转化成图片 /* 这里可以添加cnn卷积池化技术以及对图片的修复以及图像处理关键信息的捕捉 通过while循环实现图片的不断更行*/ std::vector<rectangle> face = detector1(cimg); win.clear_overlay(); win.set_title("facedemo"); win.set_image(cimg); win.add_overlay(face, rgb_pixel(0, 255, 0)); std::vector<full_object_detection> shapes; for (unsigned long i = 0; i < face.size(); ++i) shapes.push_back(pose_mode1(cimg, face[i]));//cimg if (!shapes.empty()) { for (int i = 0; i < 68; i++) { circle(video, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1); shapes[0].part(i).x();//68个 } } if (face.size() > 0)//窗口添加文字说明 { cv::putText(video, "Detect to the human face:)", cvPoint(25, 25), cv::FONT_HERSHEY_PLAIN, 2.0, cv::Scalar(0, 255, 0), 1); } else { cv::putText(video, "NO detect to the human face:(", cvPoint(20, 25), cv::FONT_HERSHEY_PLAIN, 2.0, cv::Scalar(255, 255, 0), 1); } for (unsigned long i = 0; i < face.size(); ++i)//将脸部进行矩形筛选 { cv::Point p1(100,50), p2(600, 400); cv::Scalar colorRectangle2(221, 121, 7);//GRB颜色值 cv::rectangle(video, p1, p2, colorRectangle2, 1);//画出一个矩形框 } cv::imshow("Dlib面部识别", video); cv::waitKey(10); cout << "检测人脸数" << face.size() << endl; } return 0; } //
本文来自博客园,作者:LILi2209,转载请注明原文链接:https://www.cnblogs.com/Lili-202209/p/17436629.html

浙公网安备 33010602011771号