12

Scenario:

A C++ program process captures an image using OpenCV. Another process, using Python and OpenCV has a shared memory area (with mmap) with the first program.

Problem:

How do I create in a Python process, a reference to the same image that already exists in the shared memory area? This part in the Python process could be written as a C module and imported into Python.

For my specific needs, only the C++ process creates and writes data, while the Python process just reads (and processes upon) it.

Extra info:

Given the same image, the data field of C++ cv::Mat and numpy.array are equal (same size and content). So there is no need to convert.

As the languages have separated memory managers, it may have to use some external sync (eg. semaphore) to avoid one process using a deallocated area from another.

My problem is creating in Python numpy.array object that the data fields point to the specific area in the shared memory.

1 Answer 1

1

below options can be used to create cv::Mat or numpy.array header for a preallocated memory buffer:

  • For cv::Mat, there is a constructor which creates a cv::Mat header over preallocated memory (destructor of Mat will not release the memory in this case):

    cv::Mat(int rows, int cols, int type, void * data, size_t step = AUTO_STEP)

  • numpy array from buffer:

    numpy.frombuffer(buffer, dtype=float, count=- 1, offset=0, *, like=None)

Sign up to request clarification or add additional context in comments.

2 Comments

Accepting the answer because it seems right, but I don't do OpenCV anymore and I can't verify it. So doing for the appreciation of the effort to answer. If I it's wrong and someone point it, I'll change it.
@AllanDeamon the np.frombuffer aspect of the answer was just removed by the author -- this really seems to be an "ipc" question. surely you wanted to know when a new frame is ready, besides getting the data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.