Last active
November 14, 2017 04:52
-
-
Save btsimonh/63e72ded918a5da2d7e5eb057674f4cb to your computer and use it in GitHub Desktop.
ADVANCED MOTION DETECTION WITH NODE-OPENCV IN NODE-RED flow1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"d3a698fb.007c88","type":"inject","z":"fa9050d8.3686","name":"Start","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":80,"wires":[["47e40795.d58228"]]},{"id":"47e40795.d58228","type":"function","z":"fa9050d8.3686","name":"LoadCV","func":"var require = global.get('require');\n// try for global install\nvar cv = null\ntry{\n cv = require.main.require('opencv');\n} catch(e){\n cv = require('opencv');\n}\nif (cv === null){\n node.warn('node-opencv not found');\n return;\n}\n\nvar cvdesc = Object.keys(cv);\nnode.send([null, {payload:cvdesc}]);\nflow.set('cv', cv);\n\nnode.send({payload:1});\nnode.send({payload:'next'});\n","outputs":"2","noerr":0,"x":260,"y":80,"wires":[["46d283a6.ce321c"],[]]},{"id":"46d283a6.ce321c","type":"function","z":"fa9050d8.3686","name":"GetImage","func":"\n\nif (msg.payload === 0){\n var vid = flow.get('cvvid');\n if (vid){\n node.warn(util.inspect(vid));\n vid.release();\n flow.set('cvvid', null);\n delete vid;\n }\n}\n\n\nif (msg.payload === 1){\n try{\n flow.set('start', null);\n flow.set('count', null);\n flow.set('last_s', null);\n \n var cv = flow.get('cv');\n var timings = flow.get('timings') || {};\n timings.startup = {};\n timings.startup.start = Date.now();\n var vid = new cv.VideoCapture(0);\n timings.startup.end = Date.now();\n timings.startup.diff = timings.startup.end - timings.startup.start;\n \n node.warn(vid);\n flow.set('cvvid', vid);\n } catch (e){\n node.warn(e);\n }\n}\n\nif (msg.payload === 'ack'){\n var timings = flow.get('timings');\n timings.imagecidiff = context.imageci - msg.imageci;\n return;\n}\n\n\nif (msg.payload === 'next'){\n var vid = flow.get('cvvid');\n \n if (vid){\n try{\n //vid.grab(function(err, im)\n {\n var timings = flow.get('timings') || {};\n lasttime = 0;\n if (timings.readframe)\n lasttime = timings.readframe.start;\n timings.readframe = {};\n timings.readframe.start = Date.now();\n timings.readframe.overall = timings.readframe.start - lasttime;\n \n vid.read(function(err, im){\n try{\n //node.warn(\"read \" + util.inspect(err) + \" \"+util.inspect(im));\n if (err) return;\n //im = im[0];\n //node.warn(util.inspect(im));\n //if (im.size()[0] > 0 && im.size()[1] > 0)\n {\n timings.readframe.end = Date.now();\n timings.readframe.diff = timings.readframe.end - timings.readframe.start;\n \n msg.img = im;\n msg.imdesc = util.inspect(im);\n msg.timestamp = Date.now();\n \n context.imageci = context.imageci || 0;\n msg.imageci = context.imageci++;\n \n node.send(msg);\n }\n } catch(e){\n node.warn(e);\n }\n });\n \n }\n //);\n \n } catch (e){\n node.warn(e);\n }\n }\n}\n","outputs":1,"noerr":0,"x":420,"y":120,"wires":[["c5438f05.97aa","9e05c116.4a0cb"]]},{"id":"8ee7f127.4222","type":"inject","z":"fa9050d8.3686","name":"Stop","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":120,"wires":[["46d283a6.ce321c"]]},{"id":"c5438f05.97aa","type":"function","z":"fa9050d8.3686","name":"BackgroundSubtraction","func":"var cv = flow.get('cv');\n\ntry {\n var bg = flow.get('bg');\n if (!bg){\n bg = cv.BackgroundSubtractor.createMOG();\n node.warn(\"made bg \"+util.inspect(bg));\n flow.set('bg', bg);\n }\n \n if (bg){\n bg.applyMOG(msg.img, function(err, mat){\n //node.warn(\"did mog \"+err+\" \"+util.inspect(mat));\n try{\n msg.out = mat;\n //node.warn(\"did mog:\"+util.inspect(mat));\n \n node.send(msg);\n //msg.avg.release(); \n return;\n } catch(e){\n node.warn(\"exception\"+e);\n }\n });\n msg.img.release();\n }\n \n} catch(e){\n node.warn(e);\n}\n\nreturn;\n","outputs":1,"noerr":0,"x":630,"y":120,"wires":[["772373e2.28fc1c"]]},{"id":"9e05c116.4a0cb","type":"function","z":"fa9050d8.3686","name":"GetNextFrame","func":"setTimeout(function(){\n node.send({payload:'next'});\n}, 10);\nreturn;","outputs":1,"noerr":0,"x":420,"y":180,"wires":[["46d283a6.ce321c"]]},{"id":"1effe675.b603ba","type":"multipart-encoder","z":"fa9050d8.3686","name":"","statusCode":"","ignoreMessages":true,"outputOneNew":true,"outputOneClosed":true,"outputAllClosed":true,"globalHeaders":{"Content-Type":"multipart/x-mixed-replace;boundary=myboundary","Connection":"keep-alive","Expires":"Fri, 01 Jan 1990 00:00:00 GMT","Cache-Control":"no-cache, no-store, max-age=0, must-revalidate","Pragma":"no-cache"},"partHeaders":{"Content-Type":"image/jpeg"},"destination":"all","x":880,"y":180,"wires":[[]]},{"id":"fa44c48c.383758","type":"http in","z":"fa9050d8.3686","name":"","url":"/test2","method":"get","upload":false,"swaggerDoc":"","x":680,"y":180,"wires":[["1effe675.b603ba"]]},{"id":"772373e2.28fc1c","type":"function","z":"fa9050d8.3686","name":"encodetojpg","func":"var enable = flow.get('enableavg');\n\nif (1){\n var d = msg.out.toBuffer();\n msg.out.release();\n var newmsg = {\n payload: d\n };\n node.send(newmsg);\n}\n","outputs":1,"noerr":0,"x":830,"y":120,"wires":[["1effe675.b603ba"]]},{"id":"989820d0.d13f9","type":"comment","z":"fa9050d8.3686","name":"This flow outputs the result of background subtraction using a live camera feed","info":"","x":340,"y":40,"wires":[]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for use with peterbraden/node-opencv master.
Better memory utilisation