Showing
3 changed files
with
76 additions
and
157 deletions
| 1 | -var express = require('express'); | 1 | +const express = require('express'); |
| 2 | -const request = require('request'); | 2 | +const multer = require('multer'); |
| 3 | -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 3 | +const bodyParser = require('body-parser'); |
| 4 | +const session = require('express-session'); | ||
| 5 | +const util = require('util'); | ||
| 6 | +const vision = require('@google-cloud/vision'); //google cloud vision api module | ||
| 7 | +const translate = require('@google-cloud/translate').v3beta1; //google cloud translate api module | ||
| 8 | +const credentials = require('/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'); | ||
| 9 | +const client = new vision.ImageAnnotatorClient( {credentials} ); | ||
| 10 | +const {Storage} = require('@google-cloud/storage'); //google cloud translate api storage api moudle | ||
| 11 | +const projectId = 'focused-clock-277615'; | ||
| 12 | +const keyFilename = '/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'; | ||
| 13 | +const storage = new Storage({projectId, keyFilename}); | ||
| 14 | +var upload = multer({ storage: _storage}); | ||
| 15 | +var fs = require('fs'); | ||
| 16 | +var app = express(); | ||
| 17 | + | ||
| 4 | const TOKEN = 'h8xN39Zg0nRmPhRcwCfUt35UhGQvinzOSzl1VSHWPdaKaW4VDDs2bTvhvxnOH1FVzzJ4t1L0ih/uJ1idIKz8SvPin3PM4nLgt6roTaEVw105aIsZqDwlsKPS2ewb2WVbL2TK/BqVEBQ2MmHO2xFRzAdB04t89/1O/w1cDnyilFU=' | 18 | const TOKEN = 'h8xN39Zg0nRmPhRcwCfUt35UhGQvinzOSzl1VSHWPdaKaW4VDDs2bTvhvxnOH1FVzzJ4t1L0ih/uJ1idIKz8SvPin3PM4nLgt6roTaEVw105aIsZqDwlsKPS2ewb2WVbL2TK/BqVEBQ2MmHO2xFRzAdB04t89/1O/w1cDnyilFU=' |
| 5 | const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | 19 | const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' |
| 6 | const PAPAGO_ID = 'W5BknW1dTHXMah9QZ_KK' | 20 | const PAPAGO_ID = 'W5BknW1dTHXMah9QZ_KK' |
| 7 | const PAPAGO_SECRET = 'doQdf3ZMU3' | 21 | const PAPAGO_SECRET = 'doQdf3ZMU3' |
| 8 | -const fs = require('fs'); | 22 | +var request = require('request'); |
| 9 | -const path = require('path'); | ||
| 10 | -const HTTPS = require('https'); | ||
| 11 | -const domain = "www.oss0522test.tk" | ||
| 12 | -const sslport = 23023; | ||
| 13 | -const bodyParser = require('body-parser'); | ||
| 14 | - | ||
| 15 | -const vision = require('@google-cloud/vision'); //구글 클라우드 비전 api | ||
| 16 | -const line = require('@line/bot-sdk'); | ||
| 17 | - | ||
| 18 | - | ||
| 19 | -//1. 이미지 텍스트 추출 - google cloud vision api 사용 | ||
| 20 | -// 전송받은 이미지 불러오기 -> 추출 | ||
| 21 | -//2. 추출 후 바로 번역? / 텍스트만 출력? | ||
| 22 | -//3. richmenu 활용하기 | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | -/* | ||
| 27 | -var query = "언어를 감지할 문장을 입력하세요."; | ||
| 28 | -app.get('/detectLangs', function (req, res) { | ||
| 29 | - var api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | ||
| 30 | - var request = require('request'); | ||
| 31 | - var options = { | ||
| 32 | - url: api_url, | ||
| 33 | - form: {'query': query}, | ||
| 34 | - headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET} | ||
| 35 | - }; | ||
| 36 | - request.post(options, function (error, response, body) { | ||
| 37 | - if (!error && response.statusCode == 200) { | ||
| 38 | - res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'}); | ||
| 39 | - res.end(body); | ||
| 40 | - } else { | ||
| 41 | - res.status(response.statusCode).end(); | ||
| 42 | - console.log('error = ' + response.statusCode); | ||
| 43 | - } | ||
| 44 | - }); | ||
| 45 | - }); | ||
| 46 | - app.listen(3000, function () { | ||
| 47 | - console.log('http://127.0.0.1:3000/detectLangs app listening on port 3000!'); | ||
| 48 | - }); | ||
| 49 | - */ | ||
| 50 | - | ||
| 51 | -const client_vision = new vision.ImageAnnotatorClient(); | ||
| 52 | - | ||
| 53 | - //이미지에서 텍스트 추출 | ||
| 54 | -function vision_image(bucketName, fileName){ | ||
| 55 | - const [result] = await client_vision.textDetection(`gs://${bucketName}/${fileName}`); | ||
| 56 | - const detections = result.textAnnotations; | ||
| 57 | - console.log('Text:'); | ||
| 58 | - detections.forEach(text => console.log(text)); | ||
| 59 | -} | ||
| 60 | - | ||
| 61 | 23 | ||
| 62 | - | 24 | +var _storage = multer.diskStorage({ |
| 63 | - | 25 | + destination: function (req, file, cb) { //path - 파일의 형식에 따라 나눔 if문 |
| 64 | - | 26 | + cb(null, 'uploads/') |
| 65 | - | ||
| 66 | - | ||
| 67 | -const client_line = new line.Client({ | ||
| 68 | - channelAccessToken: TOKEN | ||
| 69 | -}); | ||
| 70 | - | ||
| 71 | -client_line.setRichMenuImage('<rich_menu_id>', fs.createReadStream('./example.png')) //richmenu 이미지 설정 | ||
| 72 | - | ||
| 73 | -client_line.createRichMenu(richmenu) //richmenu 만들기 | ||
| 74 | - .then((richMenuId) => | ||
| 75 | - console.log(richMenuId)) | ||
| 76 | - | ||
| 77 | -const richmenu = { | ||
| 78 | - "size": { | ||
| 79 | - "width": 2500, | ||
| 80 | - "height": 1686 | ||
| 81 | }, | 27 | }, |
| 82 | - "selected": false, | 28 | + filename: function (req, file, cb) { //filename 중복된 파일이 존재하는지 |
| 83 | - "name": "Choose language", | 29 | + cb(null, file.originalname) |
| 84 | - "chatBarText": "ko/ja/en/fr", | ||
| 85 | - "areas": [ | ||
| 86 | - { | ||
| 87 | - "bounds": { | ||
| 88 | - "x": 0, | ||
| 89 | - "y": 0, | ||
| 90 | - "width": 2500, | ||
| 91 | - "height": 1686 | ||
| 92 | - }, | ||
| 93 | - "action": { | ||
| 94 | - "type": "postback", | ||
| 95 | - "data": "action=buy&itemid=123" | ||
| 96 | - } | ||
| 97 | } | 30 | } |
| 98 | - ] | 31 | + }) |
| 99 | -}; | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | 32 | ||
| 33 | +app.use(bodyParser.urlencoded({ extended: false })); | ||
| 34 | +app.locals.pretty = true; | ||
| 35 | +app.set('views', './views_file'); | ||
| 36 | +app.set('view engine', 'jade'); | ||
| 104 | 37 | ||
| 38 | +app.use('/user', express.static('uploads')); | ||
| 105 | 39 | ||
| 106 | 40 | ||
| 41 | +//파일 업로드 | ||
| 42 | +app.get('/upload', function(req, res){ | ||
| 43 | + res.render('upload'); | ||
| 44 | +}) | ||
| 107 | 45 | ||
| 46 | +//업로드된 파일 받기 | ||
| 47 | +app.post('/upload', upload.single('userfile'), function(req, res){ | ||
| 48 | + console.log(req.file); | ||
| 108 | 49 | ||
| 109 | -var app = express(); | 50 | + ImageRecognition(req.file.originalname); //사진에서 문자 인식 |
| 110 | -app.use(bodyParser.json()); | 51 | + //PAPAGOtranslation(); |
| 111 | -var trans_lang; | ||
| 112 | -app.post('/hook', function (req, res) { | ||
| 113 | - | ||
| 114 | - var eventObj = req.body.events[0]; | ||
| 115 | - var source = eventObj.source; | ||
| 116 | - var message = eventObj.message; | ||
| 117 | - | ||
| 118 | 52 | ||
| 119 | - // request log | 53 | + res.send('Uploaded' + req.file.originalname ); |
| 120 | - console.log('======================', new Date() ,'======================'); | 54 | +}) |
| 121 | - console.log('[request]', req.body); | ||
| 122 | - console.log('[request source] ', eventObj.source); | ||
| 123 | - console.log('[request message]', eventObj.message); | ||
| 124 | 55 | ||
| 125 | 56 | ||
| 126 | - if (eventObj.message.type == "text"){ //입력이 텍스트일 경우 | 57 | +//문자 인식 함수 |
| 58 | +async function ImageRecognition(uploadedfilename) { | ||
| 59 | + const fileName = `/home/ubuntu/termproject/uploads/${uploadedfilename}`; | ||
| 60 | + const [result] = await client.textDetection(fileName); | ||
| 61 | + console.log('Text: '+ result.fullTextAnnotation.text); | ||
| 62 | + PAPAGOtranslation (result.fullTextAnnotation.text); //번역 | ||
| 63 | + //const detections = result.textAnnotations; | ||
| 64 | + //detections.forEach(text => console.log(text)); | ||
| 65 | +} | ||
| 127 | 66 | ||
| 128 | - if (eventObj.message.text == '영어'){ | 67 | +//google cloud storage bucket list 함수 |
| 129 | - trans_lang = 'en'; | 68 | +async function googlevisiontest(){ |
| 130 | - }else if (eventObj.message.text == '일본어'){ | ||
| 131 | - trans_lang = 'ja'; | ||
| 132 | - }else if (eventObj.message.text == '프랑스어'){ | ||
| 133 | - trans_lang = 'fr'; | ||
| 134 | - }else { | ||
| 135 | - trans(eventObj.replyToken, eventObj.message.text, trans_lang); | ||
| 136 | - } | ||
| 137 | 69 | ||
| 138 | - } else if (eventObj.message.type == "image"){ //입력이 이미지일 경우 | 70 | + try { |
| 139 | - //이미지 저장? -> google cloud storage이나 웹 | 71 | + const [buckets] = await storage.getBuckets(); |
| 140 | - vision_iamge(bucketName, fileName); | ||
| 141 | - //추출된 텍스트, 번역된 텍스트 | ||
| 142 | 72 | ||
| 73 | + console.log('Buckets:'); | ||
| 74 | + buckets.forEach((bucket) => { | ||
| 75 | + console.log(bucket.name); | ||
| 76 | + }); | ||
| 77 | + } catch (err) { | ||
| 78 | + console.error('ERROR:', err); | ||
| 143 | } | 79 | } |
| 80 | +} | ||
| 81 | +//googlevisiontest(); | ||
| 144 | 82 | ||
| 145 | - | 83 | +//papago trans |
| 146 | - res.sendStatus(200); | 84 | +function PAPAGOtranslation (query) { |
| 147 | -}); | ||
| 148 | - | ||
| 149 | - | ||
| 150 | -function trans(replyToken, message, language) { | ||
| 151 | 85 | ||
| 152 | request.post( | 86 | request.post( |
| 153 | { | 87 | { |
| 154 | url: PAPAGO_URL, | 88 | url: PAPAGO_URL, |
| 155 | - headers: { | 89 | + form: {'source':'ko', 'target':'en', 'text':query}, |
| 156 | - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | 90 | + headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}, |
| 157 | - 'X-Naver-Client-Id': `${PAPAGO_ID}`, | 91 | + body: `source=ko&target=en&text=` + query, |
| 158 | - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
| 159 | - }, | ||
| 160 | - body: `source=ko&target=${language}&text=` + message, | ||
| 161 | json:true | 92 | json:true |
| 162 | - },(error, response, body) => { | 93 | + }, (error, response, body) => { |
| 163 | if(!error && response.statusCode == 200) { | 94 | if(!error && response.statusCode == 200) { |
| 164 | - console.log(body.message); | ||
| 165 | var transMessage = body.message.result.translatedText; | 95 | var transMessage = body.message.result.translatedText; |
| 96 | + console.log(transMessage); | ||
| 97 | + /* //라인에 전송 | ||
| 166 | request.post( | 98 | request.post( |
| 167 | { | 99 | { |
| 168 | url: TARGET_URL, | 100 | url: TARGET_URL, |
| ... | @@ -180,23 +112,9 @@ function trans(replyToken, message, language) { | ... | @@ -180,23 +112,9 @@ function trans(replyToken, message, language) { |
| 180 | } | 112 | } |
| 181 | },(error, response, body) => { | 113 | },(error, response, body) => { |
| 182 | console.log(body) | 114 | console.log(body) |
| 183 | - }); | 115 | + }); */ |
| 184 | } | 116 | } |
| 185 | - }); | 117 | +}); |
| 186 | - | ||
| 187 | } | 118 | } |
| 188 | 119 | ||
| 189 | -try { | ||
| 190 | - const option = { | ||
| 191 | - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
| 192 | - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
| 193 | - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
| 194 | - }; | ||
| 195 | - | ||
| 196 | - HTTPS.createServer(option, app).listen(sslport, () => { | ||
| 197 | - console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
| 198 | - }); | ||
| 199 | - } catch (error) { | ||
| 200 | - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
| 201 | - console.log(error); | ||
| 202 | - } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 120 | + var server = app.listen(23023); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
This diff is collapsed. Click to expand it.
| ... | @@ -4,9 +4,9 @@ | ... | @@ -4,9 +4,9 @@ |
| 4 | "description": "photo", | 4 | "description": "photo", |
| 5 | "main": "app.js", | 5 | "main": "app.js", |
| 6 | "dependencies": { | 6 | "dependencies": { |
| 7 | - "@google-cloud/storage": "^5.1.0", | 7 | + "@google-cloud/storage": "^5.1.1", |
| 8 | "@google-cloud/translate": "^6.0.0", | 8 | "@google-cloud/translate": "^6.0.0", |
| 9 | - "@google-cloud/vision": "^2.0.0", | 9 | + "@google-cloud/vision": "^2.1.0", |
| 10 | "axios": "^0.19.2", | 10 | "axios": "^0.19.2", |
| 11 | "body-parser": "^1.19.0", | 11 | "body-parser": "^1.19.0", |
| 12 | "core-js": "^3.6.5", | 12 | "core-js": "^3.6.5", |
| ... | @@ -17,7 +17,8 @@ | ... | @@ -17,7 +17,8 @@ |
| 17 | "jade": "^1.11.0", | 17 | "jade": "^1.11.0", |
| 18 | "multer": "^1.4.2", | 18 | "multer": "^1.4.2", |
| 19 | "pug": "^3.0.0", | 19 | "pug": "^3.0.0", |
| 20 | - "request": "^2.88.2" | 20 | + "request": "^2.88.2", |
| 21 | + "util": "^0.12.3" | ||
| 21 | }, | 22 | }, |
| 22 | "repository field": { | 23 | "repository field": { |
| 23 | "type": "http", | 24 | "type": "http", | ... | ... |
-
Please register or login to post a comment