Vikas01 commited on
Commit
348414c
1 Parent(s): 898599a

Create static/app.js

Browse files
Files changed (1) hide show
  1. static/app.js +71 -0
static/app.js ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // establishing connection between client and server by getting the url.
2
+ var socket = io.connect(window.location.protocol + '//' + document.domain + ':' + location.port, {
3
+ transports: ['websocket']
4
+ });
5
+ // once connected sending out printing out connected
6
+ socket.on('connect', function () {
7
+ console.log("Connected...!", socket.connected)
8
+ });
9
+
10
+ // print results
11
+ var result = document.getElementById('name')
12
+ var score = document.getElementById('score')
13
+ socket.on('result',(data) =>{
14
+ //console.log(data);
15
+ result.innerHTML = data['name']
16
+ score.innerHTML = data['score']
17
+ });
18
+
19
+ // global varibales for video and output.
20
+ var video = document.getElementById('videoElement');
21
+ var canvas = document.getElementById('canvas');
22
+ var context = canvas.getContext('2d');
23
+ var send_data;
24
+ // set video dimentions.
25
+ video.width = 400;
26
+ video.height = 300;
27
+ // rate of sending image
28
+ const FPS = 10;
29
+ // function for sending the webcam input
30
+ function send() {
31
+ width = video.width;
32
+ height = video.height;
33
+ context.drawImage(video, 0, 0, width, height);
34
+ var data = canvas.toDataURL('image/jpeg', 0.5);
35
+ context.clearRect(0, 0, width, height);
36
+ socket.emit('image', data);
37
+ };
38
+ // funtion to start webcam on client side
39
+ function start_camera() {
40
+ send_data = setInterval(send, 1000 / FPS)
41
+ let devices = navigator.mediaDevices
42
+ if (!devices || !devices.getUserMedia) {
43
+ console.log("getUserMedia() not supported.");
44
+ return;
45
+ }
46
+ devices.getUserMedia({
47
+ video: true
48
+ })
49
+ .then(function (vidstream) {
50
+ if ("srcObject" in video) {
51
+ video.srcObject = vidstream;
52
+
53
+ } else {
54
+ video.src = window.src = window.URL.createObjectURL(vidstream);
55
+
56
+ }
57
+ video.onloadeddata = function (e) {
58
+ video.play();
59
+ };
60
+
61
+ })
62
+ .catch(function (e) {
63
+ console.log(e.name + ": " + e.massage);
64
+ });
65
+ };
66
+ // stopping camera input and sending data.
67
+ function stop_camera() {
68
+ video.srcObject.getTracks()[0].stop();
69
+ video.srcObject = null;
70
+ clearInterval(send_data);
71
+ };