Field Hack

Cloud Vision API

[{"id":"b9814cc7.854288","type":"template","z":"cec74554.ecbe","name":"JSON template","field":"payloadrule","fieldType":"msg","format":"json","syntax":"plain","template":"{\n  \"requests\":[\n    {\n      \"image\":{\n        \"content\":\"base64-encoded-file-data\"\n      },\n      \"features\":[\n        {\n          \"type\":\"TEXT_DETECTION\",\n          \"maxResults\":1\n        }\n      ],\n      \"imageContext\": {\n          \"languageHints\": [\n            \"en\"\n          ]\n      }\n    }\n  ]\n}","x":364,"y":320,"wires":[["75f29394.9ec6ac"]]},{"id":"f1d904d.52a44f8","type":"http in","z":"cec74554.ecbe","name":"","url":"/gcvuploaderpost","method":"post","swaggerDoc":"","x":166,"y":194,"wires":[["beda337a.453d58","263728d3.a93128"]]},{"id":"10e643e0.6e26c4","type":"http response","z":"cec74554.ecbe","name":"","x":746,"y":660,"wires":[]},{"id":"beda337a.453d58","type":"debug","z":"cec74554.ecbe","name":"","active":false,"console":"false","complete":"false","x":430,"y":196.5,"wires":[]},{"id":"52b05767.7efdc","type":"debug","z":"cec74554.ecbe","name":"","active":false,"console":"false","complete":"false","x":773.5,"y":384,"wires":[]},{"id":"30dbd84c.f0123","type":"http in","z":"cec74554.ecbe","name":"","url":"/gcvup","method":"get","swaggerDoc":"","x":137,"y":100,"wires":[["5ddb1373.73dc84"]]},{"id":"5ddb1373.73dc84","type":"template","z":"cec74554.ecbe","name":"form","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n<head>\n<meta name=\"viewport\" content=\"user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;\" />\n</head>\n<body>\n<div class=\"form\">\nUpload Photo\n<form method=\"post\" action=\"gcvuploaderpost\" enctype=\"multipart/form-data\">\n<p><input type=\"file\" name=\"example5\" accept=\"image/jpeg\"></p>\n<p><input type=\"submit\"></p>\n</div>\n</form>\n</body>\n</html>","x":320,"y":100,"wires":[["d7180415.b6842"]]},{"id":"d7180415.b6842","type":"http response","z":"cec74554.ecbe","name":"","x":525,"y":100,"wires":[]},{"id":"9737bc4f.a5563","type":"comment","z":"cec74554.ecbe","name":"Uploader form","info":"","x":138,"y":52,"wires":[]},{"id":"d7af09e6.5de05","type":"debug","z":"cec74554.ecbe","name":"","active":false,"console":"false","complete":"payloadrule","x":794,"y":427,"wires":[]},{"id":"75f29394.9ec6ac","type":"change","z":"cec74554.ecbe","name":"request body json maker","rules":[{"t":"change","p":"payloadrule","pt":"msg","from":"base64-encoded-file-data","fromt":"str","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":387,"y":428,"wires":[["d7af09e6.5de05","22e577fa.d12da8"]]},{"id":"2a6b9a52.d2b906","type":"http request","z":"cec74554.ecbe","name":"","method":"POST","ret":"obj","url":"","x":355,"y":520,"wires":[["3b9e7b8c.b3ee14","5696f75c.63a58"]]},{"id":"22e577fa.d12da8","type":"change","z":"cec74554.ecbe","name":"http request maker","rules":[{"t":"set","p":"payload","pt":"msg","to":"payloadrule","tot":"msg"},{"t":"set","p":"url","pt":"msg","to":"https://vision.googleapis.com/v1/images:annotate\\?key=YOUR_GOOGLE_APIKey","tot":"str"},{"t":"set","p":"headers","pt":"msg","to":"\"Content-Type: application/json\"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":372,"y":474,"wires":[["2b488937.9b9246","2a6b9a52.d2b906"]]},{"id":"2b488937.9b9246","type":"debug","z":"cec74554.ecbe","name":"","active":false,"console":"false","complete":"true","x":756,"y":470,"wires":[]},{"id":"3b9e7b8c.b3ee14","type":"debug","z":"cec74554.ecbe","name":"","active":true,"console":"false","complete":"false","x":777,"y":516,"wires":[]},{"id":"22fe864f.d46c6a","type":"debug","z":"cec74554.ecbe","name":"","active":true,"console":"false","complete":"payload","x":759,"y":595,"wires":[]},{"id":"5696f75c.63a58","type":"function","z":"cec74554.ecbe","name":"","func":"msg.payload = msg.payload.responses[0].textAnnotations[0].description\nreturn msg;","outputs":1,"noerr":0,"x":557.5,"y":596.5,"wires":[["22fe864f.d46c6a","10e643e0.6e26c4"]]},{"id":"a7be63d8.da6318","type":"comment","z":"cec74554.ecbe","name":"API post","info":"","x":128.5,"y":150.5,"wires":[]},{"id":"7eaa7e3c.e2ade8","type":"comment","z":"cec74554.ecbe","name":"check your languageHints ->","info":"","x":129,"y":319,"wires":[]},{"id":"952ae6c7.5810f8","type":"comment","z":"cec74554.ecbe","name":"check your APIkey in msg.url ->","info":"https://cloud.google.com/vision/docs/getting-started#setting_up_an_api_key","x":135,"y":473,"wires":[]},{"id":"263728d3.a93128","type":"function","z":"cec74554.ecbe","name":"multipart splitter (file in first part only)","func":"var mpart=new Buffer(msg.payload.length);\nvar bpart=new Buffer(msg.payload.length);\n\n// skip header to bpart (after \\r\\n\\r\\n)\nvar spos=0;\nfor(var i=0; i<msg.payload.length; i++) {\n    mpart[i]=msg.payload.readInt8(i);\n    if(mpart[i]==10 && mpart[i-1]==13 && mpart[i-2]==10 && mpart[i-3]==13) {\n        spos=i+1;\n    }\n}\nmpart.copy(bpart,0,spos);\n\n// drop fooder to msg.payload (before \\r\\n--)\nvar epos=0;\nfor(var i=0; i<bpart.length; i++) {\n    mpart[i]=bpart.readInt8(i);\n    if(mpart[i]==45 && mpart[i-1]==45 && mpart[i-2]==10 && mpart[i-3]==13) {\n        epos=i-3;\n        break;\n    }\n}\n\nmpart.copy(bpart,0,0,epos);\nmsg.payload=bpart.toString('base64');\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":430,"y":269,"wires":[["eb81fd9d.aa5598","b9814cc7.854288"]]},{"id":"eb81fd9d.aa5598","type":"debug","z":"cec74554.ecbe","name":"","active":false,"console":"false","complete":"false","x":962.5,"y":260,"wires":[]}]

Flow Info

Created 8 years, 8 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x2)
  • comment (x4)
  • debug (x7)
  • function (x2)
  • http in (x2)
  • http request (x1)
  • http response (x2)
  • template (x2)

Tags

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