Google Cloud Vision | LABEL_DETECTION Example

This is an example to illustrate a way to utilise Google Cloud Vision API from Node-RED. To evaluate this flow, you have to enable the Google Cloud Vision API and get an API key. Please edit the Twitter node (i.e. configured to search #google by default) as you like and replace API_KEY in the Set node with your's.

The Set Node

[{"id":"38011ed4.7b0ad2","type":"twitter in","z":"46a7fc5.383dc04","twitter":"","tags":"#google","user":"false","name":"","topic":"tweets","x":80,"y":80,"wires":[["79175e4f.8f637"]]},{"id":"79175e4f.8f637","type":"function","z":"46a7fc5.383dc04","name":"If found a tweet with an image...","func":"// Ignore tweets without images\nif (typeof msg.tweet.entities.media === 'undefined') {\n    return null;\n}\n\nvar image = msg.tweet.entities.media[0];\nvar newMessage = { payload: image.media_url_https };\nnewMessage.url = image.media_url_https;\nreturn newMessage;","outputs":"1","noerr":0,"x":330,"y":80,"wires":[["53eadf47.4439c"]]},{"id":"53eadf47.4439c","type":"http request","z":"46a7fc5.383dc04","name":"Send a request to get the image","method":"GET","ret":"bin","url":"","x":330,"y":120,"wires":[["b600066.46b27f8"]]},{"id":"1789627e.253e5e","type":"http request","z":"46a7fc5.383dc04","name":"Send a request to get labels","method":"POST","ret":"obj","url":"","x":320,"y":240,"wires":[["1f6d8ae9.0fb8f5"]]},{"id":"b600066.46b27f8","type":"function","z":"46a7fc5.383dc04","name":"Make a request for Google Cloud Vision to get labels","func":"var image = {content: msg.payload.toString('base64')};\nvar features = {type: 'LABEL_DETECTION', maxResults: 3};\nvar imageContext = {languageHints: 'ja'};\nvar request = {image: image, features: features, imageContext: imageContext};\nvar requests = {requests: request};\nmsg.payload = requests;\n\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":160,"wires":[["fe37ed4d.3f0e7"]]},{"id":"fe37ed4d.3f0e7","type":"change","z":"46a7fc5.383dc04","name":"Set url and headers","rules":[{"t":"set","p":"url","pt":"msg","to":"https://vision.googleapis.com/v1/images:annotate?key=API_KEY","tot":"str"},{"t":"set","p":"headers","pt":"msg","to":"Content-Type: application/json","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":200,"wires":[["1789627e.253e5e"]]},{"id":"b9d122fb.37122","type":"debug","z":"46a7fc5.383dc04","name":"","active":true,"console":"false","complete":"payload","x":530,"y":280,"wires":[]},{"id":"d7010e16.64272","type":"comment","z":"46a7fc5.383dc04","name":"Replace with your favorite user or hashtag","info":"","x":180,"y":40,"wires":[]},{"id":"1f6d8ae9.0fb8f5","type":"function","z":"46a7fc5.383dc04","name":"Report found labels","func":"// To skip messages like { \"responses\": [ {} ] }\nif (Object.keys(msg.payload.responses[0]).length < 1) {\n    return null;\n}\n\nvar labels = 'labels: ';\nvar labelAnnotations = msg.payload.responses[0].labelAnnotations;\nfor (var i = 0; i < labelAnnotations.length; i++) {\n    labels += labelAnnotations[i].description;\n    labels +=' (' + labelAnnotations[i].score + '), ';\n}\n\nmsg.payload = labels.slice(0, labels.length - 2);\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":280,"wires":[["b9d122fb.37122"]]},{"id":"2da0f93a.083626","type":"comment","z":"46a7fc5.383dc04","name":"Replace with your API key","info":"","x":530,"y":200,"wires":[]}]
kotobuki

Flow Info

created 1 year, 7 months ago

Node Types

Core
  • change (x1)
  • comment (x2)
  • debug (x1)
  • function (x3)
  • http request (x2)
  • twitter in (x1)

Tags

  • Google
  • Vision
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option