Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 353
Mquinteiro Avatar asked Oct 07 '17 11:10

Mquinteiro


1 Answers

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.

like image 145
Dalen Avatar answered Sep 27 '22 22:09

Dalen