@@ -9,33 +9,36 @@ admin.initializeApp(functions.config().firebase);
9
9
import * as vision from '@google-cloud/vision' ;
10
10
const visionClient = new vision . ImageAnnotatorClient ( ) ;
11
11
12
+ // Dedicated bucket for cloud function invocation
12
13
const bucketName = 'firestarter-96e46-vision' ;
13
14
14
-
15
-
16
15
export const imageTagger = functions . storage
17
16
18
17
. bucket ( bucketName )
19
18
. object ( )
20
19
. onChange ( async event => {
21
20
21
+ // File data
22
22
const object = event . data ;
23
23
const filePath = object . name ;
24
24
25
+ // Location of saved file in bucket
25
26
const imageUri = `gs://${ bucketName } /${ filePath } ` ;
26
27
28
+ // Firestore docID === file name
27
29
const docId = filePath . split ( '.jpg' ) [ 0 ] ;
28
- console . log ( 2 , docId )
30
+
29
31
const docRef = admin . firestore ( ) . collection ( 'photos' ) . doc ( docId ) ;
30
-
31
32
33
+ // Await the cloud vision response
32
34
const results = await visionClient . labelDetection ( imageUri ) ;
33
- const labels = results [ 0 ] . labelAnnotations . map ( obj => obj . description ) ;
34
35
35
- console . log ( 3 , results )
36
+ // Map the data to desired format
37
+ const labels = results [ 0 ] . labelAnnotations . map ( obj => obj . description ) ;
38
+ const hotdog = labels . includes ( 'hot dog' )
36
39
37
40
38
- return docRef . set ( { labels } )
41
+ return docRef . set ( { hotdog , labels } )
39
42
40
43
41
44
} ) ;
0 commit comments