Receive video from network with 100*100 then resize the video into 640*368

asked 2017-07-03 01:14:46 -0600

ansari gravatar image

Resizing 640368 dimensions video to 100100 using resize() in opencv 3.2 and sending 100100 data to client in network after receiving from server then I do resize into 640368 using resize function then, then video pixels has reduced. So how should I solve this ??

client.cpp

      #include "opencv2/opencv.hpp"
      #include <vector>

       using namespace std;
       using namespace cv;
       int main(int argc,char **argv)
      {
int clientsocket;
char* serverIP;
int serverPortid;
struct sockaddr_in clientaddr;
serverIP   = argv[1];
    serverPortid = atoi(argv[2]);
clientsocket=socket(AF_INET,SOCK_STREAM,0);
if(clientsocket==0)
{
    perror("socket failed");
    exit(0);
}
clientaddr.sin_family=AF_INET;
clientaddr.sin_addr.s_addr=inet_addr(serverIP);
clientaddr.sin_port=htons(serverPortid);
if(connect(clientsocket,(struct sockaddr *)&clientaddr,sizeof(clientaddr))<0)
{
    perror("connection failed");
    exit(0);
}

VideoWriter video("output.avi",CV_FOURCC('M','J','P','G'),25,Size(640,368),true);
Mat frame_mata(368,640,CV_8UC3, Scalar(0,255,255));

while(1)
{

    vector <unsigned char> vectData(30000);

    int framereceive = recv(clientsocket,vectData.data(),vectData.size(),MSG_WAITALL);

    Mat data_mata(100, 100, CV_8UC3, vectData.data());
    cout<<"frame receive :" << framereceive << endl;
    imshow("client view before resize", data_mata);
    resize(data_mata,frame_mata,frame_mata.size(),1,1,INTER_LINEAR);
    imshow("client view after resize", frame_mata);
    video.write(frame_mata);


    if (framereceive == 0)
    {
        break;
    }

    waitKey(1);
}
video.release();
close(clientsocket);
return 0;}

server.cpp

    #include "opencv2/opencv.hpp"

   using namespace cv;
   using namespace std;

    int main(int argc, char **argv)
    {
int portid=8080;
int serversocket, clientsocket;
struct sockaddr_in serveraddr, clientaddr;
int addrLen = sizeof(struct sockaddr_in);

serversocket = socket(AF_INET,SOCK_STREAM,0);
if(serversocket == 0)
{
    perror("socket failed");
    exit(0);
}

serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveraddr.sin_port=htons(portid);

if(bind(serversocket,(struct sockaddr *)&serveraddr,sizeof(serveraddr))<0)
{
    perror("bind failed");
    exit(0);
}
if(listen(serversocket,4)<0)
{
    perror("listen failed");
    exit(0);
}
cout <<  "Waiting for connections...\n"<<  "Server Port:" << portid << std::endl;
clientsocket = accept(serversocket,(struct sockaddr *)&clientaddr, (socklen_t*)&addrLen);
if(clientsocket<0)
{
    perror("server cannot accept");
    exit(0);
}

cout<<"server has connected" << endl;

VideoCapture serverCapture("/home/srmri/Downloads/videostream/SampleVideo_640x360_1mb.mp4");
int width = serverCapture.get(CV_CAP_PROP_FRAME_WIDTH);
int height = serverCapture.get(CV_CAP_PROP_FRAME_HEIGHT);
int totalframe = serverCapture.get(CAP_PROP_FRAME_COUNT);

cout << "Width : " << width << "Height : " << height <<"total Frame : " << totalframe<< endl;

//VideoWriter writer("serveroutput.avi",CV_FOURCC('M','J','P','G'),25,Size(width,height),true);
Mat frame;

 if (!serverCapture.isOpened())
 {
    perror("camera is already is in use");
    exit(0);
 }
int imgSize =0 ;
Mat frame_send(100,100,CV_8UC3);

while(1)
 {
    frame = Mat::zeros( height,width, CV_8UC3);
    serverCapture >> frame;
    resize(frame,frame_send,frame_send.size(),1, 1,INTER_LINEAR);

    waitKey(1);
    imgSize = frame_send.total() * frame_send.elemSize();
    if(waitKey(30)==27)
    {
        perror("esc key has pressed");
        exit(0);
    }



    if(imgSize == 0 || imgSize < 0)
    {
        break;
    }
    else
    {
        imshow("server side", frame);
        imshow("Resize server side ", frame_send );
        //writer.write(frame);
    }       
    cout<<"image size :" << imgSize <<endl;
    send(clientsocket,frame_send.data,imgSize,MSG_EOR);


 }
//writer.release();
serverCapture.release();

return 0;}
edit retag flag offensive close merge delete

Comments

" then video pixels has reduced" -- pardon ? can you try to explain ?

berak gravatar imageberak ( 2017-07-03 01:22:38 -0600 )edit

are you aware, that a tcp packet is limited to 64k, and might get fragmented even further in a "real world" network, so, while you can write it out in a single chunk on the server, you have to read small fragments on the client (say, 4k), until you got the complete payload

(this is entirely unrelated to opencv, btw, off-topic !)

berak gravatar imageberak ( 2017-07-03 01:26:52 -0600 )edit

resize(data_mata,frame_mata,frame_mata.size(),0,0,INTER_LINEAR);

berak gravatar imageberak ( 2017-07-03 01:31:05 -0600 )edit

Actually I am resizing original video (640368) into (100100) and sending this 100100 size video to client. After receiving the client with the size 100100 then i need to resize into original video(640*368). At that time my video is getting blur @berak

ansari gravatar imageansari ( 2017-07-03 01:36:00 -0600 )edit

Compressed bigger data in small size and transit that file from A to B system - Again decompressed that file to its original size in B system @berak

ansari gravatar imageansari ( 2017-07-03 04:19:09 -0600 )edit

Is there any function in opencv 3.2 to do this operation?? @berak

ansari gravatar imageansari ( 2017-07-03 04:22:08 -0600 )edit

no wonder, that a video resized from VGA to 100x100 and back looks "blurry". maybe you want to avoid all of the resizing, and rather compress your image to jpg (imencode()), to save some bandwidth ?

apart from that - i have no idea, what you're talking about, sorry.

berak gravatar imageberak ( 2017-07-03 04:32:51 -0600 )edit