-
Notifications
You must be signed in to change notification settings - Fork 22
/
vision.js
106 lines (91 loc) · 3.44 KB
/
vision.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
"use strict"
const vision = require('node-cloud-vision-api')
const redis = require('redis')
const rc = process.env.REDISCLOUD_URL? redis.createClient(process.env.REDISCLOUD_URL) : null
var blind = true
if (process.env.VISION_API_KEY) {
vision.init({auth: process.env.VISION_API_KEY})
blind = false
} else
console.log("NO VISION API KEY. GOOGLE CLOUD VISION CALLS WILL FAIL.")
//var loan = {id: 1030057, image: {id: 2110898}}
/**
* "joyLikelihood":"VERY_LIKELY","sorrowLikelihood":"VERY_UNLIKELY","angerLikelihood":"VERY_UNLIKELY",
* "surpriseLikelihood":"VERY_UNLIKELY","underExposedLikelihood":"VERY_UNLIKELY","blurredLikelihood":"VERY_UNLIKELY",
* "headwearLikelihood":"VERY_UNLIKELY"
*/
const processFaceData = annotations => {
var result = {joy: [], sorrow: [], anger: [], surprise: [], headwear: []}
Object.keys(result).forEach(key => result[key] = annotations.where(a => ['VERY_LIKELY', 'LIKELY', 'POSSIBLE'].contains(a[`${key}Likelihood`])).select(a => a[`${key}Likelihood`]).distinct())
return result
}
function guaranteeGoogleVisionForLoan(loan, doneCallback) {
if (typeof doneCallback !== 'function') doneCallback = () => true
if (blind || !rc) {
doneCallback()
return
}
var facesKey = `vision_faces_${loan.id}`,
vlkey = `vision_label_${loan.id}`,
doFaceDetection = false,
doVisionLabels = false
if (loan.kl_faces) { // && loan.kl_visionLabels
doneCallback()
return
}
rc.mget([facesKey, vlkey], (err, result) => {
var features = []
//console.log("get array", err, result)
if (err) {
doneCallback(404)
return
}
if (!loan.kl_faces && !result[0]) {
features.push(new vision.Feature('FACE_DETECTION', 4))
doFaceDetection = true
}
//if (!loan.kl_visionLabels && !result[1]) {
//features.push(new vision.Feature('LABEL_DETECTION', 10))
//doVisionLabels = true
//}
if (!features.length) { //we have everything we need.
loan.kl_faces = loan.kl_faces || JSON.parse(result[0])
loan.kl_visionLabels = loan.kl_visionLabels || JSON.parse(result[1])
doneCallback()
return
}
//we have stuff to do.
const req = new vision.Request({
image: new vision.Image({url: `https://www.kiva.org/img/orig/${loan.image.id}.jpg`}),
features: features
})
console.log(`Making ${features.length} Vision API requests: ${loan.id}`)
vision.annotate([req]).then(res => {
if (doFaceDetection) {
if (res.responses[0].faceAnnotations) { //wouldn't have it if didn't need it.
loan.kl_faces = processFaceData(res.responses[0].faceAnnotations)
} else {
loan.kl_faces = {joy: [], sorrow: [], anger: [], surprise: [], headwear: []}
}
rc.set(facesKey, JSON.stringify(loan.kl_faces))
rc.expire(facesKey, '2592000') //30 days
}
/**if (doVisionLabels) {
if (res.responses[0].labelAnnotations) {
loan.kl_visionLabels = res.responses[0].labelAnnotations
} else {
loan.kl_visionLabels = []
}
rc.set(vlkey, JSON.stringify(loan.kl_visionLabels))
rc.expire(vlkey, '2592000') //30 days
}**/
doneCallback()
//console.log(JSON.stringify(loan.kl_faces))
}, e => {
doneCallback(404)
console.log('Error: ', e)
})
})
}
exports.guaranteeGoogleVisionForLoan = guaranteeGoogleVisionForLoan
exports.processFaceData = processFaceData