2

I have an executable (face recognition) written by one of our programmers who's no longer with us. It takes a commandline argument and prints the result to STDOUT like this.

I need to be able to use this in Python but performance is important so I'm not sure if I should just use subprocess. I need to capture the output of the program so is it a better idea to modify the code to make it a C++ library instead and write a wrapper for it in Python? How would that impact performance compared to using subprocess?

I have looked into the Python docs for running C code and found this but the answer here is different. The second method does not even use Python.h so I'm a little confused. What's the best way to do what I need when performance is important?

Community
  • 1
  • 1
Crypto
  • 1,217
  • 3
  • 17
  • 33
  • 1
    Why the downvote and why is the C++ tag removed when that's exactly what I'm asking? – Crypto Jan 30 '14 at 02:09
  • Executable or library imported function, decide! Removed [tag:c++] tag didn't downvote so far ...) – πάντα ῥεῖ Jan 30 '14 at 02:09
  • 2
    @πάνταῥεῖ: Yes, Crypto needs to decide, but that's no reason for a downvote. I don't think your edit or re-tag was warranted in this case. – Daniel Pryden Jan 30 '14 at 02:11
  • @DanielPryden I didn't downvote, and your edit, (re-)adding [tag:c++] is correct, sorry ... – πάντα ῥεῖ Jan 30 '14 at 02:14
  • 1
    @Crypto: If your question is "How can I call some arbitrary C++ code from Python", then it's an exact duplicate of [the question you linked to](http://stackoverflow.com/questions/145270/calling-c-c-from-python), which means it should be closed as a duplicate. If your question is "How can I execute a C++ program from Python?" then the answer is probably to use the `subprocess` module. πάντα ῥεῖ is right that it doesn't really make sense to ask both at once -- perhaps what you're really asking is "when should I link in a C++ library instead of using a subprocess"? – Daniel Pryden Jan 30 '14 at 02:14
  • @Daniel: "when should I link in a C++ library instead of using a subprocess" I think is what I meant to ask. I don't know if it's right to assume that the C++ library will be faster. – Crypto Jan 30 '14 at 02:17
  • @Crypto, what does the app prints into STDOUT? I mean, if that's nothing special but some text, I would suggest you to convert your EXE to a DLL and call DLL functions from Python. If this is a case and possible for you, let me know so I'll explain it in full detail. As you know subprocess will be really slow and I wouldn't suggest it for a serious project. – 72DFBF5B A0DF5BE9 Jan 30 '14 at 02:47
  • The input is the path to an image file. The output is just text. The target system is GNU/Linux. – Crypto Jan 30 '14 at 02:51
  • @Crypto, perfect. Can you convert current C++ code to a library? Are you familiar with C++ and libraries etc. ? – 72DFBF5B A0DF5BE9 Jan 30 '14 at 02:54
  • I managed to fix some bugs in it but even then my C++ knowledge is very limited but I might be able to make it a library if I try hard. – Crypto Jan 30 '14 at 03:01

1 Answers1

2

At first I have to tell you I think subprocess method and running external application and reading output from STD output would be slow and unreliable. I won't recommend that method.

Instead, here is some suggestions:

  • You can create a Python extension in C or C++, you can find a lot of resources online about it. So you can convert your current code to Python extension format and just use Python extension skeleton template, integrate your functions and code in it and you'll have a python extension, simply you'll be able to call it's functions from Python code. See: http://docs.python.org/2/extending/index.html

  • Convert your current C++ code directly into a DLL and exports functions of your code you want to call from python if you are in Windows or simply convert it to a shared library if you are using Linux. It will be compiled to .so . Then you can call functions as an external library in your Python code. Example with explanation: http://www.linuxforu.com/2010/05/extending-python-via-shared-libraries/

I've never tried them, but it seems these projects can help you with what you want:

72DFBF5B A0DF5BE9
  • 4,954
  • 3
  • 21
  • 24