-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.html
More file actions
480 lines (463 loc) · 22.4 KB
/
main.html
File metadata and controls
480 lines (463 loc) · 22.4 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<!DOCTYPE html>
<html id="code" lang="en" oncontextmenu="return false">
<head>
<meta charset="utf-8">
<title>WaveOut</title>
<meta name="description" content="An AI-powered self-checkout system designed to be user friendly and fast.">
<meta name="author" content="Kea-Roy Ong">
<meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Ensures optimal rendering on mobile devices. -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <!-- Optimal Internet Explorer compatibility -->
<link rel="icon" type="image/png" href="icon.png">
<link rel="stylesheet" href="styles.css">
<!--Write the Items Below With | As A Separator. Ex: Milo|Yeo's|CocoPops -->
<div id="items">
Milo|Yeo's|CocoPops
</div>
<!--Write the Prices Below With | As A Separator. Ex: 3.0|2.0|1.0 -->
<div id="prices">
3.0|2.0|1.0
</div>
</head>
<body>
<!--Item information overlay-->
<div id="itemInfoPopUp-container">
<div id="itemInfoPopUp">
<!--Item info-->
<div id="popUpItemName" class="center textImportant"></div>
<!--Buttons-->
<div class="center">
<button id="minusQty" onclick="minusQty()" class="center">-</button>
<input id="qty" class="center" type="number" step="1" min="1" max="" name="quantity" value="1" title="Qty" size="4">
<button id="plusQty" onclick="plusQty()" class="center">+</button>
</div>
<button id="popUpBack" class="buttonImportant center">Done</button>
</div>
</div>
<!--Confirmation overlay-->
<div id="itemConfirmPopUp-container">
<div id='itemConfirmPopUp'>
<!--Confirmation-->
<div class="center textImportant" id="result-container"></div>
<!--Button-->
<button id="itemConfirmPopUpYes" class="buttonImportant center" onclick="itemConfirmed()">Yes</button>
<button id="itemConfirmPopUpNo" class="buttonNormal center" onclick="itemWrong()">No</button>
</div>
</div>
<!--Payment overlay-->
<div id="paymentPopUp-container">
<div id='paymentPopUp'>
<!--Confirmation-->
<div class="center textImportant" id="belowCorrect">Select a Payment Option.</div>
<!--Cart-->
<div class="center textNormal" id="paymentTotal"></div>
<!--Button-->
<div id="paymentOptions" class="center">
<!--<button id="Mastercard" class="buttonNormal center" onclick="payViaMasterCard()">Mastercard</button>-->
<div id="paypal-button-container"></div>
<div id="paymentMessage-container"></div>
<!--<button id="Paypal" class="buttonNormal center" onclick="payViaPaypal()">Paypal</button>-->
</div>
<div class="center">
<button class="buttonImportant" onclick="closePaymentPopUp()">Back</button>
</div>
</div>
</div>
<!-- Help overlay -->
<div id="helpPopUp-container">
<div id="helpPopUp">
<div class="center textImportant">Auto Suggestion:</div>
<div class="center textNormal" id="instructionText">Text here...</div>
<div class="center">
<button class="buttonImportant" onclick="closeHelp()">Close</button>
</div>
</div>
</div>
<!--Header-->
<div class="header">
<!--Title-->
<div class="title">WaveOut</div>
<!--Subtitle-->
<div class="subtitle">Click start to begin scanning.</div>
<button class="helpIcon" onclick="help()">Need help?</button>
</div>
<!--Space-->
<div></div>
<div></div>
<!--Left Column-->
<div style="float:left;width:50%;">
<!--Start button-->
<button class="buttonNormal center animated tada delay-2s" id="Start" style="display:block;" type="button" onclick="init()">Start</button>
<!--Loading-->
<div class="textImportant center" id="Loading" style="display:none">Loading...</div>
<!--Instructions-->
<div class="textNormal center" style="display:none" id="Instructions">Show the Item to the Camera</div>
<!--Webcam-->
<div class="center" id="webcam-container"></div>
<!--Finish button-->
<div class="buttonImportant center" id="Finished" style="display:none" onclick="finished()">I've Finished Scanning All My Items</div>
<!--Labels and Results-->
<div class="center" id="label-container"></div>
</div>
<!--Right Column-->
<div style="float:right;width:50%;">
<!--Shopping Cart-->
<div class="center animated tada delay-3s" id="cart-container" style="overflow-x:auto;">
<table id="tableCart" class="center">
<thead>
<th id="realHeader" colspan="3">Your Cart (Click Item to Modify)</th>
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr id="temp_message_1">
<td colspan="3">Your items will be added here</td>
</tr>
</tbody>
<tfoot>
<th id="realFooter" colspan="3">Total: $0.00</th>
</tfoot>
</table>
</div>
<!--Checkout Button-->
<button class="buttonImportant center animated tada delay-4s" id="Pay" style="display:block;" type="button" onclick="pay()">Pay</button>
</div>
<!--Scripts-->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script>
<script src="https://www.paypal.com/sdk/js?currency=SGD&client-id=AT-RtHUmb4O6XZufeavRw3VeyivsOzq-Yoq1ey05TakIhXbSAl3FPcFbukiTOe4IWE_MofC3OvlFqJnQ">// Required. Replace SB_CLIENT_ID with your sandbox client ID.</script>
<script type="text/javascript">
// Note: Assign item from stop to the innertext of a new div.
// More API functions here:
// https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image
// the link to your model provided by Teachable Machine export panel
const URL = "https://teachablemachine.withgoogle.com/models/TBgeuZKo/";
// instantiates variables
let model, webcam, labelContainer, maxPredictions;
//RETRIEVE ITEMS AND PRICES
var items = new Array();
var prices = new Array();
var itemTextO = document.getElementById("items").innerText.trim();
items = itemTextO.split("|");
var priceTextO = document.getElementById("prices").innerText.trim();
prices = priceTextO.split("|");
console.log("items | prices");
for(let i = 0; i<items.length;i++){
items[i] = items[i].trim();
prices[i] = prices[i].trim();
console.log(items[i],prices[i]);
}
var totalSaved = 0.00;
var firstTimeRunning = true;
var stopScanning = false;
var helpOpen = false;
const predictedItem = new Array(); //[0] is name, [1] is probability, [2] is price
var frames = 0;
//--------------------FUNCTIONS BELOW--------------------
// function sets up webcam and begins scanning process.
async function init() {
//-----load image model-------------------------
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
document.getElementById("Start").style.display = "none";
document.getElementById("Loading").style.display = "block";
// load the model and metadata
// Refer to tmImage.loadFromFiles() in the API to support files from a file picker
// or files from your local hard drive
// Note: the pose library adds "tmImage" object to your window (window.tmImage)
if(firstTimeRunning){
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
}
// Convenience function to setup a webcam
const flip = true; // whether to flip the webcam
if(firstTimeRunning){
webcam = new tmImage.Webcam(400, 400, flip); // width, height, flip
await webcam.setup(); // request access to the webcam
await webcam.play();
firstTimeRunning = false;
}
document.getElementById("Loading").style.display = "none";
window.requestAnimationFrame(loop);
//document.getElementById("Stop").style.display = "block";
document.getElementById("Instructions").style.display = "block";
document.getElementById("Finished").style.display = "block";
// append elements to the DOM
document.getElementById("webcam-container").innerHTML='';
document.getElementById("webcam-container").appendChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
for (let i = 0; i < maxPredictions; i++) { // and class labels
labelContainer.appendChild(document.createElement("div"));
}
}
// function runs when stop button is pressed
async function ready(){
//-----UPDATE: BUTTONS AND STOP CAMERA-----
//document.getElementById("Start").style.display = "block";
//document.getElementById("Stop").style.display = "none";
//webcam.stop();
//-----OUTPUT: ITEMNAME-----
document.getElementById("result-container").innerText = "Is this your item?\n" + predictedItem[0];
document.getElementById("itemConfirmPopUp-container").style.display = "block";
//-----Remove Temp Message-----
var temp_message_1 = document.getElementById("temp_message_1");
if(temp_message_1 != null){
document.getElementById("tableCart").tBodies[0].deleteRow(0);
}
//-----TABLE: ADD ITEM AND PRICE-----
var theCart = document.getElementById("tableCart");//reference to cart
var rows = theCart.tBodies[0].rows;
var exists = false;
for(let i = 0; i<rows.length; i++){
//if it is the item
if(rows[i].cells[0].innerText == predictedItem[0]){
var quan = parseInt(rows[i].cells[2].innerText);
rows[i].cells[2].innerText = quan+1;
exists = true;
break;
}
}
if(!exists){
var newRow = theCart.tBodies[0].insertRow(theCart.tBodies[0].rows.length);//add a new row
newRow.insertCell(0).appendChild(document.createTextNode(predictedItem[0]));//add the itemName
newRow.insertCell(1).appendChild(document.createTextNode("$" + predictedItem[2].toFixed(2)));//add the price
newRow.insertCell(2).appendChild(document.createTextNode("1")); //default quantity
var temp561 = predictedItem[0];
newRow.addEventListener('click', function(){openItemInfoPopUp(temp561)}); //onclick function
}
//-----TABLE: UPDATE TOTAL-----
await updateTotal();
}
// function updates total in cart
async function updateTotal(){
var total = 0.00;
var theCart = document.getElementById("tableCart");//reference to cart
for(let i = 0; i<theCart.tBodies[0].rows.length; i++){
var rowies = theCart.tBodies[0].rows;
for(let x = 0; x<parseInt(rowies[i].cells[2].innerText); x++){
total += parseFloat(rowies[i].cells[1].innerHTML.substring(1));
}
}
console.log(total);
totalSaved = total;
document.getElementById("realFooter").innerText = "Total: $" + total.toFixed(2);
}
// function loops over and over until prediction is determined
async function loop() {
webcam.update(); // update the webcam frame
const prediction = await model.predict(webcam.canvas);
prediction.sort(function compare(a,b){
if(a.probability>b.probability){return -1;}
if(a.probability==b.probability){return 0;}
else{return 1;}
});
console.log(prediction);
var difference = prediction[0].probability.toFixed(2) - prediction[1].probability.toFixed(2);
if(difference >= 0.05 && frames>50 && !helpOpen){
frames = 0;
console.log(prediction[0].className + ": " + prediction[0].probability.toFixed(2));
console.log(prediction[1].className + ": " + prediction[1].probability.toFixed(2));
console.log(prediction[2].className + ": " + prediction[2].probability.toFixed(2));
predictedItem[0] = prediction[0].className;
predictedItem[1] = prediction[0].probability;
predictedItem[2] = parseFloat(prices[items.indexOf(predictedItem[0])]);
await ready();
}
else{
console.log("fail | " + difference + " | " + frames + "|" + helpOpen);
if(helpOpen == true){
frames = 0;
}
else{
frames++;
}
if(!stopScanning){
window.requestAnimationFrame(loop);
}
}
}
// function runs when yes is clicked in confirmation dialog
async function itemConfirmed(){
//keep it in the cart.
//close popup
document.getElementById("itemConfirmPopUp-container").style.display = "none";
//scan next item
await init();
}
// function runs when no is clicked in confirmation dialog
async function itemWrong(){
//remove item from cart.
var rows = document.getElementById("tableCart").tBodies[0].rows;
for(let i = 0; i<rows.length; i++){
//if it is the item
if(rows[i].cells[0].innerText == predictedItem[0]){
var quan = parseInt(rows[i].cells[2].innerText);
//if the Quantity is greater than 1 (multiple)
if(quan > 1.0){
//remove one quantity
rows[i].cells[2].innerText = quan-1;
}
//otherwise delete the row
else{
document.getElementById("tableCart").tBodies[0].deleteRow(i);
await updateTotal();
break;
}
}
}
//close popup.
document.getElementById("itemConfirmPopUp-container").style.display = "none";
//rescan?
await init();
}
// function runs when finished button is pressed
async function finished(){
stopScanning = true;
webcam.stop();
document.getElementById("webcam-container").innerHTML = "";
document.getElementById("Finished").style.display = "none";
document.getElementById("Instructions").innerText = "Good Work! Now check that your items are correct on the right.";
}
// function removes one quantity of the currently looked at item
async function minusQty(){
var currentQty = document.getElementById("qty").value;
if(currentQty > 0){
document.getElementById("qty").value--;
}
}
// function adds one quantity of the currently looked at item
async function plusQty(){
document.getElementById("qty").value++;
}
// function opens the itemInfoPopUp of specified item name
async function openItemInfoPopUp(itemName){
console.log(itemName);
document.getElementById("itemInfoPopUp-container").style.display = "block";
document.getElementById("popUpItemName").innerText = itemName;
var rows = document.getElementById("tableCart").tBodies[0].rows;
for(let i = 0; i<rows.length; i++){
//if it is the item
if(rows[i].cells[0].innerText == itemName){
var quan = parseInt(rows[i].cells[2].innerText);
document.getElementById("qty").value = quan;
break;
}
}
var old_element = document.getElementById("popUpBack");
var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);
new_element.setAttribute("id","popUpBack");
document.getElementById("popUpBack").addEventListener("click",function(){closeItemInfoPopUp(itemName)})
}
// function closes the itemInfoPopUp of specified item name
async function closeItemInfoPopUp(itemName){
//display none
document.getElementById("itemInfoPopUp-container").style.display = "none";
//find row, then adjust quantity or delete
var rows = document.getElementById("tableCart").tBodies[0].rows;
for(let i = 0; i<rows.length; i++){
//if it is the item
if(rows[i].cells[0].innerText == itemName){
console.log("moo");
//if the Quantity is greater than 1 (multiple)
if(document.getElementById("qty").value > 0){
rows[i].cells[2].innerText = document.getElementById("qty").value;
}
//otherwise delete the row
else{
document.getElementById("tableCart").tBodies[0].deleteRow(i);
}
break;
}
}
await updateTotal();
}
// function closes paymentPopUp
async function closePaymentPopUp(){
//display none
document.getElementById("paymentPopUp-container").style.display = "none";
}
// [not used] function runs the webcam image through the image model
async function predict() {
// predict can take in an image, video or canvas html element
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
const classPrediction =
prediction[i].className + ": " + prediction[i].probability.toFixed(2);
labelContainer.childNodes[i].innerHTML = classPrediction;
}
}
//function runs when pay button is pressed
async function pay(){
//remove previous renders
document.getElementById("paypal-button-container").innerHTML = "";
//window covers screen with payment steps
paypal.Buttons({
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
"currency_code": "SGD",
"value": totalSaved
}
}]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
document.getElementById("paymentMessage-container").innerText = 'Transaction was successful! Have a nice day, ' + details.payer.name.given_name;
setTimeout(function(){location.reload();},5000);
});
},
onCancel: function(data,actions) {
return actions.order.capture().then(function(details){
document.getElementById("paymentMessage-container").innerText = 'Transaction failed. Please try again.';
});
},
onError: function(data,actions) {
return actions.order.capture().then(function(details){
document.getElementById("paymentMessage-container").innerText = 'Transaction failed. Please try again.';
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
document.getElementById("paymentPopUp-container").style.display = "block";
document.getElementById("paymentTotal").innerText = document.getElementById("realFooter").innerText;
//let clonedTable = document.getElementById("tableCart").cloneNode(true);
//clonedTable.setAttribute('id','tableCartClone');
//clonedTable.style = "border-collapse: collapse;width:25%;text-align: center;vertical-align: center;border: 3px solid var(--color_grey);position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);-ms-transform: translate(-50%,-50%);";
//document.getElementById("paymentPopUp").insertBefore(clonedTable,document.getElementById("paymentOptions"));
}
//function runs when the help button is pressed
async function help(){
console.log("LLBHAOUGHA");
helpOpen = true;
document.getElementById("helpPopUp-container").style.display = "block";
var temp_message_1 = document.getElementById("temp_message_1");
var instruction = document.getElementById("instructionText");
if(temp_message_1 != null){ //the temp message still there, then haven't scanned item
//Instruction: To scan your items, click the start button, allow camera permission, then show your item to the camera and wait until it recognizes it.
instruction.innerText = "To scan your items, click the start button, allow camera permission, then show your item to the camera and wait until it recognizes it.";
}
else if(stopScanning == true){ //they finished scanning
//Instruction: Check your items are correct in the cart. You can click on the items in the cart to modify the quanitity. Once you are ready, click the pay button.
instruction.innerText = "Check your items are correct in the cart. You can click on the items in the cart to modify the quanitity. Once you are ready, click the pay button.";
}
else{ //they are still scanning
//Instructions: To scan your items, show your item to the camera and wait until it recognizes it. Try to show only one object to the camera at a time and remove any other objects from the frame.
instruction.innerText = "To scan your items, show your item to the camera and wait until it recognizes it. Try to show only one object to the camera at a time and remove any other objects from the frame.";
}
}
//function runs when the help dialog is closed
async function closeHelp(){
document.getElementById("helpPopUp-container").style.display = "none";
helpOpen = false;
}
</script>
</body>
</html>