김현기

aws rekognition으로 사진 분석 성공

Showing 1 changed file with 48 additions and 3 deletions
...@@ -13,9 +13,9 @@ var bucketName = "kindofyourdogimage"; ...@@ -13,9 +13,9 @@ var bucketName = "kindofyourdogimage";
13 // s3 버킷의 엔드 포인트 13 // s3 버킷의 엔드 포인트
14 var bucketRegion = 'ap-northeast-2'; 14 var bucketRegion = 'ap-northeast-2';
15 // access key 15 // access key
16 -var accessId= 'AKIAQVXKGU466IQYEGRN'; 16 +var accessId= 'your_accessId';
17 // access secret key 17 // access secret key
18 -var secretKey = '0FrMPdZR6+AkMkabAyPZWrnsVVi9EaI9/IdrWKCm'; 18 +var secretKey = 'your_secretKey';
19 // AWS Cognito 인증 19 // AWS Cognito 인증
20 //var identityPoolId = "ap-northeast-2:7cba9a17-588b-40d6-8c70-eb8ba4d573be"; 20 //var identityPoolId = "ap-northeast-2:7cba9a17-588b-40d6-8c70-eb8ba4d573be";
21 21
...@@ -137,17 +137,62 @@ app.get('/form', function(req, res){ ...@@ -137,17 +137,62 @@ app.get('/form', function(req, res){
137 </html> 137 </html>
138 `; 138 `;
139 res.send(output); 139 res.send(output);
140 + console.log(output);
140 }); 141 });
141 142
142 // image를 받았을 때 143 // image를 받았을 때
143 app.post('/upload_receiver', function(req,res){ 144 app.post('/upload_receiver', function(req,res){
144 var form = new formidable.IncomingForm(); 145 var form = new formidable.IncomingForm();
146 + console.log(form);
147 + const client = new AWS.Rekognition();
148 +
149 + const params = {
150 + "Image": {
151 + "S3Object": {
152 + "Bucket": bucketName,
153 + "Name": fileName
154 + }
155 + },
156 + "MaxLabels": 10,
157 + "MinConfidence": 75
158 + }
159 +
160 + // 이미지 분석하기
161 + client.detectLabels(params, function(err, response) {
162 + if (err) {
163 + console.log(err, err.stack); // an error occurred
164 + } else {
165 + console.log(`Detected labels for: ${form.name}`)
166 + response.Labels.forEach(label => {
167 + console.log(`Label: ${label.Name}`)
168 + console.log(`Confidence: ${label.Confidence}`)
169 + console.log("Instances:")
170 + label.Instances.forEach(instance => {
171 + let box = instance.BoundingBox
172 + console.log(" Bounding box:")
173 + console.log(` Top: ${box.Top}`)
174 + console.log(` Left: ${box.Left}`)
175 + console.log(` Width: ${box.Width}`)
176 + console.log(` Height: ${box.Height}`)
177 + console.log(` Confidence: ${instance.Confidence}`)
178 + })
179 + console.log("Parents:")
180 + label.Parents.forEach(parent => {
181 + console.log(` ${parent.Name}`)
182 + })
183 + console.log("------------")
184 + console.log("")
185 + }) // for response.labels
186 + } // if
187 + });
188 +
189 + // S3에 upload해주기
145 form.parse(req, function(err, fields, files){ 190 form.parse(req, function(err, fields, files){
146 var s3 = new AWS.S3(); 191 var s3 = new AWS.S3();
147 var params = { 192 var params = {
148 Bucket:bucketName, 193 Bucket:bucketName,
149 ACL:'public-read', 194 ACL:'public-read',
150 - Key:files.userfile.namem, 195 + Key:files.userfile.name,
151 Body: require('fs').createReadStream(files.userfile.path) 196 Body: require('fs').createReadStream(files.userfile.path)
152 } 197 }
153 s3.upload(params, function(err, data){ 198 s3.upload(params, function(err, data){
......