Guest User

Untitled

a guest
Nov 14th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import numpy as np
  2. import tensorflow as tf
  3.  
  4. class PyInterface(object):
  5. def __init__(self):
  6. self.X = None
  7. self.Y = None
  8.  
  9. def generate_np_uint8(self):
  10. np_array = np.random.randint(low=0, high=255, size=(2,2,2),dtype=np.uint8);
  11. print("Python, uint8:", np_array)
  12. return np_array
  13.  
  14. def generate_np_float32(self):
  15. np_array = np.random.rand(2,2,2).astype(np.float32)
  16. print("Python, float:", np_array)
  17. return np_array
  18.  
  19.  
  20. if __name__ == '__main__':
  21. py = PyInterface();
  22. py.generate_np_uint8();
  23. py.generate_np_float32();
  24.  
  25. public:
  26. float * CppPythonHandler::get_float() {
  27. return generate_np_float32();
  28. }
  29. private:
  30. float * CppPythonHandler::generate_np_float32() {
  31. const int ND{3};
  32. float *c_out = new float[8];
  33. PyArrayObject *np_ret;
  34. PyObject* np_array = PyObject_CallMethod(interface, (const char*)"generate_np_float32", NULL, NULL);
  35. if (np_array) {
  36. np_ret = reinterpret_cast<PyArrayObject*>(np_array);
  37. if (PyArray_NDIM(np_ret) != ND) {
  38. std::cout << "Function returned with wrong dimension" << std::endl;
  39. Py_DECREF(np_array);
  40. Py_DECREF(np_ret);
  41. return c_out;
  42. }
  43. c_out[0] = *((float *)PyArray_GETPTR3(np_ret, 0, 0, 0));
  44. c_out[1] = *((float *)PyArray_GETPTR3(np_ret, 0, 0, 1));
  45. c_out[2] = *((float *)PyArray_GETPTR3(np_ret, 0, 1, 0));
  46. c_out[3] = *((float *)PyArray_GETPTR3(np_ret, 0, 1, 1));
  47. c_out[4] = *((float *)PyArray_GETPTR3(np_ret, 1, 0, 0));
  48. c_out[5] = *((float *)PyArray_GETPTR3(np_ret, 1, 0, 1));
  49. c_out[6] = *((float *)PyArray_GETPTR3(np_ret, 1, 1, 0));
  50. c_out[7] = *((float *)PyArray_GETPTR3(np_ret, 1, 1, 1));
  51. for (int i = 0; i < 8; ++i) {
  52. std::cout << c_out[i] << " ";
  53. }
  54. std::cout << std::endl;
  55. Py_DECREF(np_ret);
  56. Py_DECREF(np_array);
  57. return c_out;
  58. }
  59. else{
  60. PyErr_Print();
  61. Py_DECREF(np_array);
  62. Py_DECREF(np_ret);
  63. return c_out;
  64. }
  65. }
  66.  
  67. static int numargs=0;
  68.  
  69. static PyObject*
  70. emb_numargs(PyObject *self, PyObject *args)
  71. {
  72. if(!PyArg_ParseTuple(args, ":numargs"))
  73. return NULL;
  74. return PyLong_FromLong(numargs);
  75. }
  76.  
  77. static PyMethodDef EmbMethods[] = {
  78. {"numargs", emb_numargs, METH_VARARGS,
  79. "Return the number of arguments received by the process."},
  80. {NULL, NULL, 0, NULL}
  81. };
  82.  
  83. static PyModuleDef EmbModule = {
  84. PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
  85. NULL, NULL, NULL, NULL
  86. };
  87.  
  88. static PyObject*
  89. PyInit_emb(void)
  90. {
  91. return PyModule_Create(&EmbModule);
  92. }
  93.  
  94. int main(int argc, char *argv[]) {
  95. if (argc < 1) {
  96. fprintf(stderr,"Usage: call pythonfile funcname [args]n");
  97. return 1;
  98. }
  99.  
  100. numargs = argc;
  101. PyImport_AppendInittab("emb", &PyInit_emb);
  102.  
  103. Py_Initialize(); // initialize python interpreter
  104. PyRun_SimpleString("import sys");
  105. PyRun_SimpleString("if not hasattr(sys, 'argv'):n sys.argv=['']");
  106. PyRun_SimpleString("sys.path.insert(0, "./")");
  107. PyRun_SimpleString("sys.path.insert(0, "./venv/Lib")");
  108. PyRun_SimpleString("sys.path.insert(0, "./venv/Lib/site-packages")");
  109.  
  110. // instance cpp interface object
  111. CppPythonHandler * pInter = new CppPythonHandler("main", "PyInterface");
  112. // unsigned char * np_uint8 = pInter->get_uint8();
  113. float * np_float = pInter->get_float();
  114.  
  115. std::cout<<"End test"<<std::endl;
  116. // delete []pInter;
  117.  
  118. if (Py_FinalizeEx() < 0) {
  119. std::cout << "Fails to release" << std::endl;
  120. return 120;
  121. }
  122. system("pause");
  123. return 0;
  124. }
Add Comment
Please, Sign In to add comment