繁体   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