-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
173 lines (159 loc) · 5.55 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="js/aws-cognito-sdk.min.js"></script>
<script src="js/amazon-cognito-identity.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.1125.0/aws-sdk.min.js" integrity="sha512-SMJBkbeQeucdzHmmGJt8uUd24I93qluZgBavd29suHkxnxJQRI67OJ7GXo3BvXTgso0a4OLp3wrbrjnKURhJFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="face-api.js"></script>
<script src="js/commons.js"></script>
<script src="js/faceDetectionControls.js"></script>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="content-wrapper" style="display:flex; flex-wrap:wrap;">
<div class="progress" id="loader">
<div class="indeterminate"></div>
</div>
<div class="content-inner" style="margin:auto; position:relative;">
<video onloadedmetadata="onPlay(this)" id="inputVideo" autoplay muted playsinline width="375" height="812" ></video>
<canvas id="overlay" width="375" height="812" />
</div>
<canvas id="snapshot" style="display:none"></canvas>
<div class="recognition-status">
<div class="recognition-label">
CERCO VOLTI...
</div>
</div>
</div>
</body>
<script>
anonLog();
let forwardTimes = []
const context = {
locked : false,
failed_attempts : 0,
_has_mask : false,
set has_mask(v){
this._hash_mask = v;
if(v){
$(".recognition-label").css("background-color", "forestgreen");
$(".recognition-label").text("✅ MASCHERINA PRESENTE ✅");
}else{
$(".recognition-label").css("background-color", "crimson");
$(".recognition-label").text("⚠️ INDOSSA LA MASCHERINA ⚠️");
}
},
get has_mask(){
return this._hash_mask;
}
};
function detectFaces(imageData){
AWS.region = "eu-central-1";
var rekognition = new AWS.Rekognition({apiVersion: '2016-06-27'});
var params = {
Image: {
Bytes: imageData
},
SummarizationAttributes: {
MinConfidence: 80,
RequiredEquipmentTypes: [
'FACE_COVER'
]
}
};
rekognition.detectProtectiveEquipment(params, function (err, data) {
if (err) console.log(err, err.stack);
else {
const persons = data.Persons.length;
const persons_with_mask = data.Summary.PersonsWithRequiredEquipment.length;
if(persons == persons_with_mask){
context.failed_attempts = 0;
context.has_mask = true;
}else{
context.failed_attempts++;
if(context.failed_attempts >= 2){
context.has_mask = false;
}
}
setTimeout(() => {
context.locked = false;
}, 1000)
}
});
}
function anonLog(){
AWS.config.region = 'eu-central-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
// replace with your IdentityPoolId
IdentityPoolId: 'REPLACE_HERE',
});
AWS.config.credentials.get(function(){
var accessKeyId = AWS.config.credentials.accessKeyId;
var secretAccessKey = AWS.config.credentials.secretAccessKey;
var sessionToken = AWS.config.credentials.sessionToken;
});
}
function getImageBytes(image){
const only_stream = atob(image.split("data:image/png;base64,")[1]);
var length = only_stream.length;
var imageBytes = new ArrayBuffer(length);
var ua = new Uint8Array(imageBytes);
for (var i = 0; i < length; i++) {
ua[i] = only_stream.charCodeAt(i);
}
return imageBytes;
}
function checkForMask(){
context.locked = true;
const image = takePicture();
const image_bytes = getImageBytes(image);
detectFaces(image_bytes);
}
function takePicture(){
const canvas = document.getElementById("snapshot");
const video = document.getElementById("inputVideo");
canvas.width = $(window).width();
canvas.height = $(window).height();
canvas.getContext('2d').drawImage(video, 0, 0, $(window).width(),$(window).height());
const imageStream = canvas.toDataURL('image/png');
return imageStream;
}
async function onPlay(){
const videoEl = $('#inputVideo').get(0)
if(
videoEl.paused ||
videoEl.ended ||
!isFaceDetectionModelLoaded()
)
return setTimeout(() => onPlay())
const options = getFaceDetectorOptions()
const result = await faceapi.detectSingleFace(videoEl, options)
if(result){
if(!context.locked){
checkForMask();
}
const canvas = $('#overlay').get(0)
const dims = faceapi.matchDimensions(canvas, videoEl, true)
faceapi.draw.drawDetections(canvas, faceapi.resizeResults(result, dims))
$("#overlay").show(0);
}else{
$("#overlay").hide(0);
}
setTimeout(() => onPlay())
}
async function run(){
await changeFaceDetector(TINY_FACE_DETECTOR)
const stream = await navigator.mediaDevices.getUserMedia({ video: {} })
const videoEl = $('#inputVideo').get(0)
videoEl.srcObject = stream
}
$(document).ready(function() {
run()
})
</script>
</body>
</html>