dlib人臉特征點對齊

前面我們介紹了使用dlib進行人臉檢測,下面我們給出如何使用dlib進行人臉特征點檢測。我們直接貼出代碼。我們的代碼包括如下幾部分功能:

  • 檢測單張圖片
  • 檢測一個視頻
  • 檢測一個camera
    先給出代碼:

#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>
#include <iostream>
#include <string> 
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>using namespace dlib;
using namespace std;// ----------------------------------------------------------------------------------------
void PrintHelp();
int FaceDetectionAndAlignment(const char* inputname);int main(int argc, char** argv)
{try{// This example takes in a shape model file and then a list of images to// process.  We will take these filenames in as command line arguments.// Dlib comes with example images in the examples/faces folder so give// those as arguments to this program.if (argc == 1){PrintHelp();return 0;}if (strcmp(argv[1],"Demo")==0){if (2==argc){return FaceDetectionAndAlignment("");}else if (3==argc){return FaceDetectionAndAlignment(argv[2]);}}else{PrintHelp();return 0;}}catch (exception& e){cout << "\nexception thrown!" << endl;cout << e.what() << endl;}
}// ----------------------------------------------------------------------------------------
void PrintHelp()
{cout << "Useage:" << endl;cout << "1. test model via a camera: face_alignment.exe  Demo " << endl;cout << "2. test model on a pic:     face_alignment.exe  Demo xx.jpg" << endl;  cout << "3. test model on a video:   face_alignment.exe  Demo xx.avi" << endl;cout << endl;
}
int FaceDetectionAndAlignment(const char* inputname)
{string inputName;CvCapture* capture = 0; cv::Mat frame, frameCopy, image;image_window win;frontal_face_detector detector = get_frontal_face_detector();shape_predictor pose_model;deserialize("D:/dlib/model/shape_predictor_68_face_landmarks.dat") >> pose_model;if (inputname != NULL){inputName.assign(inputname);}// name is empty or a numberif (inputName.empty()){capture = cvCaptureFromCAM(0);if (!capture){cout << "Capture from camera didn't work" << endl;return -1;}}// name is not emptyelse if (inputName.size()){if (inputName.find(".jpg") != string::npos || inputName.find(".png") != string::npos|| inputName.find(".bmp") != string::npos){image = cv::imread(inputName, 1);if (image.empty()){cout << "Read Image fail" << endl;return -1;}}else if (inputName.find(".mp4") != string::npos || inputName.find(".avi") != string::npos|| inputName.find(".wmv") != string::npos){capture = cvCaptureFromAVI(inputName.c_str());if (!capture){cout << "Capture from AVI didn't work" << endl;return -1;}}}// -- 2. Read the video streamif (capture!=nullptr){cout << "In capture ..." << endl;// Grab and process frames until the main window is closed by the user.while (!win.is_closed()){// Grab a frameIplImage* iplImg = cvQueryFrame(capture);iplImg = cvQueryFrame(capture);frame = cv::cvarrToMat(iplImg);if (frame.empty())break;if (iplImg->origin == IPL_ORIGIN_TL) //frame.copyTo(frameCopy);elsecv::flip(frame, frameCopy, 0);// Turn OpenCV's Mat into something dlib can deal with.  Note that this just// wraps the Mat object, it doesn't copy anything.  So cimg is only valid as// long as temp is valid.  Also don't do anything to temp that would cause it// to reallocate the memory which stores the image as that will make cimg// contain dangling pointers.  This basically means you shouldn't modify temp// while using cimg.cv_image<bgr_pixel> cimg(frameCopy);// Detect faces std::vector<rectangle> faces = detector(cimg);// Find the pose of each face.std::vector<full_object_detection> shapes;for (unsigned long i = 0; i < faces.size(); ++i)shapes.push_back(pose_model(cimg, faces[i]));// Display it all on the screenwin.clear_overlay();win.set_image(cimg);win.add_overlay(render_face_detections(shapes));if (cv::waitKey(10) >= 0)goto _cleanup_;}cv::waitKey(0);_cleanup_:cvReleaseCapture(&capture);}else{if (!image.empty()){cout << "In image read" << endl;cv_image<bgr_pixel> cimg(image);// Detect faces std::vector<rectangle> faces = detector(cimg);// Find the pose of each face.std::vector<full_object_detection> shapes;for (unsigned long i = 0; i < faces.size(); ++i)shapes.push_back(pose_model(cimg, faces[i]));// Display it all on the screenwin.clear_overlay();win.set_image(cimg);win.add_overlay(render_face_detections(shapes));cout << "Hit enter to exit..." << endl;cin.get();}}return 0;}

再逐一介紹。

檢測單張圖片

右擊項目,選擇屬性,調試填寫:

Demo  E:\\datasets\\helen\\testset\\30427236_1.jpg

如下圖:

實驗結果:

相當不錯的效果了。

檢測視頻

右擊項目,選擇屬性,調試填寫:

Demo  E:\\datasets\\vid.wmv


效果視頻地址:
http://7xtmgn.com1.z0.glb.clouddn.com/dlibvideo.mp4

檢測camera

右擊項目,選擇屬性,調試填寫:

Demo 

效果視頻地址:
http://7xtmgn.com1.z0.glb.clouddn.com/dlibcamera.mp4

完整源代碼:dlib face_alignment

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258920.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258920.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258920.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

IOS開發基礎知識--碎片13

1:運行程序報the file couldnt be opened because you dont have permission to view it 解決辦法&#xff1a;項目—>targets->build settings->build options->changed the value of the "Compiler for C/C/Objective-C" to Default Compiler. 2:百度…

《LoadRunner 12七天速成寶典》—第2章2.6節第二個性能測試案例

本節書摘來自異步社區《LoadRunner 12七天速成寶典》一書中的第2章&#xff0c;第2.6節第二個性能測試案例&#xff0c;作者陳霽&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 2.6 第二個性能測試案例云云&#xff1a;烤魚吃得很爽。 戀戀&#xff1a;就…

MongoDB_1

突然想去看下MongoDB的東西&#xff0c;于是有了這篇文章。其實很早以前就看過一些關于NoSql的文章&#xff0c;還記得當時里面有介紹MongoDB的&#xff0c;多瞅了2眼&#xff0c;并且在Window下安裝了MongoDB的驅動&#xff0c;小玩了會。今天重新翻出來&#xff0c;沒成想在命…

牛頓法與擬牛頓法,SDM方法的一些注記

SDM方法 考慮一般額NLS問題&#xff1a; f(x)minx||h(x)?y||2這里x為優化參數&#xff0c;h為非線性函數&#xff0c;y是已知變量&#xff0c;如下是基于梯度的迭代公式&#xff1a; ΔxαAJTh(h(x)?y)這里α是步長&#xff0c;A是縮放因子&#xff0c;Jh是h在當前參數x下的…

pyqt5從子目錄加載qrc文件_實戰PyQt5: 045-添加資源文件

添加資源文件在使用PyQt進行圖形界面開發的時候不免要用到一些外部資源&#xff0c;比如圖片&#xff0c;qss配置文件等。在前面代碼中&#xff0c;遇到這類問題&#xff0c;我們使用絕對路徑的方式來解決&#xff0c;這種方式&#xff0c;本身有其不方便之處(比如&#xff0c;…

《 Python樹莓派編程》——2.7 總結

本節書摘來自華章出版社《Python樹莓派編程》一書中的第2章&#xff0c;第2.7節&#xff0c;作者&#xff1a;[美]沃爾弗拉姆多納特&#xff08;Wolfram Donat&#xff09;著 韓德強 等譯&#xff0c;更多章節內容可以訪問云棲社區“華章計算機”公眾號查看。 2.7 總結 本章簡…

ACM的輸入輸出總結

關于ACM的輸入輸出&#xff08;一&#xff09; 一般來說ACM的現場賽會規定輸入輸出 或者是文件輸入標準輸出 也可能是文件輸入文件輸出 如果沒有規定的話那么一般就是標準的輸入輸出了 那說一下輸入輸出的重定向 一般用下面兩種方法 c常用: #include <fstream.h>ifstream…

hdu 2064漢諾塔III 遞推

漢諾塔遞推題&#xff0c;比漢諾塔多了一個限制條件&#xff0c;盤子只允許在相鄰的柱子之間移動。 分析&#xff1a; 第1步:初始狀態&#xff1b; 第2步:把上面的n-1個盤移到第3號桿上&#xff1b; 第3步:把第n個盤從1移到2&#xff1b; 第4步:把前n-1個從3移到1&#xff0c;給…

西門子ddc_鐵門關西門子兩通電動閥VVF42.25-10C+SKD60西

鐵門關西門子兩通電動閥西SIEMENS/西門子電動溫控閥、控制箱、電動蝶閥、電動球閥、超聲波熱量表、超聲波流量計、電磁流量計閥體灰口鑄鐵 EN-GJL-2502.霍尼韋爾主營&#xff1a;樓宇資料系統、熱網自控系統、風機盤管電動兩通閥、空氣壓差開關、水流開關、電動執行器、風閥執行…

swap關于指針的使用

先看下面兩個例子&#xff1a; #include <iostream> // std::cout #include <utility> // std::swapint main() {int x 10, y 20; // x:10 y:20int* p1 &x;int* p2 &y;std::swap(*p1, *p2); // x:20 y:10 …

JS-鍵盤事件之方向鍵移動元素

注意三點&#xff1a; 1&#xff1a;事件名稱onkeydown。 2&#xff1a;事件加給document&#xff0c;而非window。 3&#xff1a; 把元素的top&#xff0c;left值分別用offsetTop&#xff0c;offsetLeft來設定。 <!DOCTYPE html> <html><head><meta char…

Swift學習字符串、數組、字典

一.字符串的使用 let wiseWords "\"I am a handsome\"-boy" var emptyString "" if emptyString.isEmpty{ println("這是一個空值") }簡單說明&#xff1a;isEmpty方法是用來判斷字符串是否為空值的&#xff0c;之后會執行if語句中的…

python對excel操作簡書_Python讀寫Excel表格,就是這么簡單粗暴又好用

最近在做一些數據處理和計算的工作&#xff0c;因為數據是以.CSV格式保存的&#xff0c;因此剛開始直接用Excel來處理。 但是做著做著發現重復的勞動&#xff0c;其實并沒有多大的意義&#xff0c;于是就想著寫個小工具幫著處理。 以前正好在一本書上看到過&#xff0c;使用Pyt…

九度 1470 調整方陣

題目描述&#xff1a; 輸入一個N&#xff08;N<10&#xff09;階方陣&#xff0c;按照如下方式調整方陣&#xff1a;1.將第一列中最大數所在的行與第一行對調。2.將第二列中從第二行到第N行最大數所在的行與第二行對調。依此類推...N-1.將第N-1列中從第N-1行到第N行最大數所…

halcon/c++接口基礎 之 halcon初認識

從今天開始&#xff0c;開始更新博客&#xff0c;主要分享自己最近正在翻譯的Halcon/C教程。先給出第一篇文章&#xff0c;由于此文章&#xff0c;是用latex寫的&#xff0c;直接導成html&#xff0c;保存在七牛云存儲上&#xff0c;所以直接點擊鏈接就看到&#xff0c;后面我將…

指數型組織形成的 9 大驅動因素

指數時代&#xff0c;是一個前所未有的激動人心的世界。 Airbnb, 谷歌, 亞馬遜和GitHub這些知名的公司&#xff0c;都有一個讓人稱羨的共同點&#xff0c;那就是——他們都是非常成功的指數型組織&#xff08;Exponential Organizations&#xff0c;ExO’s&#xff09;。 “在當…

Java for LeetCode 061 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k 2, return 4->5->1->2->3->NULL. 解題思路&#xff1a; 只需找到對應的位置&#xff0c;然后指向head&…

mysqld:表mysql.plugin不存在_99%測試工程師不知道的數據庫知識|干貨

點擊上方“藍字”關注我們數據庫&#xff0c;簡而言之可視為電子化的文件柜——存儲電子文件的處所&#xff0c;用戶可以對文件中的數據進行新增、查詢、更新、刪除等操作。所謂“數據庫”是以一定方式儲存在一起、能與多個用戶共享、具有盡可能小的冗余度、與應用程序彼此獨立…

Windows Phone 執行模型概述

Windows Phone 執行模型控制在 Windows Phone 上運行的應用程序的生命周期&#xff0c;該過程從啟動應用程序開始&#xff0c;直至應用程序終止。 該執行模型旨在始終為最終用戶提供快速響應的體驗。為此&#xff0c;在任何給定時間內&#xff0c;Windows Phone 僅允許一個應用…

halcon/c++接口基礎 之 構造函數與Halcon算子

Halcon/C提供了構造函數&#xff0c;主要基于適合的Halcon算子。比如說HImage和HBarCode基于read_image and create_bar_code_model。 請注意當前的Halcon版本針對不同的算子構造函數的功能不同。如下我們介紹了一些最常用的Halcon算子&#xff0c;而一個完整的構造函數列表可…