-
Notifications
You must be signed in to change notification settings - Fork 1
/
vision.js
111 lines (92 loc) · 2.97 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
107
108
109
110
111
'use strict'
const GoogleVision = require('@google-cloud/vision')
const debug = require('debug')('node-google-vision:Vision')
const keys = {
faceDetection: 'faceAnnotations',
safeSearchDetection: 'safeSearchAnnotation',
logoDetection: 'logoAnnotations',
labelDetection: 'labelAnnotations',
landmarkDetection: 'landmarkAnnotations',
textDetection: 'textAnnotations',
imageProperties: 'imagePropertiesAnnotation',
webDetection: 'webDetection',
documentTextDetection: 'fullTextAnnotation'
}
module.exports = class Vision {
constructor(GoogleParams) {
this.visionClient = GoogleVision(GoogleParams)
}
async doCall(endpoint, imagePath) {
return this.visionClient[endpoint](getRequestImageObject(imagePath))
.then((results) => {
if (results.length && results[0][keys[endpoint]]) {
return results[0][keys[endpoint]]
}
})
.catch((err) => {
debug('error', endpoint, err)
return err
})
}
async faceDetection(imagePath) {
return await this.doCall('faceDetection', imagePath)
}
async safeSearchDetection(imagePath) {
let results = await this.doCall('safeSearchDetection', imagePath)
for(let type in results) {
results[type] = safeSearchDetectionConfidence(results[type])
}
return results
}
async logoDetection(imagePath) {
return await this.doCall('logoDetection', imagePath)
}
async labelDetection(imagePath) {
return await this.doCall('labelDetection', imagePath)
}
async landmarkDetection(imagePath) {
return await this.doCall('landmarkDetection', imagePath)
}
async textDetection(imagePath) {
return await this.doCall('textDetection', imagePath)
}
async imageProperties(imagePath) {
return await this.doCall('imageProperties', imagePath)
}
async webDetection(imagePath) {
return await this.doCall('webDetection', imagePath)
}
async documentTextDetection(imagePath) {
return await this.doCall('documentTextDetection', imagePath)
}
}
function getRequestImageObject(imagePath) {
if (imagePath.indexOf('gs://') === 0 || imagePath.indexOf('http://') === 0 || imagePath.indexOf('https://') === 0) {
return { source: { imageUri: imagePath } }
} else {
return { source: { filename: imagePath } }
}
}
function safeSearchDetectionConfidence(value) {
let confidence = 0
switch (value) {
case 'VERY_UNLIKELY':
confidence = 0.2
break;
case 'UNLIKELY':
confidence = 0.4
break;
case 'POSSIBLE':
confidence = 0.6
break;
case 'LIKELY':
confidence = 0.8
break;
case 'VERY_LIKELY':
confidence = 1
break;
default:
break;
}
return confidence
}