-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathawsAIServices.js
127 lines (110 loc) · 2.77 KB
/
awsAIServices.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
// AWS AI Services helper for AWS AI Services Chrome Extension
// Ceyhun OZGUN
// August 2018
// https://github.com/ceyhunozgun/awsAIChromeExtension
var AWSAIServices = (function() {
var polly;
var rekognition;
var translate;
var voices = {
'en': 'Joanna',
'es': 'Penelope',
'tr': 'Filiz',
'fr': 'Lea',
'de': 'Vicki',
'it': 'Carla'
};
function initAWS(accessKeyId, secretAccessKey, region) {
AWS.config.region = region;
AWS.config.accessKeyId = accessKeyId;
AWS.config.secretAccessKey = secretAccessKey;
}
function initPolly(region) {
polly = new AWS.Polly({ region: region});
}
function initRekognition(region) {
rekognition = new AWS.Rekognition({ region: region});
}
function initTranslate(region) {
translate = new AWS.Translate({ region: region});
}
function synthesizeSpeech(txt, lang, callback) {
lang = lang || 'en';
var params = {
OutputFormat: 'mp3',
Text: txt,
VoiceId: voices[lang]
};
polly.synthesizeSpeech(params, function(err, data) {
if (err)
callback(err, null);
else
callback(null, data.AudioStream);
});
}
function translateText(txt, sourceLangCode, targetLangCode, callback) {
var params = {
Text: txt,
SourceLanguageCode: sourceLangCode,
TargetLanguageCode: targetLangCode,
};
translate.translateText(params, function (err, data) {
if (err)
callback(err, data);
else
callback(null, data.TranslatedText);
});
}
function detectTextInImage(blob, callback) {
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function() {
var arrayBuffer = this.result;
var params = {
Image: {
Bytes: arrayBuffer
}
};
rekognition.detectText(params, function(err, data) {
if (err) {
console.log(err, err.stack);
callback(err, null);
}
else {
var text = '';
for (var i = 0; i < data.TextDetections.length; i++) {
var td = data.TextDetections[i];
if (td.Type == "LINE")
text += td.DetectedText + "\n";
}
callback(null, text);
}
});
};
fileReader.readAsArrayBuffer(blob);
}
function initAWSServices(key, secret, region) {
initAWS(key, secret, region);
initPolly(region);
initRekognition(region);
initTranslate(region);
}
return {
// Initialization services
init: function(key, secret, region) {
initAWSServices(key, secret, region);
},
// Polly services
synthesizeSpeech : function (txt, lang, callback) {
synthesizeSpeech(txt, lang, callback);
},
// Translate services
translateText : function(txt, sourceLangCode, targetLangCode, callback) {
translateText(txt, sourceLangCode, targetLangCode, callback);
},
// Rekognition services
detectTextInImage : function (blob, callback) {
detectTextInImage(blob, callback);
}
};
})();