Skip to content

Commit 3025d25

Browse files
committed
add remote image test
1 parent f13d164 commit 3025d25

File tree

7 files changed

+74
-30
lines changed

7 files changed

+74
-30
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
_client: null,
1313

1414
init(options) {
15-
__.defaults(options, {version: 'v1alpha1'})
15+
this._options = __.defaults(options, {version: 'v1alpha1'})
1616
const Endpoint = require('./lib/clients/' + options.version)
1717
var ep = new Endpoint(options)
1818
ep.google = this

lib/clients/v1alpha1.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class Client {
1010
this._options = options || {}
1111
}
1212

13+
/**
14+
* annotate
15+
*
16+
* @desc Call cloud vision API
17+
*
18+
* @param {array} requests - Parameters for request
19+
*/
1320
annotate(requests) {
1421
return new Promise((resolve, reject) => {
1522
this._buildRequests(requests).then((params) => {
@@ -19,18 +26,18 @@ class Client {
1926
method: 'POST'
2027
},
2128
params: {
22-
requests: params
29+
resource: {
30+
requests: params
31+
}
2332
},
2433
requiredParams: [],
2534
pathParams: [],
2635
context: this
2736
}
2837
createAPIRequest(parameters, (err, response) => {
2938
if (err) {
30-
console.log(err)
3139
reject(err)
3240
} else {
33-
console.log(response)
3441
resolve(response)
3542
}
3643
})

lib/models/Feature.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
'use strict'
2+
const __ = require('underscore')
23

34
class Feature {
45

5-
// FACE_DETECTION
6-
// LANDMARK_DETECTION
7-
// LOGO_DETECTION
8-
// LABEL_DETECTION
9-
// TEXT_DETECTION
10-
// SAFE_SEARCH_DETECTION
11-
12-
constructor(options) {
13-
options = options || {}
6+
constructor(type, maxResults) {
7+
const options = __.isObject(type) ? type : {
8+
type: type,
9+
maxResults: maxResults
10+
}
1411
this._type = options.type
1512
this._maxResults = options.maxResults || 10
1613
}

lib/models/Image.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict'
22
const fs = require('fs'),
3-
request = require('request')
3+
request = require('request'),
4+
__ = require('underscore')
45

56
class Image {
67

7-
constructor(options) {
8-
options = options || {}
8+
constructor(path) {
9+
const options = __.isObject(path) ? path : {
10+
path: path
11+
}
912
this._path = options.path
1013
this._url = options.url
1114
}
@@ -28,16 +31,12 @@ class Image {
2831

2932
_loadRemote() {
3033
return new Promise((resolve, reject) => {
31-
const url = unescape(this._url),
32-
bl = new BufferList()
3334
request({
34-
uri: url,
35-
responseBodyStream: bl
35+
url: this._url,
36+
encoding: null
3637
}, (err, response, body) => {
3738
if (!err && response.statusCode == 200) {
38-
var content = new Buffer(bl.toString(), 'binary').toString('base64')
39-
console.log(content)
40-
resolve(content)
39+
resolve(new Buffer(body).toString('base64'))
4140
} else {
4241
reject(err)
4342
}
@@ -48,6 +47,7 @@ class Image {
4847
build() {
4948
return new Promise((resolve, reject) => {
5049
this.load().then((cotent) => {
50+
console.log(content)
5151
resolve({content: content})
5252
})
5353
})

lib/models/Request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Request {
1717
return new Promise((resolve, reject) => {
1818
this._image.load().then((content) => {
1919
resolve({
20-
image: content,
20+
image: {content: content},
2121
features: __.map(this._features, (f) => f.build())
2222
})
2323
}).catch((e) => {

test_annotate.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
const vision = require('./index')
33

44
// init with auth
5-
vision.init({auth: 'YOUR_SERVER_KEY'})
5+
vision.init({auth: 'AIzaSyDwWH4G8Nx2Z7WrdPvVRhqQqlriMv7pFWI'})
66

77
// construct parameters
8-
const path = '/Users/tejitak/dev/temp/test1.jpg'
98
const req = new vision.Request({
10-
image: new vision.Image({path: path}),
11-
features: [new vision.Feature({type: 'FACE_DETECTION'})]
9+
image: new vision.Image('/Users/tejitak/temp/test1.jpg'),
10+
features: [
11+
new vision.Feature('FACE_DETECTION', 4),
12+
new vision.Feature('LABEL_DETECTION', 10),
13+
]
1214
})
1315

16+
// send single request
1417
vision.annotate(req).then((res) => {
15-
console.log(JSON.stringify(res))
16-
}, (e) => {
18+
// handling response
19+
console.log(JSON.stringify(res.responses))
20+
}, (e) => {
1721
console.log('Error: ', e)
1822
})

test_annotate_remote.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
const vision = require('./index')
3+
4+
// init with auth
5+
vision.init({auth: 'AIzaSyDwWH4G8Nx2Z7WrdPvVRhqQqlriMv7pFWI'})
6+
7+
// construct parameters
8+
// 1st image of request is load from local
9+
const req1 = new vision.Request({
10+
image: new vision.Image({
11+
path: '/Users/tejitak/temp/test1.jpg'
12+
}),
13+
features: [
14+
new vision.Feature('FACE_DETECTION', 4),
15+
new vision.Feature('LABEL_DETECTION', 10),
16+
]
17+
})
18+
19+
// 2nd image of request is load from Web
20+
const req2 = new vision.Request({
21+
image: new vision.Image({
22+
url: 'https://scontent-nrt1-1.cdninstagram.com/hphotos-xap1/t51.2885-15/e35/12353236_1220803437936662_68557852_n.jpg'
23+
}),
24+
features: [
25+
new vision.Feature('FACE_DETECTION', 1),
26+
new vision.Feature('LABEL_DETECTION', 10),
27+
]
28+
})
29+
30+
// send multi requests by one API call
31+
vision.annotate([req1, req2]).then((res) => {
32+
// handling response for each request
33+
console.log(JSON.stringify(res.responses))
34+
}, (e) => {
35+
console.log('Error: ', e)
36+
})

0 commit comments

Comments
 (0)