Advertisement
yaramohamed1

Untitled

Sep 20th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 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(number2[2]==' ')
  182. {
  183.  
  184. char c0=number2[0];
  185. char c1=number2[1];
  186. number2[0]='0';
  187. number2[1]=c0;
  188. number2[2]=c1;
  189. }
  190. for(int i=0;i<10;i++)
  191. number[i]=number1[i];
  192. int dd=0;
  193. for(int i=10;i<13;i++)
  194. {
  195. number[i]=number2[dd];
  196. dd++;
  197. }
  198. //strcpy(number,number1);
  199. //strncat(number,number2,10);
  200.  
  201.  
  202. cout << "timemilli,number" <<number1 <<","<<number2<< "," << number << endl ;
  203. //create Buffer
  204. int bSize=26; //(strlen(indexString)
  205. cout<<"index is"<< indexString<< endl;
  206. char buff[bSize];
  207.  
  208. //fill the buffer;
  209. // char posArray[1];//create posArray
  210. // posArray[0]='5';
  211. int n = sprintf(posArray, "%d ", pos);//convert from int to char[]
  212. buff[0]=posArray[0];//copy position to buff[0]
  213. for(int i=1;i<14;i++)
  214. buff[i]=number[i-1];//fill buff with the time
  215. int ind=0;
  216. for(int i=14;i<bSize-2;i++)
  217. {
  218. buff[i]=indexString[ind];//fill the rest of the buff with the value of the index
  219. ind++;
  220. }
  221. buff[bSize-2] = '\r';
  222. buff[bSize-1] = '\n';
  223. cout<<"last char"<<buff[bSize-1]<<endl;
  224. timeFile<<buff<<endl;
  225. cout << pos << "/" << buff[0] << endl;
  226. int count = send(sock_descriptor, buff, strlen(buff), 0);
  227. if (count < 0)
  228. printf("Failed writing requested bytes to server\n");
  229.  
  230. timer = 0;
  231.  
  232. }
  233.  
  234. }
  235. oldpos = newpos;
  236.  
  237. if (waitKey(30) == 27) {
  238. cout << "esc key is pressed by user" << endl;
  239. break;
  240. }
  241. }
  242. timeFile.close();
  243. return 0;
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement