Ubuntu

modified

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'); 23 +
10 -const HTTPS = require('https'); 24 +var _storage = multer.diskStorage({
11 -const domain = "www.oss0522test.tk" 25 + destination: function (req, file, cb) { //path - 파일의 형식에 따라 나눔 if문
12 -const sslport = 23023; 26 + cb(null, 'uploads/')
13 -const bodyParser = require('body-parser'); 27 + },
14 - 28 + filename: function (req, file, cb) { //filename 중복된 파일이 존재하는지
15 -const vision = require('@google-cloud/vision'); //구글 클라우드 비전 api 29 + cb(null, file.originalname)
16 -const line = require('@line/bot-sdk'); 30 + }
17 - 31 + })
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 -
62 -
63 -
64 -
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 - },
82 - "selected": false,
83 - "name": "Choose language",
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 - }
98 - ]
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 52
114 - var eventObj = req.body.events[0]; 53 + res.send('Uploaded' + req.file.originalname );
115 - var source = eventObj.source; 54 +})
116 - var message = eventObj.message;
117 55
118 56
119 - // request log 57 +//문자 인식 함수
120 - console.log('======================', new Date() ,'======================'); 58 +async function ImageRecognition(uploadedfilename) {
121 - console.log('[request]', req.body); 59 + const fileName = `/home/ubuntu/termproject/uploads/${uploadedfilename}`;
122 - console.log('[request source] ', eventObj.source); 60 + const [result] = await client.textDetection(fileName);
123 - console.log('[request message]', eventObj.message); 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 +}
124 66
67 +//google cloud storage bucket list 함수
68 +async function googlevisiontest(){
125 69
126 - if (eventObj.message.type == "text"){ //입력이 텍스트일 경우 70 + try {
127 - 71 + const [buckets] = await storage.getBuckets();
128 - if (eventObj.message.text == '영어'){ 72 +
129 - trans_lang = 'en'; 73 + console.log('Buckets:');
130 - }else if (eventObj.message.text == '일본어'){ 74 + buckets.forEach((bucket) => {
131 - trans_lang = 'ja'; 75 + console.log(bucket.name);
132 - }else if (eventObj.message.text == '프랑스어'){ 76 + });
133 - trans_lang = 'fr'; 77 + } catch (err) {
134 - }else { 78 + console.error('ERROR:', err);
135 - trans(eventObj.replyToken, eventObj.message.text, trans_lang); 79 + }
136 - } 80 +}
137 - 81 +//googlevisiontest();
138 - } else if (eventObj.message.type == "image"){ //입력이 이미지일 경우 82 +
139 - //이미지 저장? -> google cloud storage이나 웹 83 +//papago trans
140 - vision_iamge(bucketName, fileName); 84 +function PAPAGOtranslation (query) {
141 - //추출된 텍스트, 번역된 텍스트 85 +
142 - 86 + request.post(
87 + {
88 + url: PAPAGO_URL,
89 + form: {'source':'ko', 'target':'en', 'text':query},
90 + headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET},
91 + body: `source=ko&target=en&text=` + query,
92 + json:true
93 + }, (error, response, body) => {
94 + if(!error && response.statusCode == 200) {
95 + var transMessage = body.message.result.translatedText;
96 + console.log(transMessage);
97 + /* //라인에 전송
98 + request.post(
99 + {
100 + url: TARGET_URL,
101 + headers: {
102 + 'Authorization': `Bearer ${TOKEN}`
103 + },
104 + json: {
105 + "replyToken":replyToken,
106 + "messages":[
107 + {
108 + "type":"text",
109 + "text":transMessage
110 + }
111 + ]
112 + }
113 + },(error, response, body) => {
114 + console.log(body)
115 + }); */
143 } 116 }
144 -
145 -
146 - res.sendStatus(200);
147 }); 117 });
148 -
149 -
150 -function trans(replyToken, message, language) {
151 -
152 - request.post(
153 - {
154 - url: PAPAGO_URL,
155 - headers: {
156 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
157 - 'X-Naver-Client-Id': `${PAPAGO_ID}`,
158 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
159 - },
160 - body: `source=ko&target=${language}&text=` + message,
161 - json:true
162 - },(error, response, body) => {
163 - if(!error && response.statusCode == 200) {
164 - console.log(body.message);
165 - var transMessage = body.message.result.translatedText;
166 - request.post(
167 - {
168 - url: TARGET_URL,
169 - headers: {
170 - 'Authorization': `Bearer ${TOKEN}`
171 - },
172 - json: {
173 - "replyToken":replyToken,
174 - "messages":[
175 - {
176 - "type":"text",
177 - "text":transMessage
178 - }
179 - ]
180 - }
181 - },(error, response, body) => {
182 - console.log(body)
183 - });
184 - }
185 - });
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",
......