how sent a live stream(UDP) of frames captured by opencv
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;
}
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);
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.
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??
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!
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??