Ask Your Question
0

how sent a live stream(UDP) of frames captured by opencv

asked 2013-10-06 09:04:29 -0600

naeeem gravatar image

updated 2013-10-08 11:05:13 -0600

i created these programs as client and server for broadcasting(UDP) of frames. i got error as segmentation error while running client. code for client

//basic
#include <iostream>
#include <stdio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
//opencv libraries
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
//socket libraries
#include<sys/types.h> 
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#define SERVERPORT "4950"   // the port users will be connecting to


using namespace std;
using namespace cv;

void detectAndDisplay( Mat frame , Mat frame_edit);// function for detecting face
int talker(Mat frame);
/** Global variables */
String face_cascade_name = "lbpcascade_frontalface.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;


/** @function main */
int main( void ) 
{
//body for image capture from camera 
 .
 .
 .
    //calling face detect function
detectAndDisplay( frame_edit, frame );
    int c = waitKey(5);
    if( (char)c == 27 ) { break; } // escape
}
return 0;
}

/**
 * @function detectAndDisplay
 */
 void detectAndDisplay( Mat frame ,Mat frame_edit)
{
// function body for detecting face
    .

imshow( "face detection client", frame );

    //calling talker to sent the data..
int ret=talker(frame);

if(ret==1||ret==2)
{
    cout<<"error";
    exit(1);
}   
}

int talker(Mat frame)

{
int sockfd;
const char *argv[1]={"localhost"};
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
frame = (frame.reshape(0,1)); // to make it continuous
int  imgSize = frame.total()*frame.elemSize();  
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) 
{
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
}
    // loop through all the results and make a socket

for(p = servinfo; p != NULL; p = p->ai_next) 
{
    if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) 
    {
        perror("talker: socket");
        continue;
    }
break;
}
if (p == NULL)
{
    fprintf(stderr, "talker: failed to bind socket\n");
    return 2;
}
if ((numbytes = sendto(sockfd, frame.data, imgSize, 0,p->ai_addr, p->ai_addrlen)) == -1)
{
    perror("talker: sendto");
    exit(1);
}
freeaddrinfo(servinfo);
cout<<"senting frame succesfull"<<endl;
close(sockfd);
return 0;
}
edit retag flag offensive close merge delete

Comments

hi, can you please cut down the code to the essential? (the whole face-detect cruft is not nessecary here)

also, you don't want to loop though all possible protocols, and take the 1st one that works,but specify a distinct one, like socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

berak gravatar imageberak ( 2013-10-08 10:37:25 -0600 )edit

besides, sending raw images over udp might be a bad idea (packet fracturing, loss)

remember, that you can only stuff 64k into one packet!

tcp would at least fracture it automatically, and write several packets.

berak gravatar imageberak ( 2013-10-08 10:42:18 -0600 )edit

so what do you suggest...i got programs that sent frames captured using v4l2...besides that program can be easily obtained using vlc on server...i want a similar program but with opencv...will it be efficient with tcp ? i heard it causes more delay in frames..is there a way to sent frames in more efficient and faster in UDP??

naeeem gravatar imagenaeeem ( 2013-10-08 11:08:54 -0600 )edit

if you must do your own networking, at least start with tcp, get that to run, later improve. handling udp correctly is much more difficult than tcp, since you seem to be an absolute noob to network progging, avoid it at all costs!

oh, and yes, outsourcing all of it to gstreamer/libvlc or such is the better idea!

berak gravatar imageberak ( 2013-10-08 11:25:33 -0600 )edit

well you are correct about me because to your information i started working on networking since 1 month..and i am completely exhuasted since i dont have anyone for guiding me..other than opecv and stack overflow forums..whatever... can we link gstreamer and opencv??

naeeem gravatar imagenaeeem ( 2013-10-08 11:38:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-10-07 01:43:37 -0600

Haris gravatar image

updated 2013-10-07 01:46:06 -0600

Get Mat.data and directly send to the socket, the data format is like BGR BGR BGR.... On the receiving side you should know the size of image you are going to receive. After receiving just assign the received buffer(BGR BGR... array) to a Mat of size you already know.

Client:-

Mat frame;
frame = (frame.reshape(0,1)); // to make it continuous

int  imgSize = frame.total()*frame.elemSize();

// Send data here
bytes = send(clientSock, frame.data, imgSize, 0))

Server:-

   Mat  img = Mat::zeros( height,width, CV_8UC3);
   int  imgSize = img.total()*img.elemSize();
   uchar sockData[imgSize];

 //Receive data here

   for (int i = 0; i < imgSize; i += bytes) {
   if ((bytes = recv(connectSock, sockData +i, imgSize  - i, 0)) == -1) {
     quit("recv failed", 1);
    }
   }

 // Assign pixel value to img

 int ptr=0;
 for (int i = 0;  i < img.rows; i++) {
  for (int j = 0; j < img.cols; j++) {                                     
   img.at<cv::Vec3b>(i,j) = cv::Vec3b(sockData[ptr+ 0],sockData[ptr+1],sockData[ptr+2]);
   ptr=ptr+3;
   }
  }
edit flag offensive delete link more

Comments

i have edited program with your recommendations still there is error..one more thing personel i am also a malayalee from guruvayur...:)

naeeem gravatar imagenaeeem ( 2013-10-08 10:17:22 -0600 )edit

Hi you can try this link http://code.google.com/p/sable-netcv/ which send and receives opencv image over network using tcp. And I am from kannur....

Haris gravatar imageHaris ( 2013-10-09 00:11:36 -0600 )edit

that solved a major issue for me...can i get ur personal mail id?

naeeem gravatar imagenaeeem ( 2013-10-16 10:21:47 -0600 )edit

No encoding at all?

Francesco Boi gravatar imageFrancesco Boi ( 2019-02-02 12:18:59 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-10-06 09:04:29 -0600

Seen: 26,090 times

Last updated: Oct 08 '13