forked from dvshah/i-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.js
177 lines (165 loc) · 4.96 KB
/
webcam.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function newTab() {
chrome.tabs.create({active:true});
}
function removeTab() {
chrome.tabs.query({active:true,currentWindow:true},(tab)=>{
chrome.tabs.remove([tab[0].id]);
})
}
function TabForward() {
chrome.tabs.query({currentWindow:true},(tab)=>{
console.log(tab);
chrome.tabs.query({currentWindow:true,active:true},(tab2)=>{
console.log("current tab = ");
console.log(tab2);
let pos =0;
while(pos<tab.length && tab[pos].id!=tab2[0].id){
pos++;
}
pos++;
pos = pos%tab.length;
chrome.tabs.highlight({tabs:[pos]});
});
});
}
function TabBackward() {
chrome.tabs.query({currentWindow:true},(tab)=>{
console.log(tab);
chrome.tabs.query({currentWindow:true,active:true},(tab2)=>{
console.log("current tab = ");
console.log(tab2);
let pos =0;
while(pos<tab.length && tab[pos].id!=tab2[0].id){
pos++;
}
pos--;
pos = pos+tab.length;
pos = pos%tab.length;
chrome.tabs.highlight({tabs:[pos]});
});
});
}
function ScrollUp() {
chrome.tabs.query({active:true,currentWindow:true},(tab)=>{
chrome.tabs.executeScript(tab[0].id,{code:"window.scrollBy(0,-500)"},()=>{
});
});
}
function ScrollDown() {
chrome.tabs.query({active:true,currentWindow:true},(tab)=>{
chrome.tabs.executeScript(tab[0].id,{code:"window.scrollBy(0,500)"},()=>{
});
});
}
function zoomIn(){
chrome.tabs.query({active:true,currentWindow:true},(tab)=>{
chrome.tabs.getZoom(tab[0].id,(zoomFactor) => {
chrome.tabs.setZoom(tab[0].id,zoomFactor+0.50,()=>{
console.log("zooming in");
});
});
});
}
function zoomOut(){
chrome.tabs.query({active:true,currentWindow:true},(tab)=>{
chrome.tabs.getZoom(tab[0].id,(zoomFactor) => {
chrome.tabs.setZoom(tab[0].id,zoomFactor-0.25,()=>{
console.log("zooming in");
});
});
});
}
var video = document.getElementById('video');
// Get access to the camera!
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Not adding `{ audio: true }` since we only want video now
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
video.srcObject = stream;
});
}
// Elements for taking the snapshot
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var video = document.getElementById('video');
var input = $("#image");
var form = $("form");
let arr = [];
const fun = async(value) => {
}
var intId;
const postMan = () => {
intId = setInterval(() => {
context.drawImage(video, 0, 0, 640, 480);
let encode = canvas.toDataURL();
$.ajax({
url: 'http://localhost:5000/',
type: 'POST',
data: { encode },
success :async function(dat) {
JSON.parse(dat);
//console.log(dat);
res = parseInt(dat);
console.log(dat, res);
arr.push(res);
let i = arr.length, data=0;
if(i>100)
{
arr.splice(0,1);
}
//console.log(arr);
i = arr.length;
let cnt = 7;
let v = Array(cnt, 0);
for(let k=0; k<cnt; k++)
{
v[k] = 0;
}
//a = Array(8, 0);
for(let k=0;k<8;k++){
let j = k+2;
v[arr[i-j]]+=1;
//console.log(arr[i-j]);
};
let temp=0,ans=0;
for(let j=0;j<cnt;j++){
if(v[j] > temp)
{
temp = v[j];
ans = j;
}
};
if(temp > 1)
{
data = ans;
}
else
{
data = 0;
}
if(data!=0 && data!=1)
delay = 3000;
console.log(v, temp, data);
data = data.toString();
//data = "2";
console.log(data);
if(data === "1"){
//removeTab();
}else if(data === "3"){
// newTab();
}else if(data==="6"){
ScrollUp();
}else if(data==="2"){
ScrollDown();
//window.scrollBy(0,500);
}else if(data==="4"){
TabBackward();
}else if(data=="5"){
TabForward();
}
setTimeout(() => {}, delay);
},
datatype: "String"
});
}, 200);
};
postMan();