forked from tiendan/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
executable file
·220 lines (164 loc) · 7.04 KB
/
utils.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
#include "utils.h"
int tracker_status = STATUS_IDLE;
bool is_tracker_calibrated = false;
int dwelltime_parameter = 20;
int test_dwelltime_parameter = 20;
int sleep_parameter = 0;
CvRect* face_rectangle = NULL;
void releaseImage(IplImage *image) {
// cout << "deleting shared image" << endl;
cvReleaseImage(&image);
}
boost::shared_ptr<IplImage> createImage(const CvSize &size, int depth, int channels) {
return boost::shared_ptr<IplImage>(cvCreateImage(size, depth, channels),
releaseImage);
}
void mapToFirstMonitorCoordinates(Point monitor2point, Point& monitor1point)
{
int num_of_monitors = Gdk::Screen::get_default()->get_n_monitors();
Gdk::Rectangle monitor1geometry;
Gdk::Rectangle monitor2geometry;
Glib::RefPtr<Gdk::Screen> screen = Gdk::Display::get_default()->get_default_screen();
screen->get_monitor_geometry(0, monitor1geometry);
screen->get_monitor_geometry(num_of_monitors - 1, monitor2geometry);
monitor1point.x = (monitor2point.x / monitor2geometry.get_width()) * (monitor1geometry.get_width() - 40) + monitor1geometry.get_x();
monitor1point.y = (monitor2point.y / monitor2geometry.get_height()) * monitor1geometry.get_height() + monitor1geometry.get_y();
}
void mapToVideoCoordinates(Point monitor2point, double resolution, Point& videoPoint, bool reverse_x)
{
int num_of_monitors = Gdk::Screen::get_default()->get_n_monitors();
Gdk::Rectangle monitor1geometry(0, 0, 1280, 720);
Gdk::Rectangle monitor2geometry;
Glib::RefPtr<Gdk::Screen> screen = Gdk::Display::get_default()->get_default_screen();
screen->get_monitor_geometry(num_of_monitors - 1, monitor2geometry);
if(resolution == 480) {
monitor1geometry.set_width(640);
monitor1geometry.set_height(480);
}
else if(resolution == 1080) {
monitor1geometry.set_width(1920);
monitor1geometry.set_height(1080);
}
monitor2geometry.set_width(1280);
monitor2geometry.set_height(777);
if(reverse_x) {
videoPoint.x = monitor1geometry.get_width() - (monitor2point.x / monitor2geometry.get_width()) * monitor1geometry.get_width();
}
else {
videoPoint.x = (monitor2point.x / monitor2geometry.get_width()) * monitor1geometry.get_width();
}
videoPoint.y = (monitor2point.y / monitor2geometry.get_height()) * monitor1geometry.get_height();
}
// Neural network
void mapToNeuralNetworkCoordinates(Point point, Point& nnpoint)
{
int num_of_monitors = Gdk::Screen::get_default()->get_n_monitors();
Gdk::Rectangle monitor1geometry(0, 0, 1, 1);
Gdk::Rectangle monitor2geometry;
Glib::RefPtr<Gdk::Screen> screen = Gdk::Display::get_default()->get_default_screen();
screen->get_monitor_geometry(num_of_monitors - 1, monitor2geometry);
nnpoint.x = ((point.x - monitor2geometry.get_x()) / monitor2geometry.get_width()) * monitor1geometry.get_width() + monitor1geometry.get_x();
nnpoint.y = ((point.y - monitor2geometry.get_y()) / monitor2geometry.get_height()) * monitor1geometry.get_height() + monitor1geometry.get_y();
//cout << "ORIG: " << point.x << ", " << point.y << " MAP: " << nnpoint.x << ", " << nnpoint.y << endl;
}
void mapFromNeuralNetworkToScreenCoordinates(Point nnpoint, Point& point)
{
int num_of_monitors = Gdk::Screen::get_default()->get_n_monitors();
Gdk::Rectangle monitor1geometry;
Gdk::Rectangle monitor2geometry(0, 0, 1, 1);
Glib::RefPtr<Gdk::Screen> screen = Gdk::Display::get_default()->get_default_screen();
// Geometry of main monitor
screen->get_monitor_geometry(num_of_monitors - 1, monitor1geometry);
point.x = ((nnpoint.x - monitor2geometry.get_x()) / monitor2geometry.get_width()) * monitor1geometry.get_width() + monitor1geometry.get_x();
point.y = ((nnpoint.y - monitor2geometry.get_y()) / monitor2geometry.get_height()) * monitor1geometry.get_height() + monitor1geometry.get_y();
//cout << "ORIG: " << point.x << ", " << point.y << " MAP: " << nnpoint.x << ", " << nnpoint.y << endl;
}
string getUniqueFileName(string directory, string base_file_name)
{
string file_abs_name;
boost::filesystem::path current_dir(directory);
int maximum_existing_no = 0;
//boost::regex pattern(base_file_name + ".*"); // list all files having this base file name
// Check all the files matching this base file name and find the maximum serial number until now
for (boost::filesystem::directory_iterator iter(current_dir),end; iter!=end; ++iter) {
string name = iter->path().filename().string();
if (strncmp(base_file_name.c_str(), name.c_str(), base_file_name.length()) == 0) {// regex_match(name, pattern)) {
//cout << "MATCH: base=" << base_file_name << ", file=" << name << endl;
int current_no = 0;
name = name.substr(base_file_name.length() + 1);
//cout << "After substr=" << name << endl;
sscanf(name.c_str(), "%d", ¤t_no);
//cout << "NO= " << current_no << endl;
if(current_no > maximum_existing_no) {
maximum_existing_no = current_no;
}
}
}
//cout << "Max. existing no = " << maximum_existing_no << endl;
// Return the next serial number
return directory + "/" + base_file_name + "_" + boost::lexical_cast<std::string>(maximum_existing_no + 1) + ".txt";
}
namespace boost {
template<>
void checked_delete(IplImage *image) {
// cout << "deleting scoped image" << endl;
if (image)
cvReleaseImage(&image);
}
}
// Normalize to 50-200 interval
void normalizeGrayScaleImage2(IplImage *image, double standard_mean, double standard_std) {
double minVal, maxVal;
double scale = 1;
double interval_start = 25;
double interval_end = 230;
cvMinMaxLoc(image, &minVal, &maxVal);
cvConvertScale(image, image, 1, -1 * minVal); // Subtract the minimum value
// If pixel intensities are between 0 and 1
if(maxVal < 2) {
interval_start = interval_start/255.0;
interval_end = interval_end/255.0;
}
scale = (interval_end-interval_start) / (maxVal - minVal);
// Scale the image to the selected interval
cvConvertScale(image, image, scale, 0);
cvConvertScale(image, image, 1, interval_start);
}
// Normalize by making mean and standard deviation equal in all images
void normalizeGrayScaleImage(IplImage *image, double standard_mean, double standard_std) {
CvScalar scalar_mean, scalar_std;
double mean, std;
cvAvgSdv(image, &scalar_mean, &scalar_std);
mean = scalar_mean.val[0];
std = scalar_std.val[0];
//cout << "Image mean and std is " << mean << ", " << std << endl;
double ratio = standard_std/std;
double shift = standard_mean - mean*ratio;
cvConvertScale(image, image, ratio, shift); // Move the mean from 0 to 127
}
void printMat(CvMat* mat)
{
printf("(%dx%d)\n",mat->cols,mat->rows);
for(int i=0; i<mat->rows; i++)
{
if(i==0)
{
for(int j=0; j<mat->cols; j++) printf("%10d",j+1);
}
printf("\n%4d: ",i+1);
for(int j=0; j<mat->cols; j++)
{
printf("%10.2f",cvGet2D(mat,i,j).val[0]);
}
}
printf("\n");
}
void printVectorOfVectors(vector<vector<int> > v) {
for(int i=0; i<v.size(); i++) {
for(int j=0; j<v[i].size(); j++) {
cout << (v[i])[j] << ", ";
}
cout << endl;
}
cout << endl << endl;
}