{"id":718,"date":"2021-09-01T22:05:10","date_gmt":"2021-09-01T14:05:10","guid":{"rendered":"http:\/\/www.wayln.com\/?p=718"},"modified":"2021-09-01T22:05:10","modified_gmt":"2021-09-01T14:05:10","slug":"c%e5%b0%81%e8%a3%85%e7%b1%bb%e7%9a%84%e5%8a%a8%e6%80%81%e5%ba%93%e3%80%81python%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=718","title":{"rendered":"c++\u5c01\u88c5\u7c7b\u7684\u52a8\u6001\u5e93\u3001python\u8c03\u7528"},"content":{"rendered":"<p>c++\u5c01\u88c5\u7c7b\u7684\u52a8\u6001\u5e93<br \/>\n3.1.1 demo.h\u6587\u4ef6<\/p>\n<pre><code class=\"language-CPP line-numbers\">#ifndef _DEMOSO_H  \n#define _DEMOSO_H  \n\n\/\/ OpenCV includes\n#include &lt;opencv2\/core\/core.hpp&gt;\n#include &lt;opencv2\/highgui\/highgui.hpp&gt;\n#include &lt;opencv2\/imgproc.hpp&gt;\n\nstruct struHeadPose  \n{  \n    float                            angleX;                      \n    float                            angleY;  \n    float                            angleZ;  \n}; \n\nclass CCaculateFaceAngle\n{  \n    public:\n        int getPose(int* imgData, int h, int w, int channels, float cx, float cy, float fx, float fy,struHeadPose&amp; pose);        \n        int LoadModel(char* sModelPath); \n\n    private:\n        LandmarkDetector::FaceModelParameters m_det_parameters;\n        LandmarkDetector::CLNF m_clnf_model;\n}; \n#endif \/\/ _DEMOSO_H    \n\n<\/code><\/pre>\n<p>3.1.2 demo.cpp\u6587\u4ef6<\/p>\n<pre><code class=\"language-CPP line-numbers\">#include \"demo.h\"\n\nint CCaculateFaceAngle::getPose(int* imgData, int h, int w, int channels, float cx, float cy, float fx, float fy, struHeadPose&amp; pose)\n{\n    \u5185\u5bb9\u7701\u7565\n}\nint CCaculateFaceAngle::LoadModel(char* sModelPath)\n{\n\u5185\u5bb9\u7701\u7565\n}\n\n\/\/\u4ee5\u4e0b\u662f\u91cd\u70b9\uff0c\u4e0d\u7136\u4e0d\u4f1a\u5bfc\u51fac++\u7684\u7c7b\u3002\nextern \"C\" \n{\n    CCaculateFaceAngle obj;  \nint getPose(int* imgData, int h, int w, int channels, float cx, float cy, float fx, float fy,struHeadPose&amp; pose) \n    {\n        return obj.getPose(imgData, h, w, channels, cx, cy, fx, fy,pose);   \n    }\n    int LoadModel(char* sModelPath) \n    {  \n        return obj.LoadModel(sModelPath);   \n    }\n}\n\n\n<\/code><\/pre>\n<p>3.2 python\u8c03\u7528c++\u7c7b<\/p>\n<pre><code class=\"language-python line-numbers\">from ctypes import *\nimport cv2\nimport ctypes\nimport numpy as np\n\ndll = cdll.LoadLibrary('\/LandmarkDetector\/class_so\/demo.so');\n# model path\nsModelPath = \"\/LandmarkDetector\/model\/main_clnf_general.txt\"\n\n#image data\nsrc = cv2.imread(\"\/OpenFace-master\/test\/face_0.jpg\") #0-gray\ncols = src.shape[1]\nrows = src.shape[0]\n#print('img shape:{}'.format(src.shape))\nchannels = 0\nif 3==len(src.shape):\n    channels = 3    \nsrc = np.asarray(src, dtype=np.int) \nsrc1 = src.ctypes.data_as(ctypes.c_char_p)\n\n#fx fy cx cy\ncx = cols \/ 2.0\ncy = rows \/ 2.0\nfx = 500 * (cols \/ 640.0)\nfy = 500 * (rows \/ 480.0)\nfx = (fx + fy) \/ 2.0\nfy = fx\n\ncx1 = c_float(cx)\ncy1 = c_float(cy)\nfx1 = c_float(fx)\nfy1 = c_float(fy)\n\n#head pose \nclass struHeadPose(ctypes.Structure):  \n    _fields_ = [(\"angleX\", ctypes.c_float),(\"angleY\", ctypes.c_float),(\"angleZ\", ctypes.c_float)]  \npose = struHeadPose(0.0, 0.0, 0.0) \n\nif dll.LoadModel(sModelPath) &gt; -1:\n    t = dll.getPose(src1, rows, cols, channels, cx1, cy1, fx1, fy1, ctypes.byref(pose))\n\nprint (pose.angleX)\nprint (pose.angleY)\nprint (pose.angleZ)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>c++\u5c01\u88c5\u7c7b\u7684\u52a8\u6001\u5e93 3.1.1 demo.h\u6587\u4ef6 #ifndef _DEMOSO_H #define _DE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,2,33],"tags":[],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-c-c","category-python","category-2","category-33"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=718"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":719,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions\/719"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}