37,720
社区成员
发帖
与我相关
我的任务
分享
Py_Initialize();
if (!Py_IsInitialized()) {
PyErr_Print(); //打印错误信息
return;
}
string chdir_cmd = string("sys.path.append(\"") + path + "\")";
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.argv = ['']");
PyRun_SimpleString(chdir_cmd.c_str());
// 加载模块
pModuleCalc = PyImport_ImportModule("calcu_embedding");
if (!pModuleCalc) // 加载模块失败
{
PyErr_Print();
return;
}
pModuleLocate = PyImport_ImportModule("locate_face");
if (!pModuleLocate) {
PyErr_Print();
return ;
}
// 加载函数
pInit = PyObject_GetAttrString(pModuleCalc, "init_gs");
pCalc = PyObject_GetAttrString(pModuleCalc, "calcu_embedding");
if (!pInit || !PyCallable_Check(pInit) || !pCalc || !PyCallable_Check(pCalc)) // 验证是否加载成功
{
PyErr_Print();
return;
}
// 加载函数
pLocate = PyObject_GetAttrString(pModuleLocate, "locate_face");
if (!pLocate || !PyCallable_Check(pLocate)) {
PyErr_Print();
return ;
}
initArgs = PyTuple_New(1);
ret = PyTuple_SetItem(initArgs, 0, Py_BuildValue("s", modelpath);
pInitRet = PyObject_CallObject(pInit, NULL);
import facenet
#def init_gs(modelPath):
def init_gs():
#initiate session and graph
start = time.clock()
g = tf.Graph()
g.as_default()
sess = tf.Session()
sess.as_default()
# Load the model
#model = modelPath + '/models/20170512-110547/20170512-110547.pb'
model = 'E:/code/python/facenet/src/models/20170512-110547/20170512-110547.pb'
facenet.load_model(model)
#下面是facenet的
def load_model(model):
# Check if the model is a model directory (containing a metagraph and a checkpoint file)
# or if it is a protobuf file with a frozen graph
model_exp = os.path.expanduser(model)
if (os.path.isfile(model_exp)):
print('Model filename: %s' % model_exp)
with gfile.FastGFile(model_exp,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
else:
print('Model directory: %s' % model_exp)
meta_file, ckpt_file = get_model_filenames(model_exp)
print('Metagraph file: %s' % meta_file)
print('Checkpoint file: %s' % ckpt_file)
saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file))
saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file))