将DAA人脸检测做成动态库以便其他程序调用,采用隐式调用完成。
给出部分关于dll制作的关键代码:
1.DLL
多项目DLL编写注意将其他相关项目设置成静态库,以及各库涉及的版本一致性。
DLL.h
1 #define DLL_API __declspec(dllexport) 2 #include<iostream> 3 #include<opencv.hpp> 4 5 6 DLL_API void FaceDecter(cv::Mat& source,std::vector<cv::Rect>& rects);
DLL.cpp
1 #include "stdafx.h" 2 #include "FaceDectectDll.h" 3 #include "../libmi_dpm/face_detector.hpp" 4 #include <fstream> 5 6 using namespace std; 7 using namespace mi_dpm; 8 using namespace cv; 9 10 DLL_API void FaceDecter(cv::Mat &source, std::vector <cv::Rect> & rects) 11 { 12 Detector face_det; 13 face_det.detectInit(); 14 vector<float> scores; 15 vector<int> picked; 16 picked = face_det.detectFastMerge(source, rects, scores); 17 }
2.DLL调用
在demo中新建文件夹 lib-DAA ,include-DAA 存放静态库以及相关头文件,在可执行文件下放入动态库dll。
demo.h
1 // stdafx.h : include file for standard system include files, 2 // or project specific include files that are used frequently, 3 // but are changed infrequently 4 5 #pragma once 6 7 #ifndef _SECURE_ATL 8 #define _SECURE_ATL 1 9 #endif 10 11 #ifndef VC_EXTRALEAN 12 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers 13 #endif 14 15 16 17 #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit 18 19 // turns off MFC's hiding of some common and often safely ignored warning messages 20 #define _AFX_ALL_WARNINGS 21 22 #include "../include-DAA/FaceDectectDll.h" 23 #pragma comment(lib,"../lib-DAA/lib3000fps.lib") 24 #pragma comment(lib,"../lib-DAA/liblinear.lib") 25 #pragma comment(lib,"../lib-DAA/libmi_dpm.lib") 26 #pragma comment(lib,"../lib-DAA/mi_dpm.lib") 27 28 29 //#include <cxcore.h> 30 //#include <highgui.h> 31 #include<opencv.hpp> 32 33 34 using namespace cv; 35 using namespace std;
demo.cpp
1 #include <iostream> 2 #include "stdafx.h" 3 4 //#include <Windows.h> 5 6 int main() 7 { 8 Mat im = imread("F:/科研/人脸识别/DAA/mi_dpm/mi_dpm/testpic/tmp_rs.jpg"); 9 vector<Rect> re;// = FaceDecter(im); 10 FaceDecter(im, re); 11 int nNum = re.size(); 12 cout << nNum << endl; 13 for (int i = 0; i < nNum; i++) 14 { 15 cv::rectangle(im, re[i], cv::Scalar(0, 125, 255), 2); 16 printf("%d %d %d %d ", re[i].x, re[i].y, re[i].width, re[i].height); 17 } 18 imshow("dec",im); 19 waitKey(30); 20 system("pause"); 21 22 }