2016-07-18 12 views
0

親プロセスから名前付きパイプを介して子プロセスにWebカメラを使用してビデオフレームのストリームを送信します。親は、受信したフレームを子が表示している間に送信されたフレームを表示します。 UBuntu 14.04でビデオフレームにアクセスして表示するためにopenCV 2.4.12を使用しています。しかし、1つのフレームしか送信せず、フリーズしています。問題の原因を突き止めることはできません。ストリームを送信しようとすると、最初のフレーム。ここで親プロセスから子プロセスへのWebcamストリーム

コードです:

#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

あなたは 'waitKey(0);'を 'waitKey(1);'に変更して、続行するのに必要なキー押しがないようにしましたか? – Micka

+1

これはうまくいく!どうもありがとう。 – userXktape

+0

'fork()'関数は3つの返り値を持ちます:<0:エラーが発生しました== 0:子プロセス> 0親プロセス.. ''ポストされたコードは 'fork()'の戻り値を正しく処理していません。読みやすさと理解を容易にするために – user3629249

答えて

1

は、キーが押されるまでプロセスをブロックします。 waitKey(1);に変更すると、>= 1 msの後に自動的に進みます。速度が十分でない場合(たとえば、fpsが非常に高い場合)、別のGUIライブラリ(たとえばQtなど)に切り替える必要があります。

関連する問題