C++ dlib Caffe 人脸裁剪与预测

来源:互联网 发布:淘宝交易指数 构成 编辑:程序博客网 时间:2024/06/06 04:39

CmakeLists.txt

cmake_minimum_required(VERSION 2.8.8)project(faceshapeAPI)set(CMAKE_CXX_STANDARD 11)set(Caffe_DIR /home/leo/caffe/build)find_package(Caffe REQUIRED)if (NOT Caffe_FOUND)    message(FATAL_ERROR "Caffe NOT Fund!")endif (NOT Caffe_FOUND)include_directories(${Caffe_INCLUDE_DIRS})INCLUDE(/home/leo/dlib-19.4/dlib/cmake)add_definitions(${Caffe_DEFINITIONS}) # ex. -DCPU_ONLYset(SOURCE_FILES main.cpp predict.cpp predict.h cropface.h cropface.cpp)add_executable(faceshapeAPI ${SOURCE_FILES})target_link_libraries(faceshapeAPI ${Caffe_LIBRARIES} dlib)

cropface.h

//// Created by leo on 17-8-14.//#ifndef FACESHAPEAPI_CROPFACE_H#define FACESHAPEAPI_CROPFACE_H#ifndef DLIB#define DLIB#include <dlib/image_processing/frontal_face_detector.h>#include <dlib/image_processing/render_face_detections.h>#include <dlib/image_processing.h>#include <dlib/gui_widgets.h>#include <dlib/image_io.h>#endif#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <iostream>using namespace dlib;using namespace std;using namespace cv;Mat getDets(string image_path, frontal_face_detector &detector);#endif //FACESHAPEAPI_CROPFACE_H

cropface.cpp

//// Created by leo on 17-8-14.//#include "cropface.h"Mat getDets(string image_path, frontal_face_detector &detector, shape_predictor &predictor) {//    Rect rect;    Mat src = imread(image_path);    std::vector<long> xs, ys;    try    {//        Mat src = imread(image_path);        array2d<rgb_pixel> img;        load_image(img, image_path);        // Now tell the face detector to give us a list of bounding boxes        // around all the faces it can find in the image.        std::vector<dlib::rectangle> dets;        dets = detector(img);        cout << "Number of faces detected: " << dets.size() << endl;        if(dets.size()==1){            std::vector<full_object_detection> shapes; //注意形状变量的类型, full_object_detection            full_object_detection shape = predictor(img, dets[0]);            for(int i=0; i<68; i++) {                dlib::point point  = shape.part(i);                long x = point.x();                long y = point.y();                xs.push_back(x);                ys.push_back(y);            }        } else {            cout << "检测不到人脸,或有多张人脸" << endl;            exit(-1);        }    }    catch (exception& e)    {        cout << "\nexception thrown!" << endl;        cout << e.what() << endl;    }    std::vector<long>::iterator iter_min_x = min_element(begin(xs), end(xs));    std::vector<long>::iterator iter_max_x = max_element(begin(xs), end(xs));    std::vector<long>::iterator iter_min_y = min_element(begin(ys), end(ys));    std::vector<long>::iterator iter_max_y = max_element(begin(ys), end(ys));    long min_x = *iter_min_x;    long max_x = *iter_max_x;    long min_y = *iter_min_y;    long max_y = *iter_max_y;    long max_y_real = max_y + (max_y-min_y)*0.08;    if(max_y_real>src.size().height)        max_y_real = max_y;    cv::Mat dst=src.rowRange(min_y, max_y_real).colRange(min_x, max_x).clone();//    cv::Mat dst2(src,rect);//    imshow("test", dst);//    waitKey(0);    return dst;}

predict.h

//// Created by leo on 17-8-14.//#ifndef FACESHAPEAPI_PREDICT_H#define FACESHAPEAPI_PREDICT_H#include <caffe/caffe.hpp>#ifdef USE_OPENCV#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#endif  // USE_OPENCV#include <algorithm>#include <iosfwd>#include <memory>#include <string>#include <utility>#include <vector>using namespace caffe;  // NOLINT(build/namespaces)using std::string;/* Pair (label, confidence) representing a prediction. */typedef std::pair<string, float> Prediction;class Classifier {public:    /**     * 有mean file 文件     * @param model_file     * @param trained_file     * @param mean_file     * @param label_file     */    Classifier(const string& model_file,               const string& trained_file,               const string& mean_file,               const string& label_file);    /**     * 没有mean file文件     * @param model_file     * @param trained_file     * @param label_file     */    Classifier(const string& model_file,               const string& trained_file,               const string& label_file);    std::vector<Prediction> Classify(const cv::Mat& img, int N = 5);private:    void SetMean(const string& mean_file);    void SetMean();    std::vector<float> Predict(const cv::Mat& img);    void WrapInputLayer(std::vector<cv::Mat>* input_channels);    void Preprocess(const cv::Mat& img,                    std::vector<cv::Mat>* input_channels);private:    shared_ptr<Net<float> > net_;    cv::Size input_geometry_;    int num_channels_;    cv::Mat mean_;    std::vector<string> labels_;    cv::Scalar s_;};#endif //FACESHAPEAPI_PREDICT_H

predict.cpp

//// Created by leo on 17-8-14.//#include "cropface.h"Mat getDets(string image_path, frontal_face_detector &detector) {    Mat face;    try    {        array2d<rgb_pixel> img;        load_image(img, image_path);        Mat src = imread(image_path);//        pyramid_up(img);        // Now tell the face detector to give us a list of bounding boxes        // around all the faces it can find in the image.        std::vector<dlib::rectangle> dets;        dets = detector(img);        cout << "Number of faces detected: " << dets.size() << endl;        if(dets.size()>1){            cout << "只预测第一张人脸" << endl;        }        dlib::rectangle det = dets[0];        image_window win;        win.clear_overlay();        win.set_image(img);        win.add_overlay(det, rgb_pixel(255,0,0));        win.wait_until_closed();        long left = det.left(), top = det.top(), right = det.right(), bottom = det.bottom();        long width = det.width();        long height = det.height();        long new_left = left * 0.8;        long new_top = top + height*0.2;        long new_right = left*1.2 + width;        long new_bottom = top + height*1.3;        if(new_left<0)            new_left = left;        if(new_top<0)            new_top = top;        if(new_right>src.size().width)            new_right = src.size().width;        if(new_bottom>src.size().height)            new_bottom = src.size().height;        Rect rect(new_left, new_top, new_right - new_left, new_bottom - new_top);        face = Cut_img(src, rect);        return face;    }    catch (exception& e)    {        cout << "\nexception thrown!" << endl;        cout << e.what() << endl;    }    return face;}Mat Cut_img(Mat src_img, Rect rect) {    Mat roi_img;    src_img(rect).copyTo(roi_img);    return  roi_img;}
阅读全文
0 0