sckim

Use OCR API

Showing 1 changed file with 59 additions and 8 deletions
......@@ -37,6 +37,11 @@ const config = {
channelSecret: process.env.channelSecret,
};
// Microsoft Azure - Computer Vision REST API
let subscriptionKey = process.env.COMPUTER_VISION_SUBSCRIPTION_KEY
let endpoint = process.env.COMPUTER_VISION_ENDPOINT
if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.') }
var uriBase = endpoint + 'vision/v2.1/ocr'
// create LINE SDK client
const client = new line.Client(config);
......@@ -63,8 +68,56 @@ app.post('/webhook', line.middleware(config), (req, res) => {
// event handler
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
// ignore non-text-message event
return Promise.resolve(null);
if (event.message.type === 'image')
return new Promise(function(resolve, reject) {
console.log(event.message)
const imageStream = fs.createWriteStream('public/image.jpeg')
client.getMessageContent(event.message.id)
.then((stream) => {
stream.on('data', (chunk) => {
imageStream.write(chunk)
})
stream.on('error', (err) => {
console.log(err)
})
stream.on('end', () => {
imageStream.end()
const imageUrl = 'https://panguin.ml/image.jpeg'
const params = {
'language': 'unk',
'detectOrientation': 'true',
}
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
}
request.post(options, (error, response, body) => {
if (error) {
console.log('Error: ', error)
return
}
var lines = JSON.parse(body).regions[0].lines
var detected_text = ''
for (var i = 0; i < lines.length; i++) {
for (var j = 0; j < lines[i].words.length; j++)
detected_text += lines[i].words[j].text + ' '
detected_text += '\n'
}
console.log(detected_text)
var text_before_translation = detected_text.split('\n').join('').toLowerCase()
console.log(text_before_translation)
})
})
})
})
else
// ignore non-text-message event
return Promise.resolve(null);
}
// 검색 기능
......@@ -148,21 +201,19 @@ function handleEvent(event) {
result.text = objBody.message.result.translatedText;
//번역된 문자 audio로 저장
if (options.form.target == 'ko') {
var audio_options = {
let audio_options = {
'Text': result.text,
'OutputFormat': 'mp3',
'VoiceId': 'Seoyeon',
'VoiceId': 'Amy',
"LanguageCode": 'ko-KR'
};
console.log("korean language");
} else if (options.form.target == 'en') {
var audio_options = {
let audio_options = {
'Text': result.text,
'OutputFormat': 'mp3',
'VoiceId': 'Kendra',
'VoiceId': 'Amy',
"LanguageCode": 'en-US'
};
console.log("english language");
}
Polly.synthesizeSpeech(audio_options, (err, data) => {
console.log("check");
......