简体   繁体   中英

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? Multiplataform is not needed, I'm doing it in linux, I've thought share between mmap or similar think.

I have two running processes one is written in C and the other is python, and I need to share an image between them.

I will call from the c process to python via socket but I need to send and image and via memory.

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.

I suggestthat you use Unix named pipes. 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. Perhaps using np.fromstring() function.

Do not worry about the speed, pipes are pretty fast. Local and Unix sockets as well. 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. 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. Though less direct than raw memory mapping, and while it introduces an additional application layer, the data is still manipulated strictly in 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.

Here's a minimalistic example of a similar protocol implementing the source in C++, and Python for the target application. The following C++ program reads an image from a file on disk using OpenCV, and stores the image in Redis under the 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:

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 .

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