简体   繁体   English

在 C/C++ 和 Python 之间共享内存

[英]Share memory between C/C++ and Python

Is there a way to share memory to share an openCV image (MAT in C+++ and numpy in python) image between a C/C++ and python?有没有办法共享内存以在 C/C++ 和 python 之间共享 openCV 图像(C+++ 中的 MAT 和 python 中的 numpy)图像? Multiplataform is not needed, I'm doing it in linux, I've thought share between mmap or similar think.不需要多平台,我在 linux 中做,我认为 mmap 或类似想法之间的共享。

I have two running processes one is written in C and the other is python, and I need to share an image between them.我有两个正在运行的进程,一个是用 C 编写的,另一个是 python,我需要在它们之间共享一个图像。

I will call from the c process to python via socket but I need to send and image and via memory.我将通过套接字从 c 进程调用 python,但我需要通过内存发送和图像。

Another alternative could be write in memory file, not sure if it could be more time consuming.另一种选择可能是写入内存文件,不确定是否会更耗时。

OK, this is not exactly a memory sharing in its real sense. 好的,这并不是真正意义上的内存共享。 What you want is IPC to send image data from one process to another. IPC想要将图像数据从一个进程发送到另一个进程。

I suggestthat you use Unix named pipes. 我建议您使用Unix命名管道。 You will have to get the raw data in a string format in C/C++, send it through pipe or Unix socket to Python and there get a numpy array from the sent data. 您将必须以C / C ++的字符串格式获取原始数据,通过管道或Unix套接字将其发送到Python,然后从发送的数据中获取一个numpy数组。 Perhaps using np.fromstring() function. 也许使用np.fromstring()函数。

Do not worry about the speed, pipes are pretty fast. 不用担心速度,管道非常快。 Local and Unix sockets as well. 本地和Unix套接字也是如此。 Most time will be lost on getting the string representation and turning it back to matrix. 大多数时间将浪费在获取字符串表示形式并将其返回矩阵上。

There is a possibility that you can create real shared memory space and get the data from OpenCV in C/C++ directly into Python, and then use OpenCV in Python to get out numpy array, but it would be complicated. 您可能会创建实际的共享内存空间,并将C / C ++中的OpenCV中的数据直接导入Python,然后在Python中使用OpenCV来获取numpy数组,但这很复杂。 If you don't need speed of light your best bet are named pipes. 如果您不需要光速,最好的选择就是管道。

It's not exactly what you're asking for, but you can use an in-memory database such as Redis as a conduit for exchanging OpenCV images between your programs.这并不完全是您所要求的,但是您可以使用诸如 Redis 之类的内存数据库作为在程序之间交换 OpenCV 图像的管道。 Though less direct than raw memory mapping, and while it introduces an additional application layer, the data is still manipulated strictly in RAM.虽然不如原始内存映射那么直接,并且虽然它引入了一个额外的应用层,但数据仍然严格地在 RAM 中操作。 In my experience, this tactic is fast enough to be near-realtime on a modern machine.根据我的经验,这种策略足够快,可以在现代机器上接近实时。

I've used such architecture for https://github.com/vmlaker/hello-websocket , albeit it uses Python at both ends.我已经将这种架构用于https://github.com/vmlaker/hello-websocket ,尽管它在两端都使用 Python。

Here's a minimalistic example of a similar protocol implementing the source in C++, and Python for the target application.下面是一个使用 C++ 实现源代码的类似协议的简约示例,以及用于目标应用程序的 Python。 The following C++ program reads an image from a file on disk using OpenCV, and stores the image in Redis under the key image :以下 C++ 程序使用 OpenCV 从磁盘文件中读取图像,并将图像存储在 Redis 中的 key image

#include <opencv4/opencv2/opencv.hpp>
#include <cpp_redis/cpp_redis>

int main(int argc, char** argv)
{
  cv::Mat image = cv::imread("input.jpg");
  std::vector<uchar> buf;
  cv::imencode(".jpg", image, buf);

  cpp_redis::client client;
  client.connect();
  client.set("image", {buf.begin(), buf.end()});
  client.sync_commit();
}

Then in Python you grab the image from the database and do what you will:然后在 Python 中,您从数据库中获取图像并执行您将执行的操作:

import cv2
import numpy as np
import redis

store = redis.Redis()
image = store.get('image')
array = np.frombuffer(image, np.uint8)
decoded = cv2.imdecode(array, flags=1)
cv2.imshow('hello', decoded)
cv2.waitKey()

Going the other way is pretty straightforward.走另一条路非常简单。 You can get cpp_redis here: https://github.com/cpp-redis/cpp_redis .您可以在此处获取cpp_redishttps : //github.com/cpp-redis/cpp_redis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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