-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect_faces_cognito_test.html
106 lines (100 loc) · 3.32 KB
/
detect_faces_cognito_test.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
<!DOCTYPE html>
<html>
<head>
<script src="aws-cognito-sdk.min.js"></script>
<script src="amazon-cognito-identity.min.js"></script>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.16.0.min.js"></script>
<meta charset="UTF-8">
<title>Rekognition</title>
</head>
<body>
<H1>Age Estimator</H1>
<input type="file" name="fileToUpload" id="fileToUpload" accept="image/*">
<p id="opResult"></p>
</body>
<script>
document.getElementById("fileToUpload").addEventListener("change", function (event) {
ProcessImage();
}, false);
//Calls DetectFaces API and shows estimated ages of detected faces
function DetectFaces(imageData) {
AWS.region = [REGION];
var rekognition = new AWS.Rekognition();
var params = {
Image: {
Bytes: imageData
},
Attributes: [
'ALL',
]
};
rekognition.detectFaces(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
var table = "<table><tr><th>Low</th><th>High</th></tr>";
// show each face and build out estimated age table
for (var i = 0; i < data.FaceDetails.length; i++) {
table += '<tr><td>' + data.FaceDetails[i].AgeRange.Low +
'</td><td>' + data.FaceDetails[i].AgeRange.High + '</td></tr>';
}
table += "</table>";
document.getElementById("opResult").innerHTML = table;
}
});
}
//Loads selected image and unencodes image bytes for Rekognition DetectFaces API
function ProcessImage() {
AnonLog();
var control = document.getElementById("fileToUpload");
var file = control.files[0];
// Load base64 encoded image
var reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
var img = document.createElement('img');
var image = null;
img.src = e.target.result;
var jpg = true;
try {
image = atob(e.target.result.split("data:image/jpeg;base64,")[1]);
} catch (e) {
jpg = false;
}
if (jpg == false) {
try {
image = atob(e.target.result.split("data:image/png;base64,")[1]);
} catch (e) {
alert("Not an image file Rekognition can process");
return;
}
}
//unencode image bytes for Rekognition DetectFaces API
var length = image.length;
imageBytes = new ArrayBuffer(length);
var ua = new Uint8Array(imageBytes);
for (var i = 0; i < length; i++) {
ua[i] = image.charCodeAt(i);
}
//Call Rekognition
DetectFaces(imageBytes);
};
})(file);
reader.readAsDataURL(file);
}
//Provides anonymous log on to AWS services
function AnonLog() {
// Configure the credentials provider to use your identity pool
AWS.config.region = [REGION]; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: [IDENTITY],
});
// Make the call to obtain credentials
AWS.config.credentials.get(function () {
// Credentials will be available when this function is called.
var accessKeyId = AWS.config.credentials.accessKeyId;
var secretAccessKey = AWS.config.credentials.secretAccessKey;
var sessionToken = AWS.config.credentials.sessionToken;
});
}
</script>
</html>