-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcam_capvideo.cpp
223 lines (169 loc) · 5.36 KB
/
webcam_capvideo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <cv.h>
#include <highgui.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <ctime>
#include <ml.h>
#define EXPECTEDFPS 30
#define THRESHOLD 245
#define COLOUR 255
#define USECSPERSEC 1000000
#define USECSPERMSEC 1000
#define EXPCTDFTM 50000 // @ 20 fps -> 50000 microsecond between each frame
#define SHOWPREVCURR 0
#define HISTSIZE 26 // (256/10 + 1)
#define SHOWHIST 0
#define USEOPENOP 0
#define MORPHSIZE 4
#define OPENOP 2
#define flip(x) ((x == '0')?x='1':x='0')
#define isOn(x) (x == '1') // currently, when off -> bright pupil image
#define THRESHREFRESHRATE 5 // seconds
#define ADPTTHRSCUT (0.75)
#define SSORIG '0'
#define SSDIFFIMG '9'
#define RESIZEFCTR 6
#define DEBUGON 0 // boolean to turn on/off debug printf statements
#define TIMGW 75 // width of training images
#define TIMGH 75 // height of training images
#define CAMWIDTH 1920
#define CAMHEIGHT 1080
#define EYECLASS 1
#define NONEYECLASS -1
using namespace cv;
void calcplothist(Mat &img)
{
float range[] = { 0, 256 } ;
const float* histRange = { range };
int bins = HISTSIZE;
Mat hist;
calcHist( &img, 1, 0, Mat(), hist, 1, &bins, &histRange, true, false );
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/bins );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
normalize(hist, hist, 0, hist.rows*10, NORM_MINMAX, -1, Mat());
for( int i = 1; i < bins; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i-1)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
}
imshow("diffimgHist", histImage );
}
int isInRect(Point2f &pt, Rect &r)
{
if(pt.x < (r.x + r.width) && pt.x > r.x
&& pt.y < (r.y + r.height) && pt.y > r.y)
return 1;
return 0;
}
float predict_eye(CvSVM &svm, Mat &img_mat) {
equalizeHist(img_mat,img_mat);
Mat img_mat_1d(1,img_mat.size().area(),CV_32FC1);
int ii = 0;
for (int i = 0; i<img_mat.rows; i++) {
for (int j = 0; j < img_mat.cols; j++) {
img_mat_1d.at<float>(ii++) = img_mat.at<uchar>(i,j);
}
}
return svm.predict(img_mat_1d);
}
int main()
{
short recordcandidates = 0;
char arduino_state = '0', filename[256];
int i = 0, j = 0, delay = 100000000, debug = 0, thresh = 100, framecount = 1;
long stc, etc;
double time_elapsed;
if (DEBUGON) printf("point%d\n", debug++);//0
CvCapture *capture = cvCaptureFromCAM( CV_CAP_ANY );
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, CAMWIDTH);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, CAMHEIGHT);
// the fps currently doesn't work
//cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 30);
if (DEBUGON) printf("point%d\n", debug++);//1
IplImage *prevframe, *currframe;
bool captureFlag = false;
Mat prevframe_mat, prevframe_gray, currframe_mat,
currframe_gray, diff_img, colored_diff_img, element,
openedimg, openeddiffimg, overlappedimg;
std::ofstream arduino("/dev/ttyUSB0");
VideoWriter *record;
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
double width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
double height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
printf("fps = %f, height = %f, width = %f\n", fps, height, width);
if (!capture)
{
fprintf(stderr, "ERROR: capture is NULL \n");
getchar();
return -1;
}
//cvNamedWindow( "coloreddiff", CV_WINDOW_NORMAL );
//cvNamedWindow( "prevframe_mat", CV_WINDOW_NORMAL );
cvNamedWindow( "currframe_mat", CV_WINDOW_NORMAL );
// Show the image captured from the camera in the window and repeat
currframe = cvQueryFrame(capture);
prevframe = cvCloneImage(currframe);
prevframe_mat = prevframe;
cvtColor(prevframe_mat, prevframe_gray, CV_BGR2GRAY );
printf("width = %d and height = %d\n", currframe->width, currframe->height);
cvResizeWindow("currframe_mat", 1920, 1080);
while (1)
{
arduino << arduino_state;
arduino.flush();
stc = cvGetTickCount();
currframe = cvQueryFrame(capture);
currframe_mat = currframe;
// record video
if (captureFlag == true)
{
if (DEBUGON) printf("Recording frame\n");
//Mat diff_img_color;
//cvtColor(diff_img, diff_img_color, CV_GRAY2BGR);
(*record) << currframe_mat;
}
imshow("currframe_mat",currframe_mat);
etc = cvGetTickCount();
time_elapsed = (etc - stc)/cvGetTickFrequency();
//double fps = USECSPERSEC/time_elapsed;
//printf("time elapsed %f usecs. wait time = %f msecs\n", time_elapsed, (EXPCTDFTM-time_elapsed)/USECSPERMSEC);
int wait_time = time_elapsed-EXPCTDFTM < 0?(EXPCTDFTM-time_elapsed)/USECSPERMSEC:1;
if(wait_time == 0)
{
wait_time = 1;
}
int keyVal = cvWaitKey(wait_time) & 255;
if ((keyVal) == 32) // spacebar
{
if (captureFlag==false)
{
captureFlag=true;
if (DEBUGON) printf("video is now on\n");
record = new VideoWriter("control_will3.avi", CV_FOURCC('M','J','P','G'), 30, currframe_mat.size(), true);
if( !record->isOpened() ) {
printf("VideoWriter failed to open!\n");
}
}
else if (captureFlag==true)
{
captureFlag=false;
if (DEBUGON) printf("video is now off\n");
}
}
else if ( (keyVal) == 'x' )
{
break; // break out of loop to stop program
}
framecount++;
flip(arduino_state);
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}