Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenCVTest.cpp : Defines the entry point for the console application.
- //
- #ifdef WIN32
- #include <windows.h>
- #include <winsock2.h>
- #else
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #endif
- //#include "opencv2/opencv.hpp"
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <strings.h>
- #include <stdlib.h>
- #include <iostream>
- #include <time.h>
- #include <iostream>
- #include <ctime>
- #include <fstream>
- #include <sys/time.h>
- #include <stdio.h>
- using namespace cv;
- using namespace std;
- int main(int argc, char* argv[]) {
- #ifdef __WIN32__
- WORD versionWanted = MAKEWORD(1, 1);
- WSADATA wsaData;
- WSAStartup(versionWanted, &wsaData);
- #endif
- //Open File
- ofstream timeFile; //sent times
- timeFile.open("time.txt");
- int sock_descriptor;
- struct sockaddr_in serv_addr;
- struct hostent *server;
- sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
- if (sock_descriptor < 0)
- printf("Failed creating socket\n");
- memset((char *) &serv_addr, 0, sizeof(serv_addr));
- server = gethostbyname("127.0.0.1");
- if (server == NULL) {
- printf("Failed finding server name\n");
- return -1;
- }
- serv_addr.sin_family = AF_INET;
- memcpy((char *) &(serv_addr.sin_addr.s_addr), (char *) (server->h_addr), server->h_length);
- serv_addr.sin_port = htons(1248);
- if (connect(sock_descriptor, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
- {
- printf("Failed to connect to server\n");
- return -1;
- }
- else
- printf("Connected successfully\n");
- VideoCapture cap(0); // open the video camera no. 0
- if (!cap.isOpened()) // if not success, exit program
- {
- cout << "Cannot open the video cam" << endl;
- return -1;
- }
- double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
- double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
- cout << "Frame size : " << dWidth << " x " << dHeight << endl;
- bool first = true;
- int timer = 0, newpos = 0, oldpos = 0;
- Mat currentFrame, currentFrameGrey, differenceFrame, oldFrameGrey, temp;
- clock_t t0 = clock();
- time_t rawtime;
- struct tm * ptm;
- int frames = 0;
- clock_t t1, t2;
- long index=0;
- while (1)
- {
- clock_t now = clock() - t0;
- if ((now * 1000.0) / CLOCKS_PER_SEC >= 1000)
- {
- time(&rawtime);
- ptm = gmtime(&rawtime);
- cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec
- << " FPS: " << frames << endl;
- frames = 0;
- t0 = clock();
- }
- frames++;
- IplImage* oldImageGrey;
- bool bSuccess;
- bSuccess = cap.read(currentFrame);
- if (!bSuccess) //if not success, break loop
- {
- cout << "Cannot read a frame from video stream" << endl;
- break;
- }
- cvtColor(currentFrame, currentFrameGrey, CV_BGR2GRAY);
- if (first) {
- convertScaleAbs(currentFrameGrey, oldFrameGrey, 1.0, 0.0);
- first = false;
- imshow("Original Pic", currentFrame);
- continue;
- }
- absdiff(currentFrameGrey, oldFrameGrey, differenceFrame);
- blur(differenceFrame, differenceFrame, Size(3, 3));
- threshold(differenceFrame, differenceFrame, 25, 255, CV_THRESH_BINARY);
- imshow("Live Web Cam", currentFrame);
- imshow("Difference Frame", differenceFrame);
- Moments moment = moments(differenceFrame, 1);
- double mom10 = moment.m10;
- double mom01 = moment.m01;
- double area = moment.m00;
- int posX = (int) (mom10 / area);
- int posY = (int) (mom01 / area);
- int pos = 0;
- int x = floor(posX / 212.0);
- int y = floor(posY / 163.0);
- pos = x + 3 * y;
- pos++;
- newpos = pos;
- if (oldpos == newpos) {
- if (timer == 0)
- t1 = clock();
- timer++;
- if (timer == 2) {
- t2 = (clock() - t1) * 1000 / CLOCKS_PER_SEC;
- time(&rawtime);
- ptm = gmtime(&rawtime);
- cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec
- << " latency: " << t2 << endl;
- //update the index value
- index++;
- std::string indexString;//create string
- std::stringstream str;//create new stringstream
- str<<index;//put index in the stringstream
- str>>indexString;//output the stringstream in indexString
- //GET TIME IN MILISECONDS
- struct timeval sysTime; //get time of system in seconds since (1/1/1970)
- gettimeofday(&sysTime,NULL);
- int milli=sysTime.tv_usec/1000; //get miliseconds by dividing micro by 1000
- time_t timeSeconds=sysTime.tv_sec; //get seconds
- long time_mili=static_cast <long> (timeSeconds); //cast from time_t to long
- time_mili=time_mili*1000;
- time_mili+=milli;
- //from long to string
- std::string number; //create string
- std::stringstream strstream; //create new stringstream
- strstream << time_mili; //put test in the stringstream
- strstream >> number; //output the stringstream in number
- //create Buffer
- long bSize=(14)+(indexString.length());
- char buff[bSize];
- //fill the buffer;
- char posArray[1];//create posArray
- int n = sprintf(posArray, "%d ", pos);//convert from int to char[]
- buff[0]=posArray[0];//copy position to buff[0]
- for(int i=1;i<14;i++)
- buff[i]=number[i-1];//fill buff with the time
- long ind=0;
- for(int i=14;i<bSize;i++)
- {
- buff[i]=indexString[ind];//fill the rest of the buff with the value of the index
- ind++;
- }
- timeFile<<buff<<endl;
- cout << pos << "/" << buff[0] << endl;
- int count = send(sock_descriptor, buff, strlen(buff), 0);
- if (count < 0)
- printf("Failed writing requested bytes to server\n");
- timer = 0;
- }
- }
- oldpos = newpos;
- if (waitKey(30) == 27) {
- cout << "esc key is pressed by user" << endl;
- break;
- }
- }
- timeFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement