簡體   English   中英

網絡攝像頭從父進程流到子進程

[英]Webcam stream from parent process to child process

我想使用攝像頭從父進程通過命名管道向子進程發送視頻幀流。父級顯示發送的幀,而子級顯示接收的幀。我正在使用openCV 2.4.12訪問和顯示視頻UBuntu 14.04上的幀,但是它只發送一個幀並凍結。我無法弄清楚是什么原因造成的。如果我發送單個圖像,此代碼可以很好地工作,但是當我嘗試發送流時,該代碼凍結在第一幀上。

這是代碼:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
using namespace cv;
using namespace std;


void  ChildProcess(void);                /* child process prototype  */
void  ParentProcess(void);               /* parent process prototype */

int  main()
 {

 pid_t  pid;

 pid = fork();
 if (pid == 0)
      ChildProcess();
 else
    ParentProcess();

 }

void  ChildProcess(void)
{

int fd2 = open("/home/eelab/vidpipe",O_RDONLY);

for(;;)
{
int rows = 480;
int cols = 640;
int nchan = 3;
int totalbytes = rows*cols*nchan;
int buflen = cols*nchan;
int ret;
//int fd1 = open("/dev/xillybus_read_32",O_RDONLY);

uchar buf[buflen];
uchar datarray[totalbytes];
Mat img(rows,cols,CV_8UC3);

int j;
int k = 0;
int num = totalbytes/buflen;
int bread = 0;
while(bread<totalbytes)
{

    ret=read(fd2,buf,buflen);
    for ( j = 0 ; j<= (ret-1);j++ )
    {
        datarray[j+k] = buf[j];

    }
        k = k+ret;
    bread = bread+ret;
}

img.data = datarray;

namedWindow( "Received image", WINDOW_AUTOSIZE );
imshow( "Received image", img );
waitKey(0);
}
close(fd2);
}

void  ParentProcess(void)
{

int check;
int fd;
int totalbytes;
int buflen;
int count = 0;
fd = open("/home/eelab/vidpipe",O_WRONLY);
if (fd < 1)
{
    perror("open error");
}

VideoCapture cap(0);

for(;;)
{
    Mat frame;
    cap >> frame;


totalbytes = frame.total()*frame.elemSize();
buflen = (frame.cols*3);

uchar *framepointer = frame.data;



 int bwritten = 0;
 int ret;
 uchar* buf;
 buf = framepointer;
 int num = totalbytes/buflen;
 while(bwritten<totalbytes)
 {
   ret = write(fd,buf,buflen);
   write(fd,NULL,0);
   buf = buf + ret;
   bwritten = bwritten+ret;
  }



    namedWindow( "Sent image", WINDOW_AUTOSIZE );
    imshow( "Sent image", frame );
    waitKey(0);

  }

  close(fd);

  }    

請幫忙,如何獲得連續播放?

waitKey(0); 將阻止該過程,直到按下某個鍵為止。 如果更改為waitKey(1); 它會在>= 1 ms之后自動進行。 如果速度不夠快(例如fps很高),則應切換到其他GUI庫(例如Qt)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM