SeetaFaceDetection识别人脸

SeetaFaceDetection识别人脸

#pragma warning(disable: 4819)

#include <seeta/FaceEngine.h>

#include <seeta/Struct_cv.h>
#include <seeta/Struct.h>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <array>
#include <map>
#include <iostream>

#include <qdebug.h>
#include <QDateTime>

int main()
{
    seeta::ModelSetting::Device device = seeta::ModelSetting::CPU;
    int id = 0;
    seeta::ModelSetting FD_model("E:\\SeetaFaceEngine2\\SeetaFace2_install\\model\\fd_2_00.dat", device, id);
    seeta::ModelSetting PD_model("E:\\SeetaFaceEngine2\\SeetaFace2_install\\model\\pd_2_00_pts5.dat", device, id);
    seeta::ModelSetting FR_model("E:\\SeetaFaceEngine2\\SeetaFace2_install\\model\\fr_2_10.dat", device, id);
    seeta::FaceEngine engine(FD_model, PD_model, FR_model, 2, 16);

    // recognization threshold
    float threshold = 0.5f;

    //set face detector's min face size
    engine.FD.set(seeta::FaceDetector::PROPERTY_MIN_FACE_SIZE, 80);

    //std::vector<std::string> GalleryImageFilename = { "E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\5.jpg" };
    std::vector<std::string> GalleryImageFilename = { "E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\1.jpg","E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\2.jpg","E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\3.jpg","E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\4.jpg","E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\5.jpg","E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\6.jpg","E:\\SeetaFaceEngine2\\SeetaFace2_install\\data\\7.jpg" };
    

    std::vector<int64_t> GalleryIndex(GalleryImageFilename.size());
    for (size_t i = 0; i < GalleryImageFilename.size(); ++i)
    {
        //register face into facedatabase
        std::string &filename = GalleryImageFilename[i];
        int64_t &index = GalleryIndex[i];
        std::cerr << "Registering... " << filename << std::endl;
        seeta::cv::ImageData image = cv::imread(filename);
        auto id = engine.Register(image);
        index = id;
        std::cerr << "Registered id = " << id << std::endl;
    }
    std::map<int64_t, std::string> GalleryIndexMap;
    for (size_t i = 0; i < GalleryIndex.size(); ++i)
    {
        // save index and name pair
        if (GalleryIndex[i] < 0) continue;
        GalleryIndexMap.insert(std::make_pair(GalleryIndex[i], GalleryImageFilename[i]));
    }

    std::cout << "----open camera----" << std::endl;
    // Open default USB camera
    cv::VideoCapture capture;
    capture.open(0);

    cv::Mat frame;

    int width1 = 0;
    int height1 = 0;
    while (capture.isOpened())
    {
        capture >> frame;
        if (frame.empty()) continue;

        width1 = frame.cols;
        height1 = frame.rows;
        cv::resize(frame, frame, cv::Size(width1 / 2, height1 / 2));
        seeta::cv::ImageData image = frame;

        // Detect all faces
        std::vector<SeetaFaceInfo> faces = engine.DetectFaces(image);

        for (SeetaFaceInfo &face : faces)
        {
            // Query top 1
            int64_t index = -1;
            float similarity = 0;

            qDebug() << "-----------------------------------";
            //auto points = engine.DetectPoints(image, face);
            std::vector<SeetaPointF> points = engine.DetectPoints(image, face);
            std::vector<SeetaPointF>::iterator iter_1;
            for (iter_1 = points.begin(); iter_1 != points.end();++iter_1)
            {
                SeetaPointF sp1 = *iter_1;
                qDebug() << "x:" << sp1.x << "  y:" << sp1.y;
            }
            qDebug() << "-----------------------------------";

            auto queried = engine.QueryTop(image, points.data(), 1, &index, &similarity);

            cv::rectangle(frame, cv::Rect(face.pos.x, face.pos.y, face.pos.width, face.pos.height), CV_RGB(128, 128, 255), 3);
            for (int i = 0; i < 5; ++i)
            {
                auto &point = points[i];
                cv::circle(frame, cv::Point(int(point.x), int(point.y)), 2, CV_RGB(128, 255, 128), -1);
            }

            // no face queried from database
            if (queried < 1) continue;

            std::cout << "similarity:" << similarity << std::endl;
            // similarity greater than threshold, means recognized
            if (similarity > threshold)
            {
                std::cout << "person:" << GalleryIndexMap[index] << std::endl;
                cv::putText(frame, GalleryIndexMap[index], cv::Point(face.pos.x, face.pos.y - 5), 3, 1, CV_RGB(255, 128, 128));

                /////////
                QDateTime qdt1 = QDateTime::currentDateTime();
                QString timeStr = qdt1.toString("yyyyMMddhhmmsszzz");
                QString picStr = timeStr.append(".jpg");

                cv::imwrite(picStr.toStdString(), frame);
            }
        }

        cv::imshow("Frame", frame);

        auto key = cv::waitKey(20);
        if (key == 27)
        {
            break;
        }
    }
}

 

posted @ 2019-10-18 18:37  西北逍遥  阅读(972)  评论(0编辑  收藏  举报