{"id":2077,"date":"2018-10-26T13:25:13","date_gmt":"2018-10-26T05:25:13","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=2077"},"modified":"2018-10-26T16:16:16","modified_gmt":"2018-10-26T08:16:16","slug":"first-try-of-p5-and-opencv-js-in-electron","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2018\/10\/26\/first-try-of-p5-and-opencv-js-in-electron\/","title":{"rendered":"First try of P5 and OpenCV JS in Electron"},"content":{"rendered":"<p>This is my first try of the <a href=\"https:\/\/p5js.org\/\" target=\"_blank\" rel=\"noopener\">p5.js<\/a> together with the official release of OpenCV JavaScript. I decided not to use any browsers and experimented with the integration in the <a href=\"https:\/\/electronjs.org\/\" target=\"_blank\" rel=\"noopener\">Electron<\/a> environment with <a href=\"https:\/\/nodejs.org\/en\/\" target=\"_blank\" rel=\"noopener\">Node.js<\/a>. The first experiment is a simple image processing application using Canny edge detector. The IDE I choose to work on is the free <a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noopener\">Visual Studio Code<\/a> and which is also available in multiple OS platforms. I have tested both in Windows 10 and Mac OSX Mojave. In Mac OSX, I first install the Node.js with <a href=\"https:\/\/brew.sh\/\" target=\"_blank\" rel=\"noopener\">Homebrew<\/a>.<\/p>\n<pre lang=\"java\">\r\nbrew update\r\nbrew install node\r\n<\/pre>\n<p>Then I install the Electron as a global package with npm.<\/p>\n<pre lang=\"java\">\r\nnpm install -g electron\r\n<\/pre>\n<p>For the <a href=\"https:\/\/code.visualstudio.com\/\" rel=\"noopener\" target=\"_blank\">Visual Studio Code<\/a>, I also include the JavaScript support and the ESLint plugin. The next step is to download the <em>p5.js<\/em> and <em>p5.dom.js<\/em> code from the <a href=\"https:\/\/p5js.org\/download\/\" rel=\"noopener\" target=\"_blank\">p5.js website<\/a> to your local folder. I put them into a <em>libs<\/em> folder outside of my application folders. For OpenCV, it actually includes the pre-built <a href=\"https:\/\/docs.opencv.org\/3.4.3\/opencv.js\" rel=\"noopener\" target=\"_blank\">opencv.js<\/a> from its documentation repository. The version I used here is 3.4.3. The only documentation I can find for OpenCV JS is this <a href=\"https:\/\/docs.opencv.org\/3.4\/d5\/d10\/tutorial_js_root.html\" rel=\"noopener\" target=\"_blank\">tutorial<\/a>.<\/p>\n<p>For each of the Node.js application, you can initialise it with the following command in its folder. Alternately, you can also do it within the Terminal window from Visual Studio Code. Fill in the details when prompted.<\/p>\n<pre lang=\"java\">\r\nnpm init\r\n<\/pre>\n<p>In Visual Studio Code, you have to add a configuration to use the <em>electron<\/em> command to run the main program, <em>main.js<\/em>, rather than using the default <em>node<\/em> command. After adding the configuration, it will generate the <em>launch.json<\/em> file like the following,<\/p>\n<pre lang=\"java\">\r\n{\r\n    \/\/ Use IntelliSense to learn about possible attributes.\r\n    \/\/ Hover to view descriptions of existing attributes.\r\n    \/\/ For more information, visit: https:\/\/go.microsoft.com\/fwlink\/?linkid=830387\r\n    \"version\": \"0.2.0\",\r\n    \"configurations\": [\r\n        {\r\n            \"type\": \"node\",\r\n            \"request\": \"launch\",\r\n            \"name\": \"Electron Main\",\r\n            \"runtimeExecutable\": \"electron\",\r\n            \"program\": \"${workspaceFolder}\/main.js\",\r\n            \"protocol\": \"inspector\"\r\n        }\r\n    ]\r\n}\r\n<\/pre>\n<p>For the programming part, I used a <em>main.js<\/em> to define the Electron window and its related functions. The window will load the <em>index.html<\/em> page. It is the main webpage for the application. It will then call the <em>sketch.js<\/em> to perform the <em>p5.js<\/em> and <em>OpenCV<\/em> core functions. The <em>p5.js<\/em> and <em>OpenCV<\/em> communicate through the use of the <em>canvas<\/em> object. The GUI functions, <em>imread()<\/em> and <em>imshow()<\/em> are used for such communication. This example will switch on the default webcam to capture the live video and perform a blur and Canny edge detection.<\/p>\n<div id='gallery-1' class='gallery galleryid-2077 gallery-columns-3 gallery-size-thumbnail'><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV01.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV01-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV01-150x150.png 150w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV01-100x100.png 100w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>\n\t\t\t<\/div><\/figure><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV02.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV02-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV02-150x150.png 150w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2018\/10\/p5jsOpenCV02-100x100.png 100w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>\n\t\t\t<\/div><\/figure>\n\t\t<\/div>\n\n<p>Source code is now available at my <a href=\"https:\/\/github.com\/chungbwc\/Magicandlove\/tree\/master\/ml20181026a\" target=\"_blank\" rel=\"noopener\">GitHub repository<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is my first try of the p5.js together with the official release of OpenCV JavaScript. I decided not to use any browsers and experimented with the integration in the Electron environment with Node.js. The first experiment is a simple image processing application using Canny edge detector. The IDE I choose to work on is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,79],"tags":[172,3,171],"class_list":["post-2077","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-electronjs","tag-opencv","tag-p5-js"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/2077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=2077"}],"version-history":[{"count":8,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/2077\/revisions"}],"predecessor-version":[{"id":2087,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/2077\/revisions\/2087"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=2077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=2077"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=2077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}