Python和C++程序基于共享内存实现图像数据传输

引言

共享内存是一种多进程间高效通信的方式,相比于其他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;
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橘色的喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值