日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

dlib人脸特征点对齐

發布時間:2023/12/10 pytorch 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的dlib人脸特征点对齐的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。