Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.76 KB | None | 0 0
  1.  
  2.  
  3.     // OpenCVTest.cpp : Defines the entry point for the console application.
  4.     //
  5.  
  6.     #ifdef WIN32
  7.     #include <windows.h>
  8.     #include <winsock2.h>
  9.     #else
  10.     #include <sys/socket.h>
  11.     #include <netinet/in.h>
  12.     #include <netdb.h>
  13.     #endif
  14.  
  15.     //#include "opencv2/opencv.hpp"
  16.     #include "opencv2/core/core.hpp"
  17.     #include "opencv2/highgui/highgui.hpp"
  18.     #include "opencv2/imgproc/imgproc.hpp"
  19.  
  20.     #include <stdio.h>
  21.     #include <unistd.h>
  22.     #include <sys/types.h>
  23.     #include <strings.h>
  24.     #include <stdlib.h>
  25.     #include <iostream>
  26.     #include <time.h>
  27.     #include <iostream>
  28.     #include <ctime>
  29.     #include <fstream>
  30.     #include <sys/time.h>
  31.     #include <stdio.h>
  32.  
  33.     using namespace cv;
  34.     using namespace std;
  35.  
  36.     int main(int argc, char* argv[]) {
  37.     #ifdef __WIN32__
  38.             WORD versionWanted = MAKEWORD(1, 1);
  39.             WSADATA wsaData;
  40.             WSAStartup(versionWanted, &wsaData);
  41.     #endif
  42.  
  43.             //Open File
  44.               ofstream timeFile; //sent times
  45.               timeFile.open("time.txt");
  46.  
  47.             int sock_descriptor;
  48.             struct sockaddr_in serv_addr;
  49.             struct hostent *server;
  50.             sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
  51.             if (sock_descriptor < 0)
  52.                     printf("Failed creating socket\n");
  53.  
  54.             memset((char *) &serv_addr, 0, sizeof(serv_addr));
  55.             server = gethostbyname("127.0.0.1");
  56.  
  57.             if (server == NULL) {
  58.                     printf("Failed finding server name\n");
  59.                     return -1;
  60.             }
  61.  
  62.             serv_addr.sin_family = AF_INET;
  63.             memcpy((char *) &(serv_addr.sin_addr.s_addr), (char *) (server->h_addr), server->h_length);
  64.             serv_addr.sin_port = htons(64545);
  65.             if (connect(sock_descriptor, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  66.             {
  67.                     printf("Failed to connect to server\n");
  68.                     return -1;
  69.             }
  70.  
  71.             else
  72.                     printf("Connected successfully\n");
  73.  
  74.             VideoCapture cap(0); // open the video camera no. 0
  75.  
  76.             if (!cap.isOpened())  // if not success, exit program
  77.             {
  78.                     cout << "Cannot open the video cam" << endl;
  79.                     return -1;
  80.             }
  81.  
  82.  
  83.  
  84.             double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
  85.             double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
  86.  
  87.             cout << "Frame size : " << dWidth << " x " << dHeight << endl;
  88.  
  89.             bool first = true;
  90.             int timer = 0, newpos = 0, oldpos = 0;
  91.             Mat currentFrame, currentFrameGrey, differenceFrame, oldFrameGrey, temp;
  92.             clock_t t0 = clock();
  93.             time_t rawtime;
  94.             struct tm * ptm;
  95.             int frames = 0;
  96.             clock_t t1, t2;
  97.         long index=0;
  98.         char posArray[1];//create posArray
  99.         posArray[0]='5';
  100.             while (1)
  101.             {
  102.                     clock_t now = clock() - t0;
  103.                     if ((now * 1000.0) / CLOCKS_PER_SEC >= 1000)
  104.                     {
  105.                             time(&rawtime);
  106.                             ptm = gmtime(&rawtime);
  107.                             cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec
  108.                                             << "  FPS: " << frames << endl;
  109.                             frames = 0;
  110.                             t0 = clock();
  111.                     }
  112.                     frames++;
  113.                     IplImage* oldImageGrey;
  114.                     bool bSuccess;
  115.  
  116.                     bSuccess = cap.read(currentFrame);
  117.  
  118.                     if (!bSuccess) //if not success, break loop
  119.                     {
  120.                             cout << "Cannot read a frame from video stream" << endl;
  121.                             break;
  122.                     }
  123.  
  124.                     cvtColor(currentFrame, currentFrameGrey, CV_BGR2GRAY);
  125.                     if (first) {
  126.                             convertScaleAbs(currentFrameGrey, oldFrameGrey, 1.0, 0.0);
  127.                             first = false;
  128.                             imshow("Original Pic", currentFrame);
  129.                             continue;
  130.                     }
  131.                     absdiff(currentFrameGrey, oldFrameGrey, differenceFrame);
  132.                     blur(differenceFrame, differenceFrame, Size(3, 3));
  133.                     threshold(differenceFrame, differenceFrame, 25, 255, CV_THRESH_BINARY);
  134.  
  135.                     imshow("Live Web Cam", currentFrame);
  136.                     imshow("Difference Frame", differenceFrame);
  137.                     Moments moment = moments(differenceFrame, 1);
  138.                     double mom10 = moment.m10;
  139.                     double mom01 = moment.m01;
  140.                     double area = moment.m00;
  141.                     int posX = (int) (mom10 / area);
  142.                     int posY = (int) (mom01 / area);
  143.                     int pos = 0;
  144.                     int x = floor(posX / 212.0);
  145.                     int y = floor(posY / 163.0);
  146.  
  147.                     pos = x + 3 * y;
  148.                     pos++;
  149.  
  150.                     newpos = pos;
  151.                     if (oldpos == newpos) {
  152.                             if (timer == 0)
  153.                                     t1 = clock();
  154.                             timer++;
  155.                             if (timer == 2) {
  156.                                     t2 = (clock() - t1) * 1000 / CLOCKS_PER_SEC;
  157.                                     time(&rawtime);
  158.                                     ptm = gmtime(&rawtime);
  159.                                     cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec
  160.                                                     << "  latency: " << t2 << endl;
  161.  
  162.                                     //update the index value
  163.                                     index++;
  164.                                     char indexString[10]={};
  165.                             //      std::string indexString;//create string
  166.                             //      std::stringstream str;//create new stringstream
  167.                             //      str<<index;//put index in the stringstream
  168.                             //      str>>indexString;//output the stringstream in indexString
  169.                                     sprintf(indexString,"%ld",index);
  170.                                     //GET TIME IN MILISECONDS
  171.                                      struct timeval sysTime; //get time of system in seconds since (1/1/1970)
  172.                              gettimeofday(&sysTime,NULL);
  173.                                      int milli=sysTime.tv_usec/1000; //get miliseconds by dividing micro by 1000
  174.                                  time_t timeSeconds=sysTime.tv_sec; //get seconds
  175.                                  long time_mili=static_cast <long> (timeSeconds); //cast from time_t to long
  176.                                 // time_mili=time_mili*1000;
  177.                                 // time_mili+=milli;
  178.                                  //from long to string
  179.                                  char number1[10], number2[3],number[13];
  180.                                  sprintf(number1,"%ld",time_mili);
  181.                                  sprintf(number2,"%d",milli);
  182.                                  timeFile<<"Number 1 : "<<number1<<" Number2(1) : "<<number2<<endl;
  183.                                  timeFile<<"Number2[0]: "<<number2[0]<<"Number2[1]: "<<number2[1]<<"Number2[2]: "<<number2[2]<<endl;
  184.                                  if(number2[1]==NULL)
  185.                                  {
  186.                                      timeFile<<"Here1"<<endl;
  187.                                      char c0=number2[0];
  188.                                      number2[0]='0';
  189.                                  number2[1]='0';
  190.                                      number2[2]=c0;
  191.                                  }
  192.                                  else if(number2[2]==NULL)
  193.                                  {
  194.                       timeFile<<"Here"<<endl;
  195.                                      char c0=number2[0];
  196.                                      char c1=number2[1];
  197.                                      number2[0]='0';
  198.                                      number2[1]=c0;
  199.                                      number2[2]=c1;
  200.                                  }
  201.                                  timeFile<<"Number 2 after change : "<<number2<<endl;
  202.                                  for(int i=0;i<10;i++)
  203.                                      number[i]=number1[i];
  204.                                  int dd=0;
  205.                                  for(int i=10;i<13;i++)
  206.                                  {
  207.                                      number[i]=number2[dd];
  208.                                      dd++;
  209.                                  }
  210.                                  //timeFile<<"Number : "<<number<<endl;
  211.  
  212.  
  213.                                  cout << "timemilli,number" <<number1 <<","<<number2<< "," << number << endl ;
  214.                                  //create Buffer
  215.                                  int bSize=26; //(strlen(indexString)
  216.                                  cout<<"index is"<< indexString<< endl;
  217.                                  //timeFile<<"Index is "<<indexString<<endl;
  218.                                  char buff[bSize];
  219.  
  220.                                  //fill the buffer;
  221.                                 // char posArray[1];//create posArray
  222.                                 // posArray[0]='5';
  223.                                  int n = sprintf(posArray, "%d ", pos);//convert from int to char[]
  224.                                  //timeFile<<"Pos(1) : "<<posArray<<endl;
  225.                                  buff[0]=posArray[0];//copy position to buff[0]
  226.                                  //timeFile<<"Buffer(1) : "<<buff<<endl;
  227.                                  for(int i=1;i<14;i++)
  228.                                      buff[i]=number[i-1];//fill buff with the time
  229.                                  //timeFile<<"Buffer(2) : "<<buff<<endl;
  230.                                  int ind=0;
  231.                                  for(int i=14;i<bSize-2;i++)
  232.                                  {
  233.                                      buff[i]=indexString[ind];//fill the rest of the buff with the value of the index
  234.                                      ind++;
  235.                                  }
  236.                                  //timeFile<<"Buffer(3) : "<<buff<<endl;
  237.                                  for(int i=14;i<bSize-2;i++)
  238.                                  {
  239.                                      if(buff[i]==NULL)
  240.                                              buff[i]='0';
  241.                                  }
  242.                                 buff[bSize-2] = '\r';
  243.                                  buff[bSize-1] = '\n';
  244.                                  //timeFile<<"Buffer(4) : "<<buff<<endl;
  245.                                  cout<<"last char"<<buff[bSize-1]<<endl;
  246.                                  cout<< buff<< endl;
  247.                     timeFile<<buff<<endl;
  248.                                     cout << pos << "/" << buff[0] << endl;
  249.                                     int count = send(sock_descriptor, buff, strlen(buff), 0);
  250.                                     if (count < 0)
  251.                                             printf("Failed writing requested bytes to server\n");
  252.  
  253.                                     timer = 0;
  254.  
  255.                             }
  256.  
  257.                     }
  258.                     oldpos = newpos;
  259.  
  260.                     if (waitKey(30) == 27) {
  261.                             cout << "esc key is pressed by user" << endl;
  262.                             break;
  263.                     }
  264.             }
  265.             timeFile.close();
  266.             return 0;
  267.  
  268.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement