-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathindex.js
191 lines (153 loc) · 3.64 KB
/
index.js
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
/**
* Simple example Node.js application to demonstrate face detection.
*/
/**
* Define the dependencies
*/
var express = require( 'express' )
, http = require( 'http' )
, async = require( 'async' )
, multer = require( 'multer' )
, upload = multer( { dest: 'uploads/' } )
, exphbs = require( 'express-handlebars' )
, easyimg = require( 'easyimage' )
, _ = require( 'lodash' )
, cv = require( 'opencv' );
/**
* Create a simple hash of MIME types to file extensions
*/
var exts = {
'image/jpeg' : '.jpg',
'image/png' : '.png',
'image/gif' : '.gif'
}
/**
* Note that you may want to change this, depending on your setup.
*/
var port = 8080;
/**
* Create the express app
*/
var app = express();
/**
* Set up the public directory
*/
app.use(express.static(__dirname + '/public'))
/**
* Set up Handlebars templating
*/
app.engine('.hbs', exphbs( { extname: '.hbs', defaultLayout: 'default' } ) );
app.set( 'view engine', '.hbs' );
/**
* Default page; simply renders a file upload form
*/
app.get('/', function( req, res, next ) {
return res.render('index');
});
/**
* POST callback for the file upload form. This is where the magic happens.
*/
app.post('/upload', upload.single('file'), function(req, res, next){
// Generate a filename; just use the one generated for us, plus the appropriate extension
var filename = req.file.filename + exts[req.file.mimetype]
// and source and destination filepaths
, src = __dirname + '/' + req.file.path
, dst = __dirname + '/public/images/' + filename;
/**
* Go through the various steps
*/
async.waterfall(
[
function( callback ) {
/**
* Check the mimetype to ensure the uploaded file is an image
*/
if (!_.contains(
[
'image/jpeg',
'image/png',
'image/gif'
],
req.file.mimetype
) ) {
return callback( new Error( 'Invalid file - please upload an image (.jpg, .png, .gif).' ) )
}
return callback();
},
function( callback ) {
/**
* Get some information about the uploaded file
*/
easyimg.info( src ).then(
function(file) {
/**
* Check that the image is suitably large
*/
if ( ( file.width < 960 ) || ( file.height < 300 ) ) {
return callback( new Error( 'Image must be at least 640 x 300 pixels' ) );
}
return callback();
}
);
},
function( callback ) {
/**
* Resize the image to a sensible size
*/
easyimg.resize(
{
width : 960,
src : src,
dst : dst
}
).then(function(image) {
return callback();
});
},
function( callback ) {
/**
* Use OpenCV to read the (resized) image
*/
cv.readImage( dst, callback );
},
function( im, callback ) {
/**
* Run the face detection algorithm
*/
im.detectObject( cv.FACE_CASCADE, {}, callback );
}
],
function( err, faces ) {
/**
* If an error occurred somewhere along the way, render the
* error page.
*/
if ( err ) {
return res.render(
'error',
{
message : err.message
}
);
}
/**
* We're all good; render the result page.
*/
return res.render(
'result',
{
filename : filename,
faces : faces
}
);
}
);
});
/**
* Start the server
*/
http.createServer(
app
).listen( port, function( server ) {
console.log( 'Listening on port %d', port );
});