I would like to call python script files from my c++ program.
I am not sure that the people I will distribute to will have python installed.
I would like to call python script files from my c++ program.
This means that you want to embed Python in your C++ application. As mentioned in Embedding Python in Another Application:
Embedding Python is similar to
extending it, but not quite. The
difference is that when you extend
Python, the main program of the
application is still the Python
interpreter, while if you embed
Python, the main program may have
nothing to do with Python — instead,
some parts of the application
occasionally call the Python
interpreter to run some Python code.
I suggest that you first go through Embedding Python in Another Application. Then refer the following examples
Embedding Python in C/C++: Part I
Embedding Python in C/C++: Part II
Embedding Python in Multi-Threaded C/C++ Applications
If you like Boost.Python, you may visit the following links:
Embedding Python with Boost.Python Part 1
Boost has a python interface library which could help you.
Boost.Python
Interestingly, nobody has mentioned pybind11, yet. From their documentation:
pybind11 is a lightweight header-only library that exposes C++ types
in Python and vice versa, mainly to create Python bindings of existing
C++ code. Its goals and syntax are similar to the excellent
Boost.Python library by David Abrahams: to minimize boilerplate code
in traditional extension modules by inferring type information using
compile-time introspection. [...] Since its creation, this library has
grown beyond Boost.Python in many ways, leading to dramatically
simpler binding code in many common situations.
Concretely, calling into a Python function (called embedding) is as simple as this (taken from the documentation):
#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::print("Hello, World!"); // use the Python API
}
Use system call to run a python script from C++
#include<iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int result = system("/usr/bin/python3 testGen1.py 1");
cout << result;
}
Embeding the Python interpreter inside your C++ app will let you run Python programs using your application run Python scripts. It will also make it easier possible for those scripts to call C++ functions in your application. If this is what you want then the Boost library mentioned previously may be what you want to make it easier to create the link. In the past I have used SWIG to generate Python interfaces to C++ code. It was not clear from your question whether you wanted the Python scripts to call your C++ program or whether you just wanted the C++ to call Python.
Many of the Python functions use modules which are not built into the Python interpreter. If your Python scripts call these functions then you will either need to have your users install Python or include the python runtime files with your application. It will depend on what modules you import in you Python scripts.
Boost is probably the best choice, however if you're wanting something that's more standalone, and if this is for use with Windows (which seems feasible given that they are the people least likely to have Python installed), then you can use py2exe to create a DLL with entry points suitable for COM objects. You can then interface with the library via COM. (Obviously this is not at all useful as a cross-platform solution).
Using Inter Process Communication (IPC) over socket can be a possible solution. Use a local network socket to listen/trasfer commands between both.
Related
We have trained our models and tested them successfully using the provided Python scripts. However, we now want to deploy it on our website and run a web-service for the second round of tests.
Is there a C++ wrapper so that we can use to run/execute our models the same way we do with Python scripts?
I think the easiest way is to use cppflow. It is a C++ wrapper for the TensorFlow C API. It is simple but really easy to use and you do not need to install it neither compiling with Bazel. You just have to download the C API and use it like this:
Model model("graph.pb");
model.restore("path/to/checkpoint");
auto input = new Tensor(model, "input");
auto output = new Tensor(model, "output");
model.run(input, output);
You'll find code to run object detection on C++ here. You'll need an exported graph (.pb format), that you can get using the TF object detection API.
The compilation used to be tricky (except if you put your project in the tensorflow directory and compile everything with bazel, but you might not want to do that). I think it's supposed to be easier now, but I don't know how; or you can follow these instructions to compile tensorflow on its own and use it in a cmake project. You have another example of runing a graph in c++ here.
I am working on hand gesture recognition using computer vision for motion simulation. I do not have as good knowledge of python as i have of c++ and hence have programmed an opencv code in c++. Now i want this code to work in a blender.
Please tell me how can i integrate this code in blender.
Without altering blender's source code and compiling your own custom version, you will need to use an addon to use your code within blender. Blender uses python for it's addon system, each addon is a python module. You can use python's ctypes module to call compiled code from a python script.
While normally an addon is written in python it is possible to use or integrate a compiled C/C++ python module that can be used in blender. I'm not 100% sure if you can compile the module and add it to blender's addon folder or whether you need to have a folder with the library and a small python script that loads it.
You may want to look at cython, it takes python code and turns it into C/C++ code that can be compiled, this may give you a starting point to linking with your code. Have a look at CubeSurfer for an example of using cython for a blender addon.
For blender specific help you will find blender.stackexchange.com better.
I have a problem. I write a python script to make my work faster and now I want to share it with my team.
I don't want them to mess with some imports that are missing in the basic python installation. I know there is a way to compile python to exe, but I wonder if I can compile the code and the imports without messing with py2exe.
Does python have a built-in solution for that?
I saw that python have pyc compile option. Does it compile the import modules as well?
Thanks,
Or
No I don't believe you have a built-in standalone compilation mode native to Python. The pyc is a compiled code but not the kind you usually distribute as an executable program (meaning you would still need the Python interpreter).
If you don't want to use py2exe or other similar packages I advise you to use a portable version of Python with which you can distribute your software (see for example WinPython). The easiest way to accomplish this is by giving the portable distribution together with your code and perhaps a batch file (or similar, if you want to have a .exe alike behavior).
NOTE: You can provide the pyc compile code of the libraries you are using and putting them on the root of you software (or just stating where those imports should happen) but I predict this will give you problems in the future due to dependencies between different libraries. So, it's possible although I would hardly considerate it a good solution for what it seems to me you are trying to achieve.
Suppose I have a python module compiled from C++ called Foo (Foo.pyd). Can I call python from the .pyd module? Is it going to use the same interpreter that is running Foo.pyd, or it runs another interpreter?
Assuming I do not use boost python or swig, i.e., only native Python-C API is available.
I have a C++ program and I want to implement scripts on it. The desired scenario is, I have an executable of c++ code, it then calls at specific times a python script so it knows what to do through the embeded interpreter and the script then uses some form of API from the c++ program. This is where I ran into a problem. To expose c++ code to python you need to compile a DLL of the wrappers that you want and load it as a module inside python and that breaks my intention of python accessing the executable's functions.
Any way to resolve this problem without resorting to put so much pieces of c++ on a shared library?
What you want to do is to embed Python code into your application. There is an article on python.org on how to do that using raw CPython, but it's not that exhaustive when it comes to C++. A better bet might be to use Boost.Python or SWIG.