Google Cloud Vision API | LABEL_DETECTION on a Raspberry Pi with a Webcam
This is an example to illustrate a way to utilize Google Cloud Vision API from Node-RED on a Raspberry Pi with a webcam.
To evaluate this flow, please follow the steps below:
- Set up a Google Cloud Vision API project
- Set up an API key
- Replace the
API_KEY
string in the Set node with your's
Setting LABEL_DETECTION
executes Image Content Analysis on the entire image and return. Feel free to try with other feature types such as:
Feature Type | Description |
---|---|
TEXT_DETECTION |
Perform Optical Character Recognition (OCR) on text within the image |
FACE_DETECTION |
Detect faces within the image |
LOGO_DETECTION |
Detect company logos within the image |
[{"id":"b4041e8f.62d","type":"rpi-gpio in","z":"4957373c.57fd98","name":"GPIO1 - BCM18","pin":"12","intype":"tri","debounce":"25","read":true,"x":140,"y":80,"wires":[["372f366d.b3944a"]]},{"id":"49ae4a89.d604e4","type":"exec","z":"4957373c.57fd98","command":"fswebcam -r 640x480 /home/pi/Pictures/image.jpg","addpay":true,"append":"","useSpawn":"","name":"Take a picture with the first camera and save as image.jpg in /home/pi/Pictures/","x":340,"y":160,"wires":[["47f13ee1.9a22"],[],[]]},{"id":"47f13ee1.9a22","type":"file in","z":"4957373c.57fd98","name":"Load the image file","filename":"/home/pi/Pictures/image.jpg","format":"","x":150,"y":200,"wires":[["b990a8a7.acd6e8"]]},{"id":"2eff6fea.0fc58","type":"http request","z":"4957373c.57fd98","name":"Send a request to get labels","method":"POST","ret":"obj","url":"","x":180,"y":340,"wires":[["d8950b07.fd9698"]]},{"id":"b990a8a7.acd6e8","type":"function","z":"4957373c.57fd98","name":"Make a request for the Google Cloud Vision API 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":280,"y":260,"wires":[["c16d905.25e967"]]},{"id":"c16d905.25e967","type":"change","z":"4957373c.57fd98","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":150,"y":300,"wires":[["2eff6fea.0fc58"]]},{"id":"d8950b07.fd9698","type":"function","z":"4957373c.57fd98","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":150,"y":380,"wires":[["a6e69d94.bd809"]]},{"id":"52de210b.57645","type":"comment","z":"4957373c.57fd98","name":"Replace 'API_KEY' with your API key","info":"","x":410,"y":300,"wires":[]},{"id":"372f366d.b3944a","type":"switch","z":"4957373c.57fd98","name":"Choose rising edges (ignore falling edges)","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","outputs":1,"x":400,"y":80,"wires":[["49ae4a89.d604e4"]]},{"id":"e746f00a.06dea","type":"inject","z":"4957373c.57fd98","name":"Trigger","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":40,"wires":[["372f366d.b3944a"]]},{"id":"5714bb54.3b7704","type":"comment","z":"4957373c.57fd98","name":"Use this or a physical button connected to the pin","info":"","x":370,"y":40,"wires":[]},{"id":"39b9e997.d5a1b6","type":"comment","z":"4957373c.57fd98","name":"Replace LABEL_DETECTION with another feature type if needed","info":"","x":750,"y":260,"wires":[]},{"id":"a6e69d94.bd809","type":"debug","z":"4957373c.57fd98","name":"","active":true,"console":"false","complete":"false","x":130,"y":420,"wires":[]}]