我想使用网络摄像头从父进程发送视频帧流到子进程通过命名管道。父节点显示发送的帧,而子节点显示接收到的帧。我正在使用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);
}
请帮助,我该如何获得连续的流?
答案 0 :(得分:1)
waitKey(0);
将阻止该过程,直到按下某个键。如果您更改为waitKey(1);
,则会在>= 1 ms
后自动进行。如果它不够快(例如你的fps非常高),你应该切换到另一个GUI库(例如Qt)。