This repository has been archived by the owner on Nov 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
analysis.js
718 lines (642 loc) · 35 KB
/
analysis.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Called by Whisk.
*
* It expects the following parameters as attributes of "args"
* - cloudantUrl: "https://username:password@host"
* - cloudantDbName: "openwhisk-darkvision"
* - watsonKey: "123456"
* - doc: "image document in cloudant"
*/
function main(args) {
console.log("Analysis Action called");
console.log("Args:" + JSON.stringify(args));
// require the openwhisk npm package
var ow = require('openwhisk');
// read apihost, auth, and namespace from params
var apiHost = args.functionsHost;
var namespace = "_";
var auth = args.functionsAuth;
// generate api_key from auth
var base64Auth = new Buffer(auth).toString('base64');
var apiKey = "Basic " + base64Auth;
var options = {apihost: apiHost, api_key: apiKey, namespace: namespace};
// instantiate the openwhisk instance before you can use it
var openwhisk = ow(options);
var newdocpattern = /^1-.*/g;
var cloudantDocument = {};
cloudantDocument.args = args;
var fs = require('fs');
var request = require('request');
// nothing to do on deletion or update event
if (cloudantDocument.args.deleted) {
//console.log("[", cloudantDocument.args.id, "] Ignored, it was deleted");
return {status : "Ignoring cloudant change feed since it was for document deletion"};
} else if (newdocpattern.test(cloudantDocument.args.changes[0].rev)){
console.log("New cloudant doc detected!");
var cloudant = require('@cloudant/cloudant')(cloudantDocument.args.cloudantUrl);
var db = cloudant.db.use(cloudantDocument.args.cloudantDbName);
var fileName;
var thumbFileName;
//get document from cloudant
var p0 = function(cloudantDocument) {
console.log("Getting doc from Cloudant");
var promise = new Promise(function(resolve, reject) {
db.get(cloudantDocument.args.id, null, function(error, response) {
if (!error) {
console.log("Get DOC from cloudant successful " + JSON.stringify(response));
//Adding args to cloudant document for future reference
response.args = cloudantDocument.args;
cloudantDocument = response;
console.log("Entered Main Analysis Implementation");
if (cloudantDocument.hasOwnProperty("_id") &&
cloudantDocument.type == "image_db.image" &&
!cloudantDocument.hasOwnProperty("analysis") &&
cloudantDocument.hasOwnProperty("_attachments") &&
cloudantDocument._attachments.hasOwnProperty("image.jpg") &&
!cloudantDocument._attachments.hasOwnProperty("thumbnail.jpg")) {
console.log("DOC EXISTS!");
var imageDocumentId = cloudantDocument._id;
console.log("[", imageDocumentId, "] Processing image.jpg from document");
resolve(cloudantDocument);
} else {
return {status: "Document did not contain correct properties, ignoring"};
}
} else {
console.log("Error getting document");
console.log(error);
reject(err);
}
});
});
return promise;
};
var p1 = function(cloudantDocument) {
console.log("Initial doc and args here: " + JSON.stringify(cloudantDocument));
var promise = new Promise(function(resolve, reject) {
db.get(imageDocumentId, null, function(error, response) {
if (!error) {
console.log('read success', response);
resolve(cloudantDocument);
} else {
console.error('read error', error);
reject(error);
}
});
});
return promise;
};
//Enrich cloudant document with Weather Data
var p8 = function(cloudantDocument) {
var promise = new Promise(function(resolve, reject) {
cloudantDocument.weather = {};
if (true) {
resolve(cloudantDocument);
}
else {
reject(Error("It broke"));
}
});
return promise;
};
//Get Attachment from Cloudant
var p2 = function(cloudantDocument) {
console.log("After enriching data with Weather: " + JSON.stringify(cloudantDocument));
fileName = cloudantDocument._id + "-image.jpg";
var promise = new Promise(function(resolve, reject) {
db.attachment.getAsStream(cloudantDocument._id, "image.jpg").pipe(fs.createWriteStream(fileName))
.on("finish", function () {
console.log("Completed get of attachment");
resolve(cloudantDocument);
})
.on("error", function (err) {
console.log("Error on get of attachment");
reject(Error("attachment error"));
});
});
return promise;
};
//Process Thumbnail
var p3 = function(cloudantDocument) {
console.log("After attachment from Cloudant " + JSON.stringify(cloudantDocument));
thumbFileName = cloudantDocument._id + "-thumbnail.jpg";
var promise = new Promise(function(resolve, reject) {
console.log("generating thumbnail");
processThumbnail(cloudantDocument, fileName, thumbFileName, function (err, cloudantDocument, thumbFileName) {
if (err) {
console.log("Rejecting processThumbnail");
reject(err);
} else {
console.log("Resolving processThumbnail" + JSON.stringify(cloudantDocument));
resolve(cloudantDocument);
}
});
});
return promise;
};
//Process Image
var p4 = function(cloudantDocument) {
var promise = new Promise(function(resolve, reject) {
console.log("processing & analyzing image")
processImage(cloudantDocument, fileName, function (err, analysis) {
if (err) {
console.log("Rejecting processImage");
reject(err);
} else {
console.log("Resolving processImage");
cloudantDocument.analysis = analysis;
//console.log("Document info: " + JSON.stringify(cloudantDocument));
resolve(cloudantDocument);
}
});
});
return promise;
};
//Insert data into Cloudant
var p5 = function(cloudantDocument) {
var promise = new Promise(function(resolve, reject) {
console.log("Updating document: " + cloudantDocument._id + ", rev: " + cloudantDocument._rev)
db.insert(cloudantDocument, function (err, body, headers) {
if (err) {
console.log("Error reached while trying to update document");
reject(err);
} else {
//console.log("BODY AFTER UUPDATE IS: " + JSON.stringify(body));
//console.log("HEADERS AFTER UUPDATE IS: " + JSON.stringify(headers));
cloudantDocument._rev = body.rev;
console.log("doc after update is: " + JSON.stringify(cloudantDocument));
resolve(cloudantDocument);
}
});
});
return promise;
};
//Insert attachment
var p6 = function(cloudantDocument) {
var promise = new Promise(function(resolve, reject) {
console.log("saving thumbnail: " + thumbFileName + " to:");
//console.log(JSON.stringify(cloudantDocument));
fs.readFile(thumbFileName, function(err, data) {
if (err) {
reject(err);
} else {
console.log("ONE DB IS : " + JSON.stringify(db));
db.attachment.insert(cloudantDocument._id, 'thumbnail.jpg', data, 'image/jpg', {rev:cloudantDocument._rev}, function(err, body) {
console.log("insert complete");
//console.log(body);
//remove thumb file after saved to cloudant
var fs = require('fs');
fs.unlink(thumbFileName);
if (err) {
console.log("ERROR DURING ATTACHMENT INSERT");
console.log(err);
reject(err);
} else {
console.log("saved thumbnail");
//console.log("Body is: " + JSON.stringify(body));
cloudantDocument._rev = body.rev;
console.log("Doc: " + JSON.stringify(cloudantDocument));
resolve(cloudantDocument);
}
});
}
});
});
return promise;
};
//Process faces
var p7 = function(cloudantDocument) {
var promise = new Promise(function(resolve, reject) {
console.log("Processing faces");
processFaces(cloudantDocument, fileName, db, cloudantDocument.analysis, function (err) {
var fs = require('fs');
fs.unlink(fileName);
console.log("Finished processing faces");
resolve(cloudantDocument);
});
});
return promise;
};
return p0(cloudantDocument).then(p8).then(p2).then(p3).then(p4).then(p5).then(p6).then(p7);
} else {
//console.log("Cloudant update detected, ignoring");
return {status : "Ignoring cloudant change feed since it was for document update"};
}
}
/**
* Uses a callback so that this same code can be imported in another JavaScript
* to test the function outside of OpenWhisk.
*
* mainCallback(err, analysis)
*/
function mainImpl(doc) {
}
/**
* Prepares and analyzes the image.
* processCallback = function(err, analysis);
*/
function processFaces(document, fileName, db, analysis, processCallback) {
console.log("processing detected faces...");
console.log("DB VALUE IS: " + JSON.stringify(db));
var fs = require('fs');
if (analysis && analysis.hasOwnProperty("face_detection")) {
console.log("analysis has face_detection");
var faceIndex = -1,
facesToProcess = [],
latestDocument = document;
if (analysis.face_detection.images){
if (analysis.face_detection.images.length > 0) {
var images = analysis.face_detection.images;
if (images[0].faces) {
facesToProcess = analysis.face_detection.images[0].faces;
}
}
}
//iteratively create images for each face that is detected
var inProgressCallback = function (err) {
console.log("inside inProgressCallback");
faceIndex++;
if (err) {
processCallback( err );
console.log(err)
} else {
if (faceIndex < facesToProcess.length) {
console.log('generating face ' + (faceIndex+1) + " of " + facesToProcess.length);
generateFaceImage(fileName, facesToProcess[faceIndex], "face" + faceIndex +".jpg", function(err, faceImageName) {
if (err) {
inProgressCallback(err);
} else {
//save to cloudant
console.log("saving face image: " + faceImageName);
fs.readFile(faceImageName, function(readErr, data) {
if (readErr) {
console.log("readErr reached");
inProgressCallback(err);
} else {
console.log("ABOUT TO INSERT FACE ATTACHMENT");
console.log("AND DOC IS: " + JSON.stringify(latestDocument));
console.log("AND REV IS: " + latestDocument._rev);
console.log("AND faceImageName IS: " + faceImageName);
console.log("DB VALUE IS: " + JSON.stringify(db));
console.log("TWO DB IS : " + JSON.stringify(db));
db.attachment.insert(latestDocument._id, faceImageName, data, 'image/jpg',
{rev:latestDocument._rev}, function(saveErr, body) {
if (!saveErr){
console.log("insert complete");
console.log("AFTER INSERT COMPLETE BODY IS: " + JSON.stringify(body));
console.log("SAVE ERROR IS: " + saveErr);
latestDocument._id = body.id;
latestDocument._rev = body.rev;
//remove thumb file after saved to cloudant
var fs = require('fs');
fs.unlink(faceImageName);
console.log("saved thumbnail");
inProgressCallback(saveErr);
} else {
console.log("SAVE ERROR IS: " + saveErr);
return {ERROR: "Error during save faces"};
}
});
}
});
}
});
} else {
processCallback(null)
}
}
}
inProgressCallback(null);
} ;
}
/**
* Prepares the image, resizing it if it is too big for Watson or Alchemy.
* prepareCallback = function(err, fileName);
*/
function generateFaceImage(fileName, faceData, faceImageName, callback) {
console.log('inside generateFaceImage');
var
fs = require('fs'),
async = require('async'),
gm = require('gm').subClass({
imageMagick: true
});
var face_location = faceData["face_location"];
gm(fileName)
.crop(face_location.width, face_location.height, face_location.left, face_location.top)
.write(faceImageName, function (err) {
if (err) {
console.log(err);
callback( err );
} else {
console.log('face image generation done: ' + faceImageName);
callback(null, faceImageName);
}
});
}
/**
* Prepares and analyzes the image.
* processCallback = function(err, analysis);
*/
function processThumbnail(doc, fileName, thumbFileName, processCallback) {
console.log("thumbfile name before generate: " + thumbFileName);
generateThumbnail(fileName, thumbFileName, function (err) {
console.log("thumbfile name after generate: " + thumbFileName);
//save to cloudant
processCallback(err, doc, thumbFileName);
});
}
/**
* Prepares the image, resizing it if it is too big for Watson or Alchemy.
* prepareCallback = function(err, fileName);
*/
function generateThumbnail(fileName, thumbFileName, callback) {
console.log("Inside of generateThumbnail");
var
fs = require('fs'),
async = require('async'),
gm = require('gm').subClass({
imageMagick: true
});
console.log("Starting GM:");
gm(fileName)
.resize(200, 200)
.write(thumbFileName, function (err) {
if (err) {
console.log(err);
callback( err );
} else {
console.log('thumb generation done');
callback(null, thumbFileName);
}
});
console.log("completed thumbnail writing");
}
/**
* Prepares and analyzes the image.
* processCallback = function(err, analysis);
*/
function processImage(doc, fileName, processCallback) {
prepareImage(fileName, function (prepareErr, prepareFileName) {
if (prepareErr) {
processCallback(prepareErr, null);
} else {
analyzeImage(doc, prepareFileName, function (err, analysis) {
processCallback(err, analysis);
});
}
});
}
/**
* Prepares the image, resizing it if it is too big for Watson or Alchemy.
* prepareCallback = function(err, fileName);
*/
function prepareImage(fileName, prepareCallback) {
console.log("Prepare Image Method starting");
var
fs = require('fs'),
async = require('async'),
gm = require('gm').subClass({
imageMagick: true
});
async.waterfall([
function (callback) {
// Retrieve the file size
fs.stat(fileName, function (err, stats) {
if (err) {
callback(err);
} else {
callback(null, stats);
}
});
},
// Check if size is OK
function (fileStats, callback) {
if (fileStats.size > 900 * 1024) {
// Resize the file
gm(fileName).define("jpeg:extent=900KB").write(fileName + ".jpg",
function (err) {
if (err) {
callback(err);
} else {
// Process the modified file
callback(null, fileName + ".jpg");
}
});
} else {
callback(null, fileName);
}
}
], function (err, fileName) {
prepareCallback(err, fileName);
});
}
/**
* Analyzes the image stored at fileName with the callback onAnalysisComplete(err, analysis).
* analyzeCallback = function(err, analysis);
*/
function analyzeImage(doc, fileName, analyzeCallback) {
console.log("Starting Analyze Image Method");
var
request = require('request'),
async = require('async'),
fs = require('fs'),
gm = require('gm').subClass({
imageMagick: true
}),
analysis = {};
async.parallel([
function (callback) {
// Write down meta data about the image
gm(fileName).size(function (err, size) {
if (err) {
console.log("Image size", err);
} else {
analysis.size = size;
}
callback(null);
});
},
function (callback) {
// Call Watson Visual Recognition Face Detection passing the image in the request
fs.createReadStream(fileName).pipe(
request({
method: "POST",
url: "https://apikey:" + doc.args.watsonKey + "@gateway.watsonplatform.net" +
"/visual-recognition/api/v3/detect_faces" +
"?version=2018-03-19",
headers: {
'Content-Length': fs.statSync(fileName).size
},
json: true
},
function (err, response, body) {
if (err) {
console.log("Face Detection ERROR: ", err);
analysis.face_detection = {
error:err
}
} else {
console.log("Face Detection SUCCESS:")
console.log(body)
analysis.face_detection = body;
}
callback(null);
}))
},
function (callback) {
// Call Watson Visual Recognition Image Classifier passing the image in the request
console.log('CLASSIFIERS:' + doc.args.watsonClassifiers)
fs.createReadStream(fileName).pipe(
request({
method: "POST",
url: "https://apikey:" + doc.args.watsonKey + "@gateway.watsonplatform.net" +
"/visual-recognition/api/v3/classify" +
"?version=2018-03-19&threshold=0.0&owners=me,IBM&classifier_ids=" + doc.args.watsonClassifiers,
headers: {
'Content-Length': fs.statSync(fileName).size
},
json: true
},
function (err, response, body) {
if (err) {
console.log("Image Classifier ERROR", err);
analysis.image_classify = {
error:err
}
} else {
console.log("Image Classifier SUCCESS:")
console.log(JSON.stringify(body))
analysis.image_classify = body;
}
callback(null);
}))
},
function (callback) {
// Call Watson Visual Recognition 'Recognize Text' passing the image in the request
fs.createReadStream(fileName).pipe(
request({
method: "POST",
url: "https://apikey:" + doc.args.watsonKey + "@gateway.watsonplatform.net" +
"/visual-recognition/api/v3/recognize_text" +
"?version=2018-03-19",
headers: {
'Content-Length': fs.statSync(fileName).size
},
json: true
},
function (err, response, body) {
if (err) {
console.log("Recognize Text ERROR", err);
analysis.recognize_text = {
error:err
}
} else {
console.log("Recognize Text SUCCESS:")
console.log(body)
analysis.recognize_text = body;
}
callback(null);
}))
}
],
function (err, result) {
analyzeCallback(err, analysis);
}
)
}
/**
* Update a document in Cloudant database:
* https://docs.cloudant.com/document.html#update
**/
function cloudantupdatemain(message) {
var cloudantOrError = getCloudantAccount(message);
if (typeof cloudantOrError !== 'object') {
return Promise.reject(cloudantOrError);
}
var cloudant = cloudantOrError;
var dbName = message.dbname;
var doc = message.doc;
var params = {};
if(!dbName) {
return Promise.reject('dbname is required.');
}
if (typeof message.doc === 'object') {
doc = message.doc;
} else if (typeof message.doc === 'string') {
try {
doc = JSON.parse(message.doc);
} catch (e) {
return Promise.reject('doc field cannot be parsed. Ensure it is valid JSON.');
}
} else {
return Promise.reject('doc field is ' + (typeof doc) + ' and should be an object or a JSON string.');
}
if(!doc || !doc.hasOwnProperty("_rev")) {
return Promise.reject('doc and doc._rev are required.');
}
var cloudantDb = cloudant.use(dbName);
if (typeof message.params === 'object') {
params = message.params;
} else if (typeof message.params === 'string') {
try {
params = JSON.parse(message.params);
} catch (e) {
return Promise.reject('params field cannot be parsed. Ensure it is valid JSON.');
}
}
return insert(cloudantDb, doc, params);
}
/**
* Inserts updated document into database.
*/
function insert(cloudantDb, doc, params) {
return new Promise(function(resolve, reject) {
cloudantDb.insert(doc, params, function(error, response) {
if (!error) {
console.log('success', response);
resolve(response);
} else {
console.log('error', error);
reject(error);
}
});
});
}
function getCloudantAccount(message) {
// full cloudant URL - Cloudant NPM package has issues creating valid URLs
// when the username contains dashes (common in Bluemix scenarios)
var cloudantUrl;
if (message.url) {
// use bluemix binding
cloudantUrl = message.url;
} else {
if (!message.host) {
return 'cloudant account host is required.';
}
if (!message.username) {
return 'cloudant account username is required.';
}
if (!message.password) {
return 'cloudant account password is required.';
}
cloudantUrl = "https://" + message.username + ":" + message.password + "@" + message.host;
}
return require('cloudant')({
url: cloudantUrl
});
}