在 C++ 之间共享视频数据和Python
我有一个 USB 摄像头 (uEye),它具有 C++ 接口,允许您配置摄像头的某些功能。 C++ 程序可以从相机读取图像数据并将其存储在预先分配的内存中的某个位置。所有这些都在 Windows 下运行。
Python 与 numpy 为我提供了一个简单的环境来操作图像,并花一些宝贵的时间研究我的处理算法。
我想做的是:
- 使用 C++ 程序配置相机并获取图像(以视频速率),
- 将数据传递给 Python
- 在 Python 中处理数据
我的印象是我不想在 Python 中嵌入 C++或 C++ 中的 Python,因为我更喜欢有两个独立的系统(这样我可以在没有 Python 的情况下使用相机,或者在没有相机的情况下使用 Python 的东西)。
到目前为止,我能找到的是使用管道、套接字或映射内存来共享一些数据的方法,尽管它似乎仅限于少量数据或字符串。然而,我找不到的是这是否足够快以及我应该尝试实现的指示。我应该这样做吗?
如果这是一个坏主意,那么更好的选择是什么?将 Python 代码嵌入到 C++ 中还是反之亦然?或者完全放弃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:
- Use the c++ program to configure the camera and obtain images (at video rate),
- Pass the data to Python
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PyPy 博客上有一篇关于实时视频处理的最近的帖子。在示例中,他们使用 mplayer 来抓取和显示视频,这可能比尝试与您的 C++ 程序交互(假设它与您的网络摄像头配合使用)更好。如果没有,按照这些思路思考一个简单的解决方案是仅连接两个应用程序的标准输出/标准输入。使用 PyPy 进行视频处理可能也是个好主意。
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.
既然你说设备有一个“C++ 接口”,我假设它提供了一个头文件 + DLL,你可以通过 API 链接到并控制设备。在这种情况下,最快的方法是用 Python 封装此 API(使用 Swig 或其他 C++ 到 Python API 工具)。这将提供非常低的开销,只需几个过程调用,将数据直接作为内存指针传递。
如果你不想“嫁给”Python,也可以用 C++ 自己编写一个控制应用程序,但我认为将 API 连接到 Python 的最快、最方便的方法就是上面的方法。
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.