代码备份

成为她曾经期待的样子 / 2023-05-27 / 原文

#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;
}