forked from perillamint/3dface
/
main.cpp
212 lines (166 loc) · 4.97 KB
/
main.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
/*
* main.cpp
*
* Created on: 2012. 5. 12.
* Author: maneulyori
*/
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include "libfreenect_cv.h"
#include "faceprocess.h"
#include "PCAWrapper.h"
#define DEBUG printf("%s %s %d\n", __FILE__, __func__, __LINE__)
static CvMemStorage *storage = 0;
static CvHaarClassifierCascade *cascade = 0;
void detect_and_draw(IplImage * img, IplImage * depth, IplImage *faceDepthRet, bool save)
{
int scale = 1;
// Create a new image based on the input image
IplImage *temp =
cvCreateImage(cvSize(img->width / scale, img->height / scale), 8, 3);
memcpy(temp->imageData, img->imageData, 640 * 480 * 3);
IplImage *depthTemp =
cvCreateImage(cvSize(img->width / scale, img->height / scale), 16, 1);
memcpy(depthTemp->imageData, depth->imageData, 640 * 480 * 2);
IplImage *faceDepth =
cvCreateImage(cvSize(img->width / scale, img->height / scale), 16, 1);
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i, j, k;
// Clear the memory storage which was used before
cvClearMemStorage(storage);
// Find whether the cascade is loaded, to find the faces. If yes, then:
if (cascade)
{
// There can be more than one face in an image. So create a growable
// sequence of faces.
// Detect the objects and store them in the sequence
/* CvSeq* faces = cvHaarDetectObjects( temp, cascade, storage, 1.1, 2,
CV_HAAR_DO_CANNY_PRUNING, cvSize(40, 40) ); */
CvSeq *faces = cvHaarDetectObjects(temp, cascade, storage,
1.6, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40));
// Loop the number of faces found.
for (i = 0; i < (faces ? faces->total : 0); i++)
{
// Create a new rectangle for drawing the face
CvRect *r = (CvRect *) cvGetSeqElem(faces, i);
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x * scale;
pt2.x = (r->x + r->width) * scale;
pt1.y = r->y * scale;
pt2.y = (r->y + r->height) * scale;
// Draw the rectangle in the input image
cvRectangle(temp, pt1, pt2, CV_RGB(0, 0, 255), 3, 8, 0);
cvRectangle(depthTemp, pt1, pt2, CV_RGB(0, 0, 255), 3, 8, 0);
cvSetImageROI(depth,
cvRect(pt1.x, pt1.y, r->width * scale,
r->height * scale));
IplImage *faceDepthTemp =
cvCreateImage(cvGetSize(depth), depth->depth,
depth->nChannels);
IplImage *faceDepthTemp2 =
cvCreateImage(cvGetSize(depth), 8,
depth->nChannels);
cvCopy(depth, faceDepthTemp, NULL);
cvResetImageROI(depth);
// Maximize standard deviation.
//stretchFaceDepth(faceDepthTemp);
cvResize(faceDepthTemp, faceDepth);
cvConvertScale(faceDepthTemp, faceDepthTemp2, 1.0/256.0, 0);
cvResize(faceDepthTemp2, faceDepthRet);
cvReleaseImage(&faceDepthTemp);
if (save)
{
FILE *csvFile = fopen("face.csv", "w");
for (j = pt1.y; j < pt2.y; j++)
{
for (k = pt1.x; k < pt2.x; k++)
{
fprintf(csvFile, "%u,",
(((uint16_t *) (depth->imageData)) +
j * depth->width)[k]);
}
fprintf(csvFile, "\n");
}
printf("Face captured!\n");
fclose(csvFile);
}
}
}
// Show the image in the window named "result"
cvShowImage("result", temp);
cvShowImage("resultDepth", depthTemp);
cvShowImage("faceDepth", faceDepth);
// Release the temp image created.
cvReleaseImage(&temp);
cvReleaseImage(&depthTemp);
cvReleaseImage(&faceDepth);
}
int main(int argc, char **argv)
{
cascade = (CvHaarClassifierCascade *) cvLoad("cascade.xml", 0, 0, 0);
IplImage *faceDepth = cvCreateImage(cvSize(100, 100), 8, 1);
char name[1000];
int imageCnt=0;
PCAWrapper pca;
if (!cascade)
{
fprintf(stderr, "ERROR: Could not load classifier cascade\n");
return -1;
}
storage = cvCreateMemStorage(0);
int key = -1;
while ((key & 0xFF) != 0x1B)
{ // Break when ESC is pressed.
key = cvWaitKey(10);
IplImage *image = freenect_sync_get_rgb_cv(0);
if (!image)
{
printf("Error: Kinect not connected?\n");
return -1;
}
// DEBUG;
/*
IplImage *irimage = freenect_sync_get_ir_cv(0); if (!irimage) {
printf("Error: Kinect not connected?\n"); return -1; } */
// DEBUG;
// cvCvtColor(image, image, CV_RGB2BGR);
IplImage *depth = freenect_sync_get_depth_cv(0);
if (!depth)
{
printf("Error: Kinect not connected?\n");
return -1;
}
// DEBUG;
// printf("%d\n", key);
if ((key & 0xFF) == 'p')
{
detect_and_draw(image, depth, faceDepth, true);
}
detect_and_draw(image, depth, faceDepth, false);
if ((key & 0xFF) == 'i')
{
imageCnt++;
sprintf(name, "face%d", imageCnt);
printf("face %s registered!\n", name);
pca.insertImage(faceDepth, name);
}
if((key & 0xFF) == 'r')
{
printf("%s Dist = %f\n", pca.search(faceDepth), pca.searchDist(faceDepth));
}
if((key & 0xFF) == 't')
{
printf("Training...\n");
pca.training();
}
cvShowImage("RGB", image);
// DEBUG;
// cvShowImage("IR", irimage);
cvShowImage("Depth", depth);
// cvShowImage("Depth", GlViewColor(depth));
}
return 0;
}