File tree Expand file tree Collapse file tree 7 files changed +74
-30
lines changed Expand file tree Collapse file tree 7 files changed +74
-30
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ module.exports = {
12
12
_client : null ,
13
13
14
14
init ( options ) {
15
- __ . defaults ( options , { version : 'v1alpha1' } )
15
+ this . _options = __ . defaults ( options , { version : 'v1alpha1' } )
16
16
const Endpoint = require ( './lib/clients/' + options . version )
17
17
var ep = new Endpoint ( options )
18
18
ep . google = this
Original file line number Diff line number Diff line change @@ -10,6 +10,13 @@ class Client {
10
10
this . _options = options || { }
11
11
}
12
12
13
+ /**
14
+ * annotate
15
+ *
16
+ * @desc Call cloud vision API
17
+ *
18
+ * @param {array } requests - Parameters for request
19
+ */
13
20
annotate ( requests ) {
14
21
return new Promise ( ( resolve , reject ) => {
15
22
this . _buildRequests ( requests ) . then ( ( params ) => {
@@ -19,18 +26,18 @@ class Client {
19
26
method : 'POST'
20
27
} ,
21
28
params : {
22
- requests : params
29
+ resource : {
30
+ requests : params
31
+ }
23
32
} ,
24
33
requiredParams : [ ] ,
25
34
pathParams : [ ] ,
26
35
context : this
27
36
}
28
37
createAPIRequest ( parameters , ( err , response ) => {
29
38
if ( err ) {
30
- console . log ( err )
31
39
reject ( err )
32
40
} else {
33
- console . log ( response )
34
41
resolve ( response )
35
42
}
36
43
} )
Original file line number Diff line number Diff line change 1
1
'use strict'
2
+ const __ = require ( 'underscore' )
2
3
3
4
class Feature {
4
5
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
+ }
14
11
this . _type = options . type
15
12
this . _maxResults = options . maxResults || 10
16
13
}
Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
const fs = require ( 'fs' ) ,
3
- request = require ( 'request' )
3
+ request = require ( 'request' ) ,
4
+ __ = require ( 'underscore' )
4
5
5
6
class Image {
6
7
7
- constructor ( options ) {
8
- options = options || { }
8
+ constructor ( path ) {
9
+ const options = __ . isObject ( path ) ? path : {
10
+ path : path
11
+ }
9
12
this . _path = options . path
10
13
this . _url = options . url
11
14
}
@@ -28,16 +31,12 @@ class Image {
28
31
29
32
_loadRemote ( ) {
30
33
return new Promise ( ( resolve , reject ) => {
31
- const url = unescape ( this . _url ) ,
32
- bl = new BufferList ( )
33
34
request ( {
34
- uri : url ,
35
- responseBodyStream : bl
35
+ url : this . _url ,
36
+ encoding : null
36
37
} , ( err , response , body ) => {
37
38
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' ) )
41
40
} else {
42
41
reject ( err )
43
42
}
@@ -48,6 +47,7 @@ class Image {
48
47
build ( ) {
49
48
return new Promise ( ( resolve , reject ) => {
50
49
this . load ( ) . then ( ( cotent ) => {
50
+ console . log ( content )
51
51
resolve ( { content : content } )
52
52
} )
53
53
} )
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ class Request {
17
17
return new Promise ( ( resolve , reject ) => {
18
18
this . _image . load ( ) . then ( ( content ) => {
19
19
resolve ( {
20
- image : content ,
20
+ image : { content : content } ,
21
21
features : __ . map ( this . _features , ( f ) => f . build ( ) )
22
22
} )
23
23
} ) . catch ( ( e ) => {
Original file line number Diff line number Diff line change 2
2
const vision = require ( './index' )
3
3
4
4
// init with auth
5
- vision . init ( { auth : 'YOUR_SERVER_KEY ' } )
5
+ vision . init ( { auth : 'AIzaSyDwWH4G8Nx2Z7WrdPvVRhqQqlriMv7pFWI ' } )
6
6
7
7
// construct parameters
8
- const path = '/Users/tejitak/dev/temp/test1.jpg'
9
8
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
+ ]
12
14
} )
13
15
16
+ // send single request
14
17
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 ) => {
17
21
console . log ( 'Error: ' , e )
18
22
} )
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments