sckim

Use OCR API

Showing 1 changed file with 57 additions and 6 deletions
...@@ -37,6 +37,11 @@ const config = { ...@@ -37,6 +37,11 @@ const config = {
37 channelSecret: process.env.channelSecret, 37 channelSecret: process.env.channelSecret,
38 }; 38 };
39 39
40 +// Microsoft Azure - Computer Vision REST API
41 +let subscriptionKey = process.env.COMPUTER_VISION_SUBSCRIPTION_KEY
42 +let endpoint = process.env.COMPUTER_VISION_ENDPOINT
43 +if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.') }
44 +var uriBase = endpoint + 'vision/v2.1/ocr'
40 45
41 // create LINE SDK client 46 // create LINE SDK client
42 const client = new line.Client(config); 47 const client = new line.Client(config);
...@@ -63,6 +68,54 @@ app.post('/webhook', line.middleware(config), (req, res) => { ...@@ -63,6 +68,54 @@ app.post('/webhook', line.middleware(config), (req, res) => {
63 // event handler 68 // event handler
64 function handleEvent(event) { 69 function handleEvent(event) {
65 if (event.type !== 'message' || event.message.type !== 'text') { 70 if (event.type !== 'message' || event.message.type !== 'text') {
71 + if (event.message.type === 'image')
72 + return new Promise(function(resolve, reject) {
73 + console.log(event.message)
74 + const imageStream = fs.createWriteStream('public/image.jpeg')
75 + client.getMessageContent(event.message.id)
76 + .then((stream) => {
77 + stream.on('data', (chunk) => {
78 + imageStream.write(chunk)
79 + })
80 + stream.on('error', (err) => {
81 + console.log(err)
82 + })
83 + stream.on('end', () => {
84 + imageStream.end()
85 + const imageUrl = 'https://panguin.ml/image.jpeg'
86 + const params = {
87 + 'language': 'unk',
88 + 'detectOrientation': 'true',
89 + }
90 + const options = {
91 + uri: uriBase,
92 + qs: params,
93 + body: '{"url": ' + '"' + imageUrl + '"}',
94 + headers: {
95 + 'Content-Type': 'application/json',
96 + 'Ocp-Apim-Subscription-Key' : subscriptionKey
97 + }
98 + }
99 + request.post(options, (error, response, body) => {
100 + if (error) {
101 + console.log('Error: ', error)
102 + return
103 + }
104 + var lines = JSON.parse(body).regions[0].lines
105 + var detected_text = ''
106 + for (var i = 0; i < lines.length; i++) {
107 + for (var j = 0; j < lines[i].words.length; j++)
108 + detected_text += lines[i].words[j].text + ' '
109 + detected_text += '\n'
110 + }
111 + console.log(detected_text)
112 + var text_before_translation = detected_text.split('\n').join('').toLowerCase()
113 + console.log(text_before_translation)
114 + })
115 + })
116 + })
117 + })
118 + else
66 // ignore non-text-message event 119 // ignore non-text-message event
67 return Promise.resolve(null); 120 return Promise.resolve(null);
68 } 121 }
...@@ -148,21 +201,19 @@ function handleEvent(event) { ...@@ -148,21 +201,19 @@ function handleEvent(event) {
148 result.text = objBody.message.result.translatedText; 201 result.text = objBody.message.result.translatedText;
149 //번역된 문자 audio로 저장 202 //번역된 문자 audio로 저장
150 if (options.form.target == 'ko') { 203 if (options.form.target == 'ko') {
151 - var audio_options = { 204 + let audio_options = {
152 'Text': result.text, 205 'Text': result.text,
153 'OutputFormat': 'mp3', 206 'OutputFormat': 'mp3',
154 - 'VoiceId': 'Seoyeon', 207 + 'VoiceId': 'Amy',
155 "LanguageCode": 'ko-KR' 208 "LanguageCode": 'ko-KR'
156 }; 209 };
157 - console.log("korean language");
158 } else if (options.form.target == 'en') { 210 } else if (options.form.target == 'en') {
159 - var audio_options = { 211 + let audio_options = {
160 'Text': result.text, 212 'Text': result.text,
161 'OutputFormat': 'mp3', 213 'OutputFormat': 'mp3',
162 - 'VoiceId': 'Kendra', 214 + 'VoiceId': 'Amy',
163 "LanguageCode": 'en-US' 215 "LanguageCode": 'en-US'
164 }; 216 };
165 - console.log("english language");
166 } 217 }
167 Polly.synthesizeSpeech(audio_options, (err, data) => { 218 Polly.synthesizeSpeech(audio_options, (err, data) => {
168 console.log("check"); 219 console.log("check");
......