This is the first time I post a question here, so I hope, I am doing it correctly otherwise please let me know.
I am trying to make a wrapper with c++ and call a python module. I am able to call a simple python module, however, as soon as I include a python module, which imports modules, I get a segmentation fault.
I import the code as, where I use a helper class:
setenv("PYTHONPATH", "./AugmentedAutoencoder/auto_pose/test", 0);
CPyInstance hInstance;
if(!Py_IsInitialized()){
std::cout << "unable to init Py" << std::endl;
}
else{
CPyObject pName = PyUnicode_FromString("aae_webcam");
CPyObject pModule = PyImport_Import(pName);
if(pModule)
{
std::cout << "Module imported.. " << std::endl;
}
else
{
printf("ERROR: Module not imported \n");
PyErr_Print();
}
}
The beginning of the python module imported is something like:
import cv2
import argparse
import tensorflow as tf
import numpy as np
import os
import configparser
from auto_pose.ae import factory
from auto_pose.ae import utils as u
from webcam_video_stream import WebcamVideoStream
parser = argparse.ArgumentParser()
parser.add_argument("experiment_name")
arguments = parser.parse_args()
full_name = arguments.experiment_name.split('/')
experiment_name = full_name.pop()
experiment_group = full_name.pop() if len(full_name) > 0 else ''
I've tried to look for the answer online, however, unsuccessful. Thus I hope you are able to come to my aid.
Edit: The CPyObject is an instance of a class:
class CPyInstance
{
public:
CPyInstance()
{
Py_Initialize();
}
~CPyInstance()
{
Py_Finalize();
}
};
class CPyObject
{
private:
PyObject *p;
public:
CPyObject() : p(NULL)
{}
CPyObject(PyObject* _p) : p(_p)
{}
~CPyObject()
{
Release();
}
PyObject* getObject()
{
return p;
}
PyObject* setObject(PyObject* _p)
{
return (p=_p);
}
PyObject* AddRef()
{
if(p)
{
Py_INCREF(p);
}
return p;
}
void Release()
{
if(p)
{
Py_DECREF(p);
}
p= NULL;
}
PyObject* operator ->()
{
return p;
}
bool is()
{
return p ? true : false;
}
operator PyObject*()
{
return p;
}
PyObject* operator = (PyObject* pp)
{
p = pp;
return p;
}
operator bool()
{
return p ? true : false;
}
};