仓库地址: https://gitee.com/liudegui/cpp_py_shmbuf_sample

在Python和C++程序见高效图像数据1080p image transfer sample between cpp and python3 app (based on shared memory)

  • works fine in linux platform.
  • cpp-app read 1080p video stream,write image data to the shared memory.
  • python3.8±app read image data from the shared memory and show video image.
  • cpp-app use boost-ipc and opencv, python-app need 3.8+ version, and pip install opencv-python.

在这里插入图片描述

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;
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐