2016-07-18 5 views
0

부모 프로세스에서 명명 된 파이프를 통해 자식 프로세스로 웹캠을 사용하여 비디오 프레임 스트림을 보내려고합니다. 부모는받은 프레임을 표시하는 동안 보낸 프레임을 표시합니다 .I UBuntu 14.04에서 비디오 프레임을 액세스하고 표시하기 위해 openCV 2.4.12를 사용하고 있습니다. 그러나 하나의 프레임 만 보내고 정지합니다. 문제의 원인을 파악할 수 없습니다.이 코드는 단일 이미지를 보내면 작동하지만 완벽하게 작동합니다. 스트림을 보낼 때 첫 번째 프레임. 나는 연속 스트림을 얻을 수있는 방법부모 프로세스에서 자식 프로세스로 웹캠 스트림

#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()'함수는 세 가지 반환 값을 가질 수 있습니다 : <0 : 오류가 발생했습니다 == 0 : 자식 프로세스> 0 부모 프로세스 .. 게시 된 코드가'fork()'의 반환 값을 올바르게 처리하지 못합니다. 가독성과 이해를 쉽게하기 위해 – user3629249

답변

1

waitKey(0);은 키를 누를 때까지 프로세스를 차단합니다. waitKey(1);으로 변경하면 >= 1 ms 이후에 자동으로 진행됩니다. 속도가 너무 빠르면 (예 : fps가 매우 높음) 다른 GUI 라이브러리 (예 : Qt)로 전환해야합니다.

관련 문제