은승우

ocr com?

Showing 1 changed file with 53 additions and 2 deletions
...@@ -80,8 +80,59 @@ function handleEvent(event) { ...@@ -80,8 +80,59 @@ function handleEvent(event) {
80 } 80 }
81 else if (event.type == 'message'&& event.message.type == 'image') { 81 else if (event.type == 'message'&& event.message.type == 'image') {
82 return new Promise((resolve,reject)=>{ 82 return new Promise((resolve,reject)=>{
83 - var upload = multer({ dest: 'photo/' }) 83 + var cheerio = require('cheerio');
84 - upload.single(event.message.image); 84 +
85 + let subscriptionKey = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'];
86 + let endpoint = process.env['COMPUTER_VISION_ENDPOINT'];
87 + var uriBase = endpoint + 'vision/v2.1/ocr';
88 + var options = {
89 + uri: uriBase,
90 + qs: {
91 + visualFeatures: 'Categories',
92 + details: '',
93 + language: 'en'
94 + },
95 + headers: {
96 + 'Content-Type': 'application/octet-stream',
97 + 'Ocp-Apim-Subscription-Key': subscriptionKey
98 + },
99 + body: fs.readFileSync(event.message.image)
100 + };
101 + request.post(options, function (error, response, body) {
102 + var data=JSON.stringify(body);
103 + var text='';
104 + while(data.indexOf('text\\')!=-1)
105 + {
106 + data=data.substring(data.indexOf('text\\')+9);
107 + text+=data.substring(0,data.indexOf("\\"))+" ";
108 + }
109 + console.log(text);
110 + text="나의모든날들";
111 + console.log(text);
112 + var url="https://www.genie.co.kr/search/searchLyrics?query="+ encodeURI(text);
113 + request(url, function(error, response, html){
114 + var $ = cheerio.load(html);
115 + const $bodyList= $('#body-content > div.search_lyrics > div.music-list-wrap.type-lyrics > table > tbody > tr');
116 +
117 + var songList=[];
118 + $bodyList.each(function(i, elem){
119 + if(i<20){
120 + songList.push({
121 + singer: $(this).find("td.info").find("a.artist.ellipsis").text().trim(),
122 + song: $(this).find("td.info").find("a.title.ellipsis").text().trim(),
123 + });
124 +
125 + }
126 + })
127 + var result='';
128 + for(var i=0;i<songList.length;i++){
129 + if(songList[i].singer!=''){
130 + result+=songList[i].singer+", "+songList[i].song+"\n";
131 + }
132 + }
133 + client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
134 + });
135 + });
85 136
86 }) 137 })
87 } 138 }
......