简体   繁体   中英

Share video data between C++ and Python

I have a USB camera (uEye) which has a C++ interface allowing you to configure some features of the camera. The C++ program can read the image data from the camera and store it somewhere in pre-allocated memory. All of this runs under Windows.

Python with numpy gives me a simple environment to manipulate images and spend some quality time working on my processing algorithms.

What I would like to do is:

  1. Use the c++ program to configure the camera and obtain images (at video rate),
  2. Pass the data to Python
  3. Process the data in Python

I am under the impression that I do not want to embed C++ in Python or Python in C++, as I prefer to have two stand-alone systems (so I can use the camera without the Python stuff, or use the Python stuff without the camera).

What I can find so far are methods to share some data using pipes, sockets, or mapped memory, though it appears to be restricted to small amounts of data or strings. What I can not find, however, is an indication if this is fast enough and something that I should attempt to implement. Should I want to do this?

If this is a bad idea, what would be a better alternative? Embed the Python code in C++ or vice versa? Or ditch Python all together because the savings in development time there do not offset the additional effort in getting the interprocess communication to work?

There was a recent post on the PyPy blog about real-time video processing. In the example they use mplayer to be grab and display video, which might be preferable to trying to interface with your C++ program (assuming it works with your webcam). If not, thinking along those lines a simple solution is to just connect stdout/stdin of your two applications. Also probably a good idea to look at PyPy for video processing.

Since you say the device has a "C++ interface", I assume it provides a header file + DLL which you can link to and control the device via an API. In such a case, the fastest approach would be to wrap this API in Python (using Swig or other C++-to-Python API tools). This will provide a very low overhead of just a couple of procedure calls, passing the data directly as pointers to memory.

If you don't want to "marry" Python, write yourself a controlling app in C++ too, but I think the fastest and most convenient way of connecting the API to Python is the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM