引言
共享内存是一种多进程间高效通信的方式,相比于其他IPC(Inter-Process Communication)方式,如管道或消息队列,共享内存可以避免数据的复制,提升数据交换的速度和效率。
Python通过multiprocessing模块和第三方库支持共享内存的使用,而C++则可以利用标准库或第三方库如Boost.Interprocess实现共享内存通信。
Python和C++程序实现高效图像数据传输
- 在Linux平台上表现良好。
- C++应用程序读取1080p视频流,将图像数据写入共享内存。
- Python 3.8+应用程序从共享内存中读取图像数据并显示视频图像。
- C++应用程序使用Boost IPC和OpenCV,Python应用程序需要3.8以上版本,并且需要使用
pip install opencv-python
安装OpenCV。 - 仓库地址: cpp_py_shmbuf_sample
Python端
import time
import multiprocessing as mp
from shm_buf import SharedMemoryBuffer
import numpy as np
import cv2
'''
Suppose the video is 3-channel 1080p
'''
WIDTH = 1920
HEIGHT = 1080
CHANNELS = 3
def consumer():
shm_name = "shm_name_test"
shm_buf = SharedMemoryBuffer(shm_name)
while True:
if not shm_buf.readable():
time.sleep(0.1)
continue
bytes_data = shm_buf.read_shm()
if bytes_data and len(bytes_data) == HEIGHT*WIDTH*CHANNELS:
img = np.frombuffer(bytes_data, dtype=np.uint8)
img = img.reshape((HEIGHT, WIDTH, CHANNELS))
cv2.imshow("img", img)
cv2.waitKey(1)
if __name__ == "__main__":
consume_proc = mp.Process(target=consumer, args=(), daemon=True)
consume_proc.start()
consume_proc.join()
C++端
#include <iostream>
#include <string>
#include "shm_buf.h"
#include <unistd.h>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// Suppose the video is 3-channel 1080p
static const uint32_t WIDTH = 1920;
static const uint32_t HEIGHT = 1080;
static const uint32_t CHANNELS = 3;
void producer()
{
const char *shm_name = "shm_name_test";
//SharedMemoryBuffer::remove_shm(shm_name);
SharedMemoryBuffer shmbuf(shm_name, WIDTH*HEIGHT*CHANNELS*100 + 12);
int64 t0 = cv::getTickCount();;
int64 t1 = 0;
string fps;
int nFrames = 0;
cv::Mat img_original = cv::imread("../../1080p.jpg");
while(true) {
char buff[255] = {0};
sprintf(buff, "frame idx:%d", nFrames);
cv::Mat frame = img_original.clone();
cv::putText(frame, buff, cv::Point(0, 50), cv::FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, cv::Scalar(255, 0, 0));
nFrames++;
if (!frame.empty())
{
if (nFrames % 100 == 0)
{
const int N = 100;
int64 t1 = cv::getTickCount();
fps = " Send FPS:" + to_string((double)getTickFrequency() * N / (t1 - t0)) + "fps";
t0 = t1;
}
cv::putText(frame, fps, Point(100, 100), cv::FONT_HERSHEY_COMPLEX, 2, cv::Scalar(255, 255, 255),1);
}
auto fsize = frame.cols * frame.rows * frame.channels();
shmbuf.write_shm(frame.data, fsize);
if ((waitKey(1) & 0xFF) == 'q')
break;
}
}
int main(int argc, char *argv[])
{
producer();
return 0;
}