-
Notifications
You must be signed in to change notification settings - Fork 3
/
identify.js
134 lines (113 loc) · 3.8 KB
/
identify.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
let video = document.getElementById("videoElement");
let uname;
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (error) {
console.log("Something went wrong!");
});
}
async function captureImage() {
let a;
await navigator.mediaDevices.getUserMedia({ video: true })
.then(async function (mediaStream) { a = await takePhoto(mediaStream); })
.catch(error => console.error('getUserMedia() error:', error));
return a;
}
async function takePhoto(mediaStream) {
// For taking an image
const mediaStreamTrack = mediaStream.getVideoTracks()[0];
const imageCapture = new ImageCapture(mediaStreamTrack);
try {
let blob = await imageCapture.takePhoto();
console.log("Took a photo!");
return blob;
// display image todo
} catch (err) {
console.error('takePhoto() error:', err);
}
mediaStream.removeTrack(mediaStreamTrack);
return undefined;
}
async function identify() {
//let confidence = await verify();
$("#videoElement").fadeOut(300).fadeIn(150);
image = await captureImage();
$(".video-overlay").text("Processing...");
$(".video-overlay").show();
let user = await verify(image);
uname = await getCurrentUser();
if (user[1] === uname) {
if (user[0] > 0.45) {
let store = new IdbKvStore(USER_DATA_DB);
let data = await store.get(uname);
console.log(data);
store = new IdbKvStore(USER_FILES_DB);
let fdata = await store.get(uname);
console.log(fdata);
Object.assign(data, fdata);
chrome.runtime.sendMessage({ verified: true, data: data });
window.close();
}
else {
// face found is not verified
$('#hiddenpass').show();
$('#takePhoto').hide();
let login_submit = document.getElementById('pass');
login_submit.onclick = check_password;
}
}
else if (uname == undefined) {
alert("Please Login first!");
window.close();
// window.open("index.html", "_self");
chrome.runtime.sendMessage({ verified: false });
}
else {
// face found is not verified
$('#hiddenpass').show();
$('#takePhoto').hide();
let login_submit = document.getElementById('pass');
login_submit.onclick = check_password;
}
}
async function check_password() {
let pass = document.getElementById("password").value;
if (uname == undefined || pass == undefined)
console.log("Empty password field" + pass + uname);
else {
console.log("Valid uname/pass");
let store = new IdbKvStore(USER_DB);
console.log("Created DB");
// Get user from DB
let value = await store.get(uname);
if (value == pass) {
let store = new IdbKvStore(USER_DATA_DB);
let data = await store.get(uname);
console.log(data);
store = new IdbKvStore(USER_FILES_DB);
let fdata = await store.get(uname);
console.log(fdata);
Object.assign(data, fdata);
chrome.runtime.sendMessage({ verified: true, data: data });
window.close();
}
else {
console.log("Value " + value + " pass " + pass);
console.log("Invalid password");
}
}
}
let login_butt = document.getElementById("login");
login_butt.onclick = identify;
setTimeout(function () {
$('.video-overlay').fadeOut("slow");
}, 3000);
$('#hiddenpass').hide();
async function displayName(){
uname = await getCurrentUser();
$('#username').text("Hi " + uname + "!");
}
displayName();