Advertisement
yaramohamed1

Client-new

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