Advertisement
Guest User

JS IR/Night Vision With Darkness/Light (Voice Detection) V12

a guest
Feb 22nd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.64 KB | None | 0 0
  1.  
  2.  
  3.  
  4. <meta name="robots" content="noindex">
  5. <style>
  6. body{
  7. filter:brightness(400%);
  8.  
  9. }
  10. </style>
  11. <body onload="script:setInterval('this.score.textContent='',5);">
  12. <font color="white">
  13. <center>
  14. <figure>
  15. <video id="video" hidden></video>
  16. </figure>
  17.  
  18. <figure>
  19. <canvas id="motion"></canvas>
  20. <figcaption>
  21.  
  22. <br>
  23.  
  24. <p>
  25. <button id="reload" style="background-color:limegreen;color:black;border-radius:2px;" onclick="script:
  26. var conf=confirm('Are you sure you want to restart the application?');
  27. if(conf==1){
  28. document.location.reload();
  29. }else{return;}"><b>Restart Application</button>
  30. <button id="stats"style="background-color:limegreen;color:black;border-radius:2px;" onclick="script:if(majamm!=0){majamm--;}
  31. alert('# of times you were in a light area: '+vlightcnt); alert('# of times you were in a dark area: '+vdarkcnt);"><b>View All Statistics</button>
  32. <button id="reset"style="background-color:limegreen;color:black;border-radius:2px;" onclick="script:
  33. var conf1=confirm('Are you sure you want to reset all statistics now?');
  34. if(conf1==1){ ammount=0;majamm=0;large=0;vlarge=0;extreme=0;max=0;darkcnt=0;lightcnt=0;vlightcnt=0;vdarkcnt=0;}else{return;}"><b>Reset All</button><p>
  35.  
  36. <span id="score1"></span><p>
  37. <span id="score2"></span><p>
  38. <span id="score3"></span><p>
  39. <span id="score"></span><p>
  40.  
  41.  
  42. </figcaption>
  43. </figure>
  44. <script>
  45. var DiffCamEngine = (function() {
  46. var stream; // stream obtained from webcam
  47. var video; // shows stream
  48. var captureCanvas; // internal canvas for capturing full images from video
  49. var captureContext; // context for capture canvas
  50. var diffCanvas; // internal canvas for diffing downscaled captures
  51. var diffContext; // context for diff canvas
  52. var motionCanvas; // receives processed diff images
  53. var motionContext; // context for motion canvas
  54.  
  55. var initSuccessCallback; // called when init succeeds
  56. var initErrorCallback; // called when init fails
  57. var startCompleteCallback; // called when start is complete
  58. var captureCallback; // called when an image has been captured and diffed
  59.  
  60. var captureInterval; // interval for continuous captures
  61. var captureIntervalTime; // time between captures, in ms
  62. var captureWidth; // full captured image width
  63. var captureHeight; // full captured image height
  64. var diffWidth; // downscaled width for diff/motion
  65. var diffHeight; // downscaled height for diff/motion
  66. var isReadyToDiff; // has a previous capture been made to diff against?
  67. var pixelDiffThreshold; // min for a pixel to be considered significant
  68. var scoreThreshold; // min for an image to be considered significant
  69. var includeMotionBox; // flag to calculate and draw motion bounding box
  70. var includeMotionPixels; // flag to create object denoting pixels with motion
  71.  
  72. function init(options) {
  73.  
  74. // sanity check
  75. if (!options) {
  76. throw 'No options object provided';
  77. }
  78.  
  79. // incoming options with defaults
  80. video = options.video || document.createElement('video');
  81. motionCanvas = options.motionCanvas || document.createElement('canvas');
  82. captureIntervalTime = options.captureIntervalTime || 100;
  83. captureWidth = options.captureWidth || 850;
  84. captureHeight = options.captureHeight || 450;
  85. diffWidth = options.diffWidth || 850;
  86. diffHeight = options.diffHeight || 450;
  87. pixelDiffThreshold = options.pixelDiffThreshold || 32;
  88. scoreThreshold = options.scoreThreshold || 16;
  89. includeMotionBox = options.includeMotionBox || false;
  90. includeMotionPixels = options.includeMotionPixels || false;
  91.  
  92. // callbacks
  93. initSuccessCallback = options.initSuccessCallback || function() {};
  94. initErrorCallback = options.initErrorCallback || function() {};
  95. startCompleteCallback = options.startCompleteCallback || function() {};
  96. captureCallback = options.captureCallback || function() {};
  97.  
  98. // non-configurable
  99. captureCanvas = document.createElement('canvas');
  100. diffCanvas = document.createElement('canvas');
  101. var img=document.getElementById('img')
  102. isReadyToDiff = false;
  103.  
  104. // prep video
  105. video.autoplay = true;
  106.  
  107. // prep capture canvas
  108. captureCanvas.width = captureWidth;
  109. captureCanvas.height = captureHeight;
  110. captureContext = captureCanvas.getContext('2d');
  111.  
  112. // prep diff canvas
  113. diffCanvas.width = diffWidth;
  114. diffCanvas.height = diffHeight;
  115. diffContext = diffCanvas.getContext('2d');
  116.  
  117. // prep motion canvas
  118. motionCanvas.width = diffWidth;
  119. motionCanvas.height = diffHeight;
  120. motionContext = motionCanvas.getContext('2d');
  121.  
  122. requestWebcam();
  123. }
  124.  
  125. function requestWebcam() {
  126. var constraints = {
  127. audio: false,
  128. video: { width: captureWidth, height: captureHeight }
  129. };
  130.  
  131. navigator.mediaDevices.getUserMedia(constraints)
  132. .then(initSuccess)
  133. .catch(initError);
  134. }
  135.  
  136. function initSuccess(requestedStream) {
  137. stream = requestedStream;
  138. initSuccessCallback();
  139. }
  140.  
  141. function initError(error) {
  142. console.log(error);
  143. initErrorCallback();
  144. }
  145.  
  146. function start() {
  147. if (!stream) {
  148. throw 'Cannot start after init fail';
  149. }
  150.  
  151. // streaming takes a moment to start
  152. video.addEventListener('canplay', startComplete);
  153. video.srcObject = stream;
  154. }
  155.  
  156. function startComplete() {
  157. video.removeEventListener('canplay', startComplete);
  158. captureInterval = setInterval(capture, captureIntervalTime);
  159. startCompleteCallback();
  160. }
  161.  
  162. function stop() {
  163. clearInterval(captureInterval);
  164. video.src = '';
  165. motionContext.clearRect(0, 0, diffWidth, diffHeight);
  166. isReadyToDiff = false;
  167. }
  168.  
  169. function capture() {
  170. // save a full-sized copy of capture
  171. captureContext.drawImage(video, 0, 0, captureWidth, captureHeight);
  172. var captureImageData = captureContext.getImageData(0, 0, captureWidth, captureHeight);
  173.  
  174. // diff current capture over previous capture, leftover from last time
  175. diffContext.globalCompositeOperation = 'difference';
  176. //diffContext.drawImage(video, 0, 0, diffWidth, diffHeight);
  177. var diffImageData = diffContext.getImageData(0, 0, diffWidth, diffHeight);
  178.  
  179. if (isReadyToDiff) {
  180. var diff = processDiff(diffImageData);
  181.  
  182. motionContext.putImageData(diffImageData, 0, 0);
  183. if (diff.motionBox) {
  184. motionContext.strokeStyle = '#fff';
  185. motionContext.strokeRect(
  186. diff.motionBox.x.min + 0.5,
  187. diff.motionBox.y.min + 0.5,
  188. diff.motionBox.x.max - diff.motionBox.x.min,
  189. diff.motionBox.y.max - diff.motionBox.y.min
  190. );
  191. }
  192. captureCallback({
  193. imageData: captureImageData,
  194. score: diff.score,
  195. hasMotion: diff.score >= scoreThreshold,
  196. motionBox: diff.motionBox,
  197. motionPixels: diff.motionPixels,
  198. getURL: function() {
  199. return getCaptureUrl(this.imageData);
  200. },
  201. checkMotionPixel: function(x, y) {
  202. return checkMotionPixel(this.motionPixels, x, y)
  203. }
  204. });
  205. }
  206.  
  207. // draw current capture normally over diff, ready for next time
  208. diffContext.globalCompositeOperation = 'source-over';
  209. diffContext.drawImage(video, 0, 0, diffWidth, diffHeight);
  210. isReadyToDiff = true;
  211. }
  212.  
  213. function processDiff(diffImageData) {
  214. var rgba = diffImageData.data;
  215.  
  216. // pixel adjustments are done by reference directly on diffImageData
  217. var score = 0;
  218. var motionPixels = includeMotionPixels ? [] : undefined;
  219. var motionBox = undefined;
  220. for (var i = 0; i < rgba.length; i += 4) {
  221. var pixelDiff = rgba[i] * 0.3 + rgba[i + 1] * 0.6 + rgba[i + 2] * 0.1;
  222. var normalized = Math.min(255, pixelDiff * (255 / pixelDiffThreshold));
  223. rgba[i] = 0;
  224. rgba[i + 1] = pixelDiff;
  225. rgba[i + 2] = 0;
  226.  
  227. if (pixelDiff >= pixelDiffThreshold) {
  228. score++;
  229. coords = calculateCoordinates(i / 4);
  230.  
  231. if (includeMotionBox) {
  232. motionBox = calculateMotionBox(motionBox, coords.x, coords.y);
  233. }
  234.  
  235. if (includeMotionPixels) {
  236. motionPixels = calculateMotionPixels(motionPixels, coords.x, coords.y, pixelDiff);
  237. }
  238.  
  239. }
  240. }
  241.  
  242. return {
  243. score: score,
  244. motionBox: score > scoreThreshold ? motionBox : undefined,
  245. motionPixels: motionPixels
  246. };
  247. }
  248.  
  249. function calculateCoordinates(pixelIndex) {
  250. return {
  251. x: pixelIndex % diffWidth,
  252. y: Math.floor(pixelIndex / diffWidth)
  253. };
  254. }
  255.  
  256. function calculateMotionBox(currentMotionBox, x, y) {
  257. // init motion box on demand
  258. var motionBox = currentMotionBox || {
  259. x: { min: coords.x, max: x },
  260. y: { min: coords.y, max: y }
  261. };
  262.  
  263. motionBox.x.min = Math.min(motionBox.x.min, x);
  264. motionBox.x.max = Math.max(motionBox.x.max, x);
  265. motionBox.y.min = Math.min(motionBox.y.min, y);
  266. motionBox.y.max = Math.max(motionBox.y.max, y);
  267.  
  268. return motionBox;
  269. }
  270.  
  271. function calculateMotionPixels(motionPixels, x, y, pixelDiff) {
  272. motionPixels[x] = motionPixels[x] || [];
  273. motionPixels[x][y] = true;
  274.  
  275. return motionPixels;
  276. }
  277.  
  278. function getCaptureUrl(captureImageData) {
  279. // may as well borrow captureCanvas
  280. captureContext.putImageData(captureImageData, 0, 0);
  281. return captureCanvas.toDataURL();
  282. }
  283.  
  284. function checkMotionPixel(motionPixels, x, y) {
  285. return motionPixels && motionPixels[x] && motionPixels[x][y];
  286. }
  287.  
  288. function getPixelDiffThreshold() {
  289. return pixelDiffThreshold;
  290. }
  291.  
  292. function setPixelDiffThreshold(val) {
  293. pixelDiffThreshold = val;
  294. }
  295.  
  296. function getScoreThreshold() {
  297. return scoreThreshold;
  298. }
  299.  
  300. function setScoreThreshold(val) {
  301. scoreThreshold = val;
  302. }
  303.  
  304. return {
  305. // public getters/setters
  306. getPixelDiffThreshold: getPixelDiffThreshold,
  307. setPixelDiffThreshold: setPixelDiffThreshold,
  308. getScoreThreshold: getScoreThreshold,
  309. setScoreThreshold: setScoreThreshold,
  310.  
  311. // public functions
  312. init: init,
  313. start: start,
  314. stop: stop
  315. };
  316. })();
  317. var video = document.getElementById('video');
  318. var canvas = document.getElementById('motion');
  319. var score = document.getElementById('score');
  320. var score1 = document.getElementById('score1');
  321. var score2=document.getElementById('score2');
  322. var score3=document.getElementById('score3');
  323. var photo = document.getElementById('motion');
  324.  
  325. function initSuccess() {
  326. DiffCamEngine.start();
  327. }
  328.  
  329. function initError() {
  330. alert('Something went wrong.');
  331. }
  332. var ammount=0;
  333. var majamm=0;
  334. var large=0;
  335. var vlarge=0;
  336. var extreme=0;
  337. var max=0;
  338. var darkcnt=0;
  339. var lightcnt=0;
  340. var vlightcnt=0;
  341. var vdarkcnt=0;
  342. function capture(payload) {
  343. var p= photo.getContext('2d')
  344. //score.textContent = payload.score;
  345. oscillator.stop(0);
  346. console.log(payload.score)
  347. score3.textContent=('# Of Times Entered Into A Dark Area: '+vdarkcnt);
  348. score1.textContent='Light Value: '+payload.score;
  349. if(payload.score>=150000){score.textContent =('You have entered into a light area/room!!')
  350. lightcnt++;
  351. if(lightcnt==2){
  352. vlightcnt++;
  353. score2.textContent=('# Of Times You Entered A Light Area: '+vlightcnt);
  354. var Message = new SpeechSynthesisUtterance("You have entered into a light area "+ vlightcnt+" times!!");
  355. if(vlightcnt==1){
  356. var Message = new SpeechSynthesisUtterance("You have entered into a light area "+ vlightcnt+" time!!");
  357. }
  358. var Voices = window.speechSynthesis.getVoices();
  359. Message.Voice = Voices[1];
  360. window.speechSynthesis.speak(Message);
  361. darkcnt=0;
  362. return;
  363. }
  364. }
  365. if(payload.score<=150000){
  366. score.textContent =('You have entered into a dark area/room!!')
  367. darkcnt++;
  368. if(darkcnt==2){
  369. vdarkcnt++;
  370. score3.textContent=('# Of Times Entered Into A Dark Area: '+vdarkcnt);
  371. var Message = new SpeechSynthesisUtterance("You have entered into a dark area" + vdarkcnt +"times!!");
  372. if(vdarkcnt==1){
  373. var Message = new SpeechSynthesisUtterance("You have entered into a dark area" + vdarkcnt +"time!!");
  374. }
  375. var Voices = window.speechSynthesis.getVoices();
  376. Message.Voice = Voices[1];
  377. window.speechSynthesis.speak(Message)
  378. lightcnt=0;
  379. return;
  380. }
  381. }
  382. setTimeout(function(){ score.textContent='' }, 500);
  383. if(payload.score<350000){
  384. //score.textContent =('You are in a light area/room!!')
  385. //else{
  386. // score.textContent =('You are in a illuminated area!!')
  387. // }
  388. //score.textContent =('A Normal Amount Of Movement Has Been Detected '+majamm +' times!!')
  389. majamm++;
  390.  
  391. //photo.setAttribute('src',canvas.toDataUrl('image/png'));
  392.  
  393. //window.open(captureImageData.ToDataURL());
  394. // return;
  395. // p.putImage(captureImageData, 100, 100, 200, 200);
  396. }
  397. if(payload.score<35){}else{
  398. //p.putImage(captureImageData, 100, 100, 200, 200);
  399. console.log('Minor Movement Detected '+ammount+' times!!')
  400. ammount++;
  401. return;
  402. }
  403. }
  404.  
  405. DiffCamEngine.init({
  406. video: video,
  407. motionCanvas: canvas,
  408. initSuccessCallback: initSuccess,
  409. initErrorCallback: initError,
  410. captureCallback: capture
  411. });
  412. var context = new AudioContext();
  413. oscillator=context.createOscillator();
  414. </script>
  415.  
  416. <center>
  417. <body bgcolor="black">
  418. <font color="white">
  419. <canvas id="canvas" width="850" height="450"></canvas>
  420. <script>
  421. var canvas = document.getElementById("motion");
  422. var ctx = canvas.getContext("2d");
  423. var width=120
  424. ctx.fillStyle = "white";
  425. ctx.fillRect(0,0,120,1000);
  426. ctx.fillStyle = "blue";
  427. ctx.fillRect(0,400,120,100);
  428. ctx.fillStyle = "yellow";
  429. ctx.fillRect(width,0,120,1000);
  430. ctx.fillStyle = "magenta";
  431. ctx.fillRect(width,400,120,100);
  432. ctx.fillStyle = "cyan";
  433. ctx.fillRect(width*2,0,120,1000);
  434. ctx.fillStyle = "yellow";
  435. ctx.fillRect(width*2,400,120,100);
  436. ctx.fillStyle = "#0FFF00";
  437. ctx.fillRect(width*3,0,120,1000);
  438. ctx.fillStyle = "red";
  439. ctx.fillRect(width*3,400,120,100);
  440. ctx.fillStyle = "magenta";
  441. ctx.fillRect(width*4,0,120,1000);
  442. ctx.fillStyle = "cyan";
  443. ctx.fillRect(width*4,400,120,100);
  444. ctx.fillStyle = "red";
  445. ctx.fillRect(width*5,0,120,1000);
  446. ctx.fillStyle = "#0FFF00";
  447. ctx.fillRect(width*5,400,120,100);
  448. ctx.fillStyle = "blue";
  449. ctx.fillRect(width*6,0,120,1000);
  450. ctx.fillStyle = "white";
  451. ctx.fillRect(width*6,400,120,100);
  452.  
  453. oscillator.type = 'sine';
  454. oscillator.frequency.value = 1000;
  455. oscillator.connect(context.destination);
  456. oscillator.start(0);
  457. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement