Skip to content

Instantly share code, notes, and snippets.

@kasacchiful
Created December 4, 2015 16:57
Show Gist options
  • Save kasacchiful/d4d9d5563ef8cfa1bf21 to your computer and use it in GitHub Desktop.
Save kasacchiful/d4d9d5563ef8cfa1bf21 to your computer and use it in GitHub Desktop.
NDS#45 での Node-RED サンプル
[{"id":"318c2552.ce73da","type":"inject","z":"77abd3e2.88542c","name":"Test URL Injection","topic":"","payload":"http://example.net/hoge_sample.jpg","payloadType":"string","repeat":"","crontab":"","once":false,"x":152,"y":520,"wires":[["6249d35b.9db62c"]]},{"id":"9c6eb24d.63915","type":"http in","z":"77abd3e2.88542c","name":"sample html page","url":"/sample","method":"get","swaggerDoc":"","x":148,"y":68,"wires":[["da68040.f2598"]]},{"id":"42f767f4.bd0898","type":"http response","z":"77abd3e2.88542c","name":"sample html page","x":593,"y":68,"wires":[]},{"id":"da68040.f2598","type":"template","z":"77abd3e2.88542c","name":"html template","field":"payload","format":"html","template":"<!DOCTYPE html>\n<html>\n<head>\n <title>Face Detection Sample</title>\n <script src=\"https://code.jquery.com/jquery-2.1.4.min.js\"></script>\n <script>\n $(document).ready(function() {\n $('#url_analyze').on('click', function() {\n var image_url, img;\n image_url = $('#image_url').val();\n img = new Image;\n img.src = image_url;\n img.onload = function() {\n var canvas, ctx;\n $('#canvas').attr({\n 'width': img.width,\n 'height': img.height\n });\n canvas = $('#canvas').get(0);\n ctx = canvas.getContext('2d');\n ctx.drawImage(img, 0, 0);\n analyze_url(image_url, canvas, ctx);\n };\n });\n });\n function analyze_url(image_url, canvas, ctx) {\n var api_endpoint = \"/api/face\";\n var api_url = api_endpoint +\n \"?url=\" + encodeURIComponent(image_url);\n $('#loading').css('visibility', 'visible');\n return $.ajax({\n url: api_url,\n dataType: 'json',\n context: this\n }).success(function(data, status, xhr) {\n console.log(data);\n display(data, canvas, ctx);\n return $('#loading').css('visibility', 'hidden');\n }).error(function(data, status, xhr) {\n alert(status);\n return $('#loading').css('visibility', 'hidden');\n });\n }\n function display(data, canvas, ctx) {\n var age, face, gender, height, width, x, y;\n data.forEach(function(face, index, ar) {\n age = face.age.ageRange;\n gender = face.gender.gender;\n height = face.height;\n width = face.width;\n x = face.positionX;\n y = face.positionY;\n ctx.globalCompositeOperation = 'source-over';\n ctx.strokeStyle = \"rgb(255,0,0)\";\n ctx.strokeRect(x, y, width, height);\n ctx.fillStyle = \"rgb(255,255,255)\";\n ctx.textAlign = \"left\";\n ctx.textBaseline = \"top\";\n ctx.fillText(\"age: \" + age + \", gender: \" + gender, x, y);\n });\n }\n </script>\n</head>\n<body>\n <div>\n <input type=\"text\" name=\"image_url\" id=\"image_url\">\n <button type=\"button\" id=\"url_analyze\" name=\"button\">analyze!</button>\n <img src=\"http://face-detection-nds45.mybluemix.net/img/loading.gif\" id=\"loading\" style=\"visibility: hidden;\">\n </div>\n\n <canvas id=\"canvas\"></canvas>\n</body>\n</html>\n","x":380,"y":68,"wires":[["42f767f4.bd0898"]]},{"id":"6249d35b.9db62c","type":"alchemy-image-analysis","z":"77abd3e2.88542c","name":"Face Detection","apikey":"","image-feature":"imageFaces","x":397,"y":391,"wires":[["704b34fd.8fb4cc","b284d203.4d7b3"]]},{"id":"351f28ab.cae0d8","type":"http in","z":"77abd3e2.88542c","name":"face image","url":"/api/face","method":"get","swaggerDoc":"","x":125,"y":174,"wires":[["daaa756f.255588"]]},{"id":"704b34fd.8fb4cc","type":"debug","z":"77abd3e2.88542c","name":"","active":true,"console":"false","complete":"result","x":642,"y":275,"wires":[]},{"id":"8e8e57a.f7171a8","type":"http response","z":"77abd3e2.88542c","name":"face image result","x":643,"y":544,"wires":[]},{"id":"b8696240.4796a","type":"inject","z":"77abd3e2.88542c","name":"Test parameter","topic":"","payload":"{\"url\":\"http://example.net/hoge_sample.jpg\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":150,"y":398,"wires":[["35418d8b.cabe72"]]},{"id":"35418d8b.cabe72","type":"json","z":"77abd3e2.88542c","name":"","x":202,"y":279,"wires":[["daaa756f.255588"]]},{"id":"daaa756f.255588","type":"function","z":"77abd3e2.88542c","name":"url to payload","func":"msg.payload = msg.payload.url;\nreturn msg;\n","outputs":1,"noerr":0,"x":371,"y":174,"wires":[["6249d35b.9db62c"]]},{"id":"b284d203.4d7b3","type":"function","z":"77abd3e2.88542c","name":"result to payload","func":"msg.payload = msg.result\n\nreturn msg;","outputs":1,"noerr":0,"x":629,"y":391,"wires":[["8e8e57a.f7171a8"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment