-
Notifications
You must be signed in to change notification settings - Fork 0
/
rekognition.js
56 lines (42 loc) · 1.62 KB
/
rekognition.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const url = require('url')
const AWS = require('aws-sdk')
const Bluebird = require('bluebird')
const R = require('ramda')
const awsConfig = require('../config').aws
const log = require('../utils/logger')
const rekognition = new AWS.Rekognition()
const detectLabels = Bluebird.promisify(rekognition.detectLabels, { context: rekognition })
// COMMENTED OUT AS 'url.parse' HAS BEEN DEPRECATED
// const pathFromUrl = photoUrl => url.parse(photoUrl).pathname
const pathFromUrl = photoUrl => new url.URL(photoUrl).pathname
const getS3KeyFromPathname = pathName => pathName.replace(/^\/|\/$/gu, '')
const PROP_NAME = 'Name'
const PROP_VALUE = 'Dog'
const getLabels = async photoUrl => {
log.info('Getting Labels')
const nameGot = getS3KeyFromPathname(pathFromUrl(photoUrl))
log.info(`getS3KeyFromPathname: ${nameGot}`)
const params = {
Image: {
S3Object: {
Bucket: awsConfig.s3.bucketName,
Name: getS3KeyFromPathname(pathFromUrl(photoUrl)),
},
},
}
return detectLabels(params)
}
module.exports = {
isDogRecognized: async photoUrl => {
log.info('Starting isDogRecognized()')
log.info(`photoUrl: ${photoUrl}`)
const labelsResponse = await getLabels(photoUrl)
// It never passes the getLabels function, thus the following lines are not executed.
log.info(`labelsResponse: ${labelsResponse}`)
const dogLabel = R.find(R.propEq(PROP_NAME, PROP_VALUE))(labelsResponse.Labels)
log.info(`dogLabel: ${dogLabel}`)
log.info(`dogLabel.Confidence: ${dogLabel.Confidence}`)
return Boolean(dogLabel && dogLabel.Confidence > awsConfig.rekognition.minConfidence)
},
}