Advertisement
yaramohamed1

Untitled

Sep 20th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 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(65532);
  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. char posArray[1];//create posArray
  97. posArray[0]='5';
  98. while (1)
  99. {
  100. clock_t now = clock() - t0;
  101. if ((now * 1000.0) / CLOCKS_PER_SEC >= 1000)
  102. {
  103. time(&rawtime);
  104. ptm = gmtime(&rawtime);
  105. cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec
  106. << " FPS: " << frames << endl;
  107. frames = 0;
  108. t0 = clock();
  109. }
  110. frames++;
  111. IplImage* oldImageGrey;
  112. bool bSuccess;
  113.  
  114. bSuccess = cap.read(currentFrame);
  115.  
  116. if (!bSuccess) //if not success, break loop
  117. {
  118. cout << "Cannot read a frame from video stream" << endl;
  119. break;
  120. }
  121.  
  122. cvtColor(currentFrame, currentFrameGrey, CV_BGR2GRAY);
  123. if (first) {
  124. convertScaleAbs(currentFrameGrey, oldFrameGrey, 1.0, 0.0);
  125. first = false;
  126. imshow("Original Pic", currentFrame);
  127. continue;
  128. }
  129. absdiff(currentFrameGrey, oldFrameGrey, differenceFrame);
  130. blur(differenceFrame, differenceFrame, Size(3, 3));
  131. threshold(differenceFrame, differenceFrame, 25, 255, CV_THRESH_BINARY);
  132.  
  133. imshow("Live Web Cam", currentFrame);
  134. imshow("Difference Frame", differenceFrame);
  135. Moments moment = moments(differenceFrame, 1);
  136. double mom10 = moment.m10;
  137. double mom01 = moment.m01;
  138. double area = moment.m00;
  139. int posX = (int) (mom10 / area);
  140. int posY = (int) (mom01 / area);
  141. int pos = 0;
  142. int x = floor(posX / 212.0);
  143. int y = floor(posY / 163.0);
  144.  
  145. pos = x + 3 * y;
  146. pos++;
  147.  
  148. newpos = pos;
  149. if (oldpos == newpos) {
  150. if (timer == 0)
  151. t1 = clock();
  152. timer++;
  153. if (timer == 2) {
  154. t2 = (clock() - t1) * 1000 / CLOCKS_PER_SEC;
  155. time(&rawtime);
  156. ptm = gmtime(&rawtime);
  157. cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec
  158. << " latency: " << t2 << endl;
  159.  
  160. //update the index value
  161. index++;
  162. char indexString[10]={};
  163. // std::string indexString;//create string
  164. // std::stringstream str;//create new stringstream
  165. // str<<index;//put index in the stringstream
  166. // str>>indexString;//output the stringstream in indexString
  167. sprintf(indexString,"%ld",index);
  168. //GET TIME IN MILISECONDS
  169. struct timeval sysTime; //get time of system in seconds since (1/1/1970)
  170. gettimeofday(&sysTime,NULL);
  171. int milli=sysTime.tv_usec/1000; //get miliseconds by dividing micro by 1000
  172. time_t timeSeconds=sysTime.tv_sec; //get seconds
  173. long time_mili=static_cast <long> (timeSeconds); //cast from time_t to long
  174. // time_mili=time_mili*1000;
  175. // time_mili+=milli;
  176. //from long to string
  177. char number1[10], number2[3],number[13];
  178.  
  179. sprintf(number1,"%ld",time_mili);
  180. sprintf(number2,"%d",milli);
  181. if(number1[2]==' ')
  182. {
  183. char c0=number1[0];
  184. char c1=number1[1];
  185. number1[0]='0';
  186. number1[1]=c0;
  187. number1[2]=c1;
  188. }
  189. for(int i=0;i<10;i++)
  190. number[i]=number1[i];
  191. int dd=0;
  192. for(int i=10;i<13;i++)
  193. {
  194. number[i]=number2[dd];
  195. dd++;
  196. }
  197. //strcpy(number,number1);
  198. //strncat(number,number2,10);
  199.  
  200.  
  201. cout << "timemilli,number" <<number1 <<","<<number2<< "," << number << endl ;
  202. //create Buffer
  203. int bSize=26; //(strlen(indexString)
  204. cout<<"index is"<< indexString<< endl;
  205. char buff[bSize];
  206.  
  207. //fill the buffer;
  208. // char posArray[1];//create posArray
  209. // posArray[0]='5';
  210. int n = sprintf(posArray, "%d ", pos);//convert from int to char[]
  211. buff[0]=posArray[0];//copy position to buff[0]
  212. for(int i=1;i<14;i++)
  213. buff[i]=number[i-1];//fill buff with the time
  214. int ind=0;
  215. for(int i=14;i<bSize-2;i++)
  216. {
  217. buff[i]=indexString[ind];//fill the rest of the buff with the value of the index
  218. ind++;
  219. }
  220. buff[bSize-2] = '\r';
  221. buff[bSize-1] = '\n';
  222. cout<<"last char"<<buff[bSize-1]<<endl;
  223. timeFile<<buff<<endl;
  224. cout << pos << "/" << buff[0] << endl;
  225. int count = send(sock_descriptor, buff, strlen(buff), 0);
  226. if (count < 0)
  227. printf("Failed writing requested bytes to server\n");
  228.  
  229. timer = 0;
  230.  
  231. }
  232.  
  233. }
  234. oldpos = newpos;
  235.  
  236. if (waitKey(30) == 27) {
  237. cout << "esc key is pressed by user" << endl;
  238. break;
  239. }
  240. }
  241. timeFile.close();
  242. return 0;
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement