在Linux下用Makefile编译一个c++库以供python调用
其Makefile文件如下:
#This is a makefile for Baidu AI sdk based on Linux
cc := g++
LDFLAGS1 := XXXX/Baidu_Face/test-face-api/lib3/json/lib-ubuntu
LDFLAGS2 := XXXX/Baidu_Face/test-face-api/facelib
LDFLAGS3 := /home/scrooge/anaconda3/lib
libs1 := json
libs2 := BaiduFaceApi-ubuntu
libs3 := FaceSDK
libs4 := python3.7m
LIBS := /home/scrooge/anaconda3/lib/libpython3.7m.a
#DEFINES :=
INCLUDE1 := /home/scrooge/anaconda3/include/python3.7m/
INCLUDE2 := XXXX/Baidu_Face/test-face-api/include/
#INCLUDE3 :=XXXX/Baidu_Face/test-face-api/include/
#CFLAGS :=
#CXXFLAGS:=
# link parameter
LIB := noboost.so
#link
$(LIB) : baiduAi_linux.o
$(cc) -shared -fPIC -o $@ $^ -Xlinker -export-dynamic -L$(LDFLAGS3) -l$(libs4) -L$(LDFLAGS1) -l$(libs1) -L$(LDFLAGS2) -l$(libs2)
#compile
baiduAi_linux.o : baiduAi_linux.cpp
$(cc) -I$(INCLUDE1) -I$(INCLUDE2) -Wall -fPIC -c baiduAi_linux.cpp -o $@
#clean
clean:
rm -fr *.o
已知编译Makefile文件未报错
测试cpp文件如下:
#include "Python.h"
#include
#include
#include
#include
#include
#include "baidu_face_api.h"
#include "json/json.h"
using namespace std;
BaiduFaceApi *api = new BaiduFaceApi();
void TrackInfo2Json(TrackFaceInfo info, int index, Json::Value& val)
{
Json::Value jsinfo;
FaceInfo ibox = info.box;
Json::Value jsbox;
jsbox["mWidth"] = ibox.mWidth;
jsbox["mAngle"] = ibox.mAngle;
jsbox["mCenter_y"] = ibox.mCenter_y;
jsbox["mCenter_x"] = ibox.mCenter_x;
jsbox["mConf"] = ibox.mConf;
jsinfo["ibox"] = jsbox;
std::vector landmarks = info.landmarks;
string strland = "[";
for (int i = 0; i < landmarks.size(); i++)
{
char buffer[50];
if (i == landmarks.size() - 1)
sprintf(buffer, "%d", landmarks[i]);
else
sprintf(buffer, "%d,", landmarks[i]);
string strbuf = buffer;
strland += strbuf;
}
strland += "]";
jsinfo["landmarks"] = strland;
jsinfo["score"] = info.score;
jsinfo["face_id"] = (int)info.face_id;
std::vector headpose = info.headPose;
string strheadpose = "[";
for (int i = 0; i < headpose.size(); i++)
{
char buffer[50];
if (i == headpose.size() - 1)
sprintf(buffer, "%f", headpose[i]);
else
sprintf(buffer, "%f,", headpose[i]);
string strpos = buffer;
strheadpose += strpos;
}
strheadpose += "]";
jsinfo["headPose"] = strheadpose;
val[index] = jsinfo;
}
//人脸检测
string track(string simg)
{
/*cv::Mat mat=ndarray2Mat(img);
if (mat.empty())
return "";*/
std::vector *track_info = new std::vector();
bool stop = false;
while (!stop)
{
try
{
//p::tuple shape = p::make_tuple(10, 10, 3);
//np::dtype dtype = np::dtype::get_builtin();
//np::ndarray a = np::zeros(shape, dtype);
//string simg = ImageBase64::file2base64("C:\\Users\\bymm\\Desktop\\pydtb\\x64\\Release\\2.jpg");
//string simg = ImageBase64::encode((unsigned char*)a.get_data(), 300);
//string simg = ImageBase64::mat2base64(mat);
track_info->clear();
int size = api->track(track_info, simg.c_str(), 1, 5);
//int size = api->track(track_info, mat, 5);
std::cout << size << std::endl;
Json::Value root;
for (int i = 0; i < size; i++)
{
TrackFaceInfo info = track_info->at(i);
TrackInfo2Json(info, i, root);
}
//Json::StreamWriterBuilder builder;
//std::unique_ptr writer(builder.newStreamWriter());
//string json_file = Json::writeString(builder, root);
//above code is based on windows
Json::FastWriter writer;
string json_file = writer.write(root);
return json_file;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
delete track_info;
return "";
}
//获得人脸属性(年龄,种族,是否戴眼镜等)
string face_att(string simg)
{
/*cv::Mat mat = ndarray2Mat(img);
if (mat.empty())
return "";*/
try
{
//string simg = ImageBase64::mat2base64(mat);
//cout<face_attr(simg.c_str(), 1);
//cout<
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return "";
}
}
// 测试获取人脸质量
string face_quality(string simg)
{
//cv::Mat mat = ndarray2Mat(img);
//string simg = ImageBase64::mat2base64(mat);
std::string res = api->face_quality(simg.c_str(), 1);
return res;
}
PyObject* track(PyObject* self, PyObject* args)
{
const char* simg;
if (!PyArg_ParseTuple(args, "s", &simg))
{
return NULL;
}
return Py_BuildValue("s", track(simg).c_str());
}
PyObject* face_att(PyObject* self, PyObject* args)
{
const char* simg;
if (!PyArg_ParseTuple(args, "s", &simg))
{
return NULL;
}
//cout<
}
PyObject* face_quality(PyObject* self, PyObject* args)
{
const char* simg;
if (!PyArg_ParseTuple(args, "s", &simg))
{
return NULL;
}
string res = face_quality(simg);
std::cout << "res is:" << res << std::endl;
return Py_BuildValue("s", res.c_str());
}
static PyMethodDef GreateModuleMethods[] = {
{ "face_att", face_att, METH_VARARGS, "" },
{ "face_quality", face_quality, METH_VARARGS, "" },
{ "track", track, METH_VARARGS, "" },
{ NULL, NULL, 0, NULL }
};
static struct PyModuleDef noboost =
{
PyModuleDef_HEAD_INIT,
"noboost", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
GreateModuleMethods
};
PyMODINIT_FUNC PyInit_noboost(void)
{
api->set_min_face_size(60);
return PyModule_Create(&noboost);
}
编译后未报错,但显示“face-resource folder not exist,please check it”
已把附带的face-resource文件放入上级目录(诸目录都有该文件复印件)