-
Notifications
You must be signed in to change notification settings - Fork 7
/
whoswho.html
246 lines (217 loc) · 8.02 KB
/
whoswho.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html lang="en">
<head>
<title>Celebrity Recognition</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<style>
html,body {
height: 95%;
}
.topHeader
{
background-color: #232f3e;
padding: 10px;
border-bottom: solid 1px #cacaca;
color: white
}
.imgCell{
width: 1200px;
}
.links{
max-height: 100%;
overflow-y: scroll;
}
.labels{
margin: 5px;
padding: 10px;
}
.lblButton{
margin-top: 10px;
}
.mdbox{
font-weight: bold;
}
</style>
</head>
<body>
<div class="container-fluid h-100">
<!--Top Header-->
<div class="row topHeader">
<div class="col-md-6">
<h4>Who's Who in Media - Celebrity Recognition</h4>
</div>
</div>
<div class="row h-100">
<div class="col-sm-3 links">
<br>
<div class="row alert alert-secondary ml-1 mr-1">
<div class="col-sm-12">
<div id='celebrities-timeline-title'></div>
</div>
</div>
<div id='celebrities-timeline'></div>
</div>
<div class="col-sm-9 links">
<br>
<div class="row alert alert-secondary ml-1 mr-1 align-middle">
<div class="col-sm-12 align-middle">
<button type="button" id="btnRecognizeCelebrities" onclick="recognizeCelebrities()" class="btn btn-primary">Recognize Celebrities</button>
<span id="infoCard" class="align-middle"></span>
</div>
</div>
<div class="row text-center">
<div class="col-sm">
<video id="player" width="1280" height="720" controls ontimeupdate="onVideoTimeUpdate()">
<source src="media.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<br>
</div>
</div>
<div class="row">
<div class="col-sm links">
<canvas id="canvas" width="1280px" height="720px" style="display: none" crossorigin="anonymous"></canvas>
</div>
</div>
</div>
</div>
</div>
<script>
//Update BaseURL with Invoke URL of your API Gateway Endpoint
var apiGatewayBaseUrl = 'https://8kp7jh2rgi.execute-api.us-east-2.amazonaws.com/prod';
var apiGatewayVideoUrl = apiGatewayBaseUrl + '/GetCelebrityRecognition?v=media.mp4';
var apiGatewayImageUrl = apiGatewayBaseUrl + '/RecognizeCelebrities';
var player = document.getElementById("player")
var canvas = document.getElementById("canvas")
var context=canvas.getContext('2d');
var btnRecognize = document.getElementById("btnRecognizeCelebrities")
var infoCard = document.getElementById("infoCard")
var celebrities = {}
celebrityArrival = []
function onVideoTimeUpdate(){
currentTime = player.currentTime*1000
cinfo = ""
if(celebrityArrival){
for(ec in celebrityArrival){
celeb = celebrityArrival[ec].Celebrity
if(celeb.Timestamp < currentTime){
cinfo += '[ <a target="_blank" href="' + celeb.Celebrity.Urls + '">' + celeb.Celebrity.Name + '</a> ] '
}
else{
break;
}
}
if(cinfo){
cinfo = 'Celebrity Arrival: ' + cinfo
}
infoCard.innerHTML = cinfo
}
}
function recognizeCelebrities(){
player.pause();
btnRecognize.innerText = "Recognizing celebrities..."
btnRecognize.disabled =true;
context.fillRect(0,0,canvas.width,canvas.height);
context.drawImage(player,0,0,canvas.width,canvas.height);
var dataURL = canvas.toDataURL('image/jpeg', 1.0);
dataURL = dataURL.replace(/^data:image\/(png|jpg|jpeg);base64,/, "")
$.ajax({
type: "POST",
url: apiGatewayImageUrl,
data: dataURL
}).then(function(data) {
infoCard.innerHTML = "";
var outputText = JSON.stringify(data).replace(/\\/g, "");
var jsonResponse = JSON.parse(data.replace(/\\/g, ""));
var displayURL = "";
var cinfo = ""
if(jsonResponse.CelebrityFaces && jsonResponse.CelebrityFaces.length!=0)
{
for (ef in jsonResponse.CelebrityFaces){
celeb = jsonResponse.CelebrityFaces[ef]
if(celeb.Name && celeb.Urls){
cinfo += '[ <a target="_blank" href="' + celeb.Urls + '">' + celeb.Name + '</a> ] '
}
}
infoCard.innerHTML = cinfo
}
else{
infoCard.innerHTML = "No celebrities detected";
}
btnRecognize.disabled =false;
btnRecognize.innerText = "Recognize Celebrities"
});
}
function setVideo(ts){
player.currentTime = ts/1000;
player.play()
}
function renderCelebrityTimeline(key, celebrityDetails, i){
card = '<div class="card"> <div class="card-header" id="' + 'heading' + i + '"> <h5 class="mb-0"> '
card += '<button class="btn btn-link collapsed" data-toggle="collapse"'
card += ' data-target="#collapse' + i + '"'
card += ' aria-expanded="false"'
card += ' aria-controls="collapse' + i + '">'
card += '<strong>' + celebrityDetails[0].Celebrity.Name + '</strong>'
card += ' </button> </h5> </div>'
card += ' <div id="collapse' + i + '" class="collapse"'
card += ' aria-labelledby="heading' + i + '" data-parent="#accordion"> <div class="card-body">'
card += '<strong>Fun Facts:</strong><br><a target="_blank" href="' + celebrityDetails[0].Celebrity.Urls[0] + '">' + celebrityDetails[0].Celebrity.Urls[0] + '<a><br><br><strong>Timeline:</strong><hr/>'
pt = -1
for(ei in celebrityDetails){
ct = Math.round(celebrityDetails[ei].Timestamp/1000)
if(pt !== ct){
card += '<button type="button" class="btn btn-primary" style="width:100%" onclick="setVideo(' + celebrityDetails[ei].Timestamp + ');">' + Math.round(celebrityDetails[ei].Timestamp/1000) + '</button><br><br>'
}
pt = ct
}
card += ' </div> </div>'
return card;
}
function parseCelebrities(){
window.uniqueCelebs = {}
for (ec in celebrities.Celebrities){
c = celebrities.Celebrities[ec]
if(c.Celebrity && c.Celebrity.Id){
if(c.Celebrity.Id in uniqueCelebs){
uniqueCelebs[c.Celebrity.Id].push(c)
}
else{
uniqueCelebs[c.Celebrity.Id] = [c]
celebrityArrival.push({'Timestamp' : c.Timestamp , 'Celebrity' : c })
}
}
}
}
function renderTimeline(){
parseCelebrities();
$("#celebrities-timeline-title").html("<h5>Recognized Celebrities (" + Object.keys(uniqueCelebs).length + ")</h5>");
timelineui = '<div id="accordion">'
i = 0
for(key in uniqueCelebs){
timelineui += renderCelebrityTimeline(key, uniqueCelebs[key], i)
i++;
}
timelineui += '</div>'
$("#celebrities-timeline").html(timelineui);
}
function startApp(){
$.ajax({
type: "GET",
url: apiGatewayVideoUrl,
}).then(function(data) {
celebrities = JSON.parse(data)
if(celebrities.Celebrities && celebrities.Celebrities.length > 0){
renderTimeline();
}
});
}
startApp()
</script>
</body>
</html>