-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrashDetection_Dev.js
More file actions
445 lines (388 loc) · 18.7 KB
/
TrashDetection_Dev.js
File metadata and controls
445 lines (388 loc) · 18.7 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
import SimulatorPlugins from "./reusable/SimulatorPlugins.js"
import StatusTable from "./reusable/StatusTable.js"
import MobileNotifications from "./reusable/MobileNotifications.js"
import { PLUGINS_APIKEY } from "./reusable/apikey.js"
import GoogleMapsLocation from "./reusable/GoogleMapsLocation.js"
async function fetchRowsFromSpreadsheet(spreadsheetId, apiKey) {
// Set the range to A1:Z1000
const range = "A1:Z1000";
// Fetch the rows from the Google Spreadsheet API
const response = await fetch(
`https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}?key=${encodeURIComponent(apiKey)}`
);
const json = await response.json();
// Get the headers from the first row
const headers = json.values[0];
// Convert the remaining rows to an array of objects
const rows = json.values.slice(1).map(row => {
const rowObject = {};
for (let i = 0; i < row.length; i++) {
rowObject[headers[i]] = row[i];
}
return rowObject;
});
return rows;
}
const plugin = ({widgets, simulator, vehicle}) => {
let simInterval = null
const loadSpreadSheet = async () => {
let sheetID = "1KopET4hpEUQqswqvBP1Nx2xljYE7Ws-6kRqH1rxGJv4";
fetchRowsFromSpreadsheet(sheetID, PLUGINS_APIKEY)
.then((rows) => {
SimulatorPlugins(rows, simulator)
})
}
widgets.register("Table",
StatusTable({
apis:["Vehicle.Connectivity.IsConnectivityAvailable","Vehicle.IsMoving", "Vehicle.Cabin.Seat.Row1.Pos1.IsOccupied", "Vehicle.CurrentLocation.Latitude", "Vehicle.CurrentLocation.Longitude"],
vehicle: vehicle,
refresh: 800
}))
let setLocationGlobal = null;
widgets.register("Map", (box) => {
const initialLocation = {
"lat": 46.477127,
"lng": 10.367829
}
GoogleMapsLocation(PLUGINS_APIKEY, box, initialLocation).then(({setLocation}) => {
setLocationGlobal = setLocation
})
})
// let container = null
// let resultImgDiv = null
// let resultRecDiv = null
// let imgWidth = 0;
// let imgHeight = 0;
//widgets.register("Result", (box) => {
// const container = document.createElement('div');
// container.innerHTML = `
// <div style="width: 50%; height: 50%; position: relative;">
// <div id="resultRec" style="position: absolute; border: 2px solid red; top: 0; left: 0; width: 0; height: 0; z-index: 2;"></div>
// <img id="resultImg" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 100%; max-height: 100%; z-index: 1;"
// src="https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2FLanding_AI_Logo_RGB_600.png?alt=media&token=9f6e445d-cf6d-4556-9240-4645a804b240"/>
// </div>
// `;
// const resultImgDiv = container.querySelector("#resultImg");
// const resultRecDiv = container.querySelector("#resultRec");
// box.injectNode(container);
//});
let container = null
let resultImgDiv = null
let resultRecDiv = null
let landingAiLogo = `https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2FAIOT_LAB_Logo_RGB_RZ.jpg?alt=media&token=f12df497-6f0b-4549-ac8b-c006de5d06d1`
let imgWidth = 0;
let imgHeight = 0;
widgets.register("Resultnew", (box) => {
const container = document.createElement('div');
container.innerHTML = `
<div style="width:100%;height:100%; position: relative;">
<div id="resultRec"
style="position:absolute;border: 2px solid red; top: 0; left: 0; width: 0; height: 0; z-index: 2;">
</div>
<img id="resultImg"
style="display:block;position:absolute;top:0%;left:0%;width:100%;height:100%; z-index: 1;"
src="https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2F2023-05-18_17h11_11.png?alt=media&token=81335e79-b793-4c81-ad40-1e6dc4d93f54"/>
<img id="logoImg"
style="position:absolute;top:5%;right:5%;width:30%;padding:6px; z-index: 3;object-fit:contain;background:white;"
src="${landingAiLogo}"/>
</div>
`;
resultImgDiv = container.querySelector("#resultImg");
resultRecDiv = container.querySelector("#resultRec");
box.injectNode(container);
});
widgets.register("InputImage", (box) => {
container = document.createElement('div')
container.innerHTML =
`
<div id="image" style="display:block;z-index:1;">
<img id="output" width="100%" height="100%"
src="https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2F0000.JPG?alt=media&token=4e2bb785-846f-4ee1-8774-0a101b473bca"/>
</div>
<div class="btn btn-color"
style="display:flex;z-index:2; position:absolute; width: 100%; bottom: 10px; opacity:85%; align-items:center; align-content:center; flex-direction:row; justify-content:center">
<button id="upload-btn"
style="background-color: rgb(104 130 158);padding: 10px 24px;cursor: pointer;float: left;margin:2px;border-radius:5px;font-size:1em;font-family:Lato;color: rgb(255, 255, 227);border:0px">
Upload
</button>
<button id="submit-btn"
style="background-color: rgb(104 130 158);padding: 10px 24px;cursor: pointer;float: left;margin:2px;border-radius:5px;font-size:1em;font-family:Lato;color: rgb(255, 255, 227);border:0px">
Submit
</button>
<input id="upload" type="file" accept="image/*" style="display:none">
</div>
`
const upload_btn = container.querySelector("#upload-btn")
const upload = container.querySelector("#upload")
upload_btn.onclick = () => {
if(upload) upload.click()
}
let imageEncoded = null
let file = null
const img_output = container.querySelector('#output');
const img = container.querySelector("#image")
upload.onchange = (event) => {
file = event.target.files[0]
img_output.src = URL.createObjectURL(event.target.files[0]);
img.style = "display: block"
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
var base_image = new Image();
base_image.src = img_output.src;
base_image.onload = function() {
canvas.width = base_image.width;
canvas.height = base_image.height;
imgWidth = base_image.width;
imgHeight = base_image.height;
ctx.drawImage(base_image, 0, 0);
imageEncoded = canvas.toDataURL('image/jpeg')
canvas.remove();
}
}
const imageUpload = async (image) => {
if(!file) return
const data = new FormData()
data.append('file', file)
const res = await fetch(
`https://predict.app.landing.ai/inference/v1/predict?endpoint_id=582a8a02-0357-412f-a31d-865549855e43`, {
method:'POST',
mode: 'cors',
headers: {
'apikey':'2a7uwh0n7kojn34iov5wo4epnt6qcum',
'apisecret':'klprn1tfzninfffpdsb7jqwt09zvbtvbughnljq6wq8pa2gavc2ku15ybysykp'
},
body: data
});
if (!res.ok) {
const message = `An error has occured: ${res.status}`;
throw new Error(message);
}
const response = await res.json()
return response
}
const submit_btn = container.querySelector("#submit-btn")
submit_btn.onclick = async () => {
const resData = await imageUpload(imageEncoded)
if(resultImgDiv) {
resultImgDiv.src = imageEncoded;
resultImgDiv.style.display='block'
}
if(resData) {
if(resData.backbonepredictions) {
for(let key in resData.backbonepredictions) {
let coordinates = resData.backbonepredictions[key].coordinates
if(resultImgDiv) {
resultImgDiv.src = imageEncoded;
let imgWidthDiv = resultImgDiv.width
let imgHeightDiv = resultImgDiv.height
let xmax = coordinates.xmax
let xmin = coordinates.xmin
let ymax = coordinates.ymax
let ymin = coordinates.ymin
let leftPercent = (1.0*xmin)/(imgWidth*1.0)
let topPercent = (1.0*ymin)/(imgHeight*1.0)
let widthPercent = (xmax-xmin)/(imgWidth*1.0)
let heightPercent = (ymax-ymin)/(imgHeight*1.0)
resultRecDiv.style.left = `${imgWidthDiv * leftPercent}px`
resultRecDiv.style.top = `${imgHeightDiv * topPercent}px`
resultRecDiv.style.width = `${imgWidthDiv * widthPercent}px`
resultRecDiv.style.height = `${imgHeightDiv * heightPercent}px`
}
break;
}
}
}
}
box.injectNode(container)
return () => { }
})
widgets.register("Video Panel", (box) => {
container = document.createElement('div')
container.innerHTML =
`
<div id="image" style="display:block;z-index:1;">
<img id="output" width="100%" height="100%"
src="https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2F0000.JPG?alt=media&token=4e2bb785-846f-4ee1-8774-0a101b473bca"/>
</div>
<!-- <div id="video" style="display:block; width:100%; height:100%"> -->
<!-- <video id="raw-video" width="100%" height="100%" style="object-fit:fill">
<source src="https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2Fwallet-detection%2Fwallet-detection-default.mp4?alt=media&token=e7a9ed4e-a463-4bd8-be45-af1a3e498f51" type="video/mp4"></source>
</video> -->
<!-- <div style="width:3em;cursor: pointer;position:absolute;bottom:45%;left:45%"" id="play-btn">
<img src="https://firebasestorage.googleapis.com/v0/b/digital-auto.appspot.com/o/media%2Fplay.svg?alt=media&token=4f68e20d-5c11-4e2c-9ae3-7f44ebdd0416" alt="play" style="filter: invert(100%);">
</div> -->
<!-- </div> -->
<div class="btn btn-color" style="display:flex;z-index:2; position:absolute; width: 100%; bottom: 10px; opacity:85%; align-items:center; align-content:center; flex-direction:row; justify-content:center">
<button id="upload-btn" style="background-color: rgb(104 130 158);padding: 10px 24px;cursor: pointer;float: left;margin:2px;border-radius:5px;font-size:1em;font-family:Lato;color: rgb(255, 255, 227);border:0px">
Upload
</button>
<button id="submit-btn" style="background-color: rgb(104 130 158);padding: 10px 24px;cursor: pointer;float: left;margin:2px;border-radius:5px;font-size:1em;font-family:Lato;color: rgb(255, 255, 227);border:0px">
Submit
</button>
<input id="upload" type="file" accept="image/*" style="display:none">
</div>
`
const upload_btn = container.querySelector("#upload-btn")
const upload = container.querySelector("#upload")
upload_btn.onclick = () => {
// container.querySelector("#upload").click()
if(upload) upload.click()
}
let imageEncoded = null
const img_output = container.querySelector('#output');
const img = container.querySelector("#image")
upload.onchange = (event) => {
img_output.src = URL.createObjectURL(event.target.files[0]);
img.style = "display: block"
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
var base_image = new Image();
base_image.src = img_output.src;
base_image.onload = function() {
canvas.width = base_image.width;
canvas.height = base_image.height;
imgWidth = base_image.width;
imgHeight = base_image.height;
ctx.drawImage(base_image, 0, 0);
imageEncoded = canvas.toDataURL('image/jpeg')
canvas.remove();
}
// const video = container.querySelector("#raw-video");
// container.querySelector("#video").style = "display: none"
// video.innerHTML = `<source src=${URL.createObjectURL(event.target.files[0])} type="video/mp4"></source>`
// video.load()
// container.querySelector("#video").style = "display: block"
}
const imageUpload = async (image) => {
image = image.replace('data:image/jpeg;base64,', '')
const apikey = 'h644blf0bp1g3k4d8ffkazchyfb412e'
const apisecret = 'yswm5qiyg0lhf45fo3pn1epsv5m01li03094wgwf7hgactxlq76kdd55whymfx'
const endpoint_id = '582a8a02-0357-412f-a31d-865549855e43'
const res = await fetch(
`https://aiotapp.net/walletdetection/image-upload`, {
method:'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
image,
apikey,
apisecret,
endpoint_id
})
});
// waits until the request completes...
if (!res.ok) {
const message = `An error has occured: ${res.status}`;
throw new Error(message);
}
//conver response to json
const response = await res.json()
let op = response["output"]
op = op.replaceAll('\"', "'").replaceAll('"{', '{').replaceAll('"}','}').replaceAll("'", '"')
return op
}
const videoUpload = async () => {
const data = new FormData()
data.append('file', upload.files[0])
console.log(data)
const res = await fetch(
`https://predict.app.landing.ai/inference/v1/predict?endpoint_id=582a8a02-0357-412f-a31d-865549855e43`, {
method:'POST',
mode: 'cors',
headers: {
'Content-Type': 'multipart/form-data',
'apikey':'h644blf0bp1g3k4d8ffkazchyfb412e',
'apisecret':'yswm5qiyg0lhf45fo3pn1epsv5m01li03094wgwf7hgactxlq76kdd55whymfx'
},
body: data
});
// waits until the request completes...
if (!res.ok) {
const message = `An error has occured: ${res.status}`;
throw new Error(message);
}
//convert response to json
const response = await res.json()
return response
}
const submit_btn = container.querySelector("#submit-btn")
submit_btn.onclick = async () => {
// console.log(setLocationGlobal)
const res = await imageUpload(imageEncoded)
// console.log(resultImgDiv)
// console.log(imageEncoded)
if(res) {
let resData = JSON.parse(res)
// console.log(resData)
if(resData && resData.backbonepredictions) {
for(let key in resData.backbonepredictions) {
let coordinates = resData.backbonepredictions[key].coordinates
// console.log("res.backbonepredictions.coordinates", coordinates)
if(resultImgDiv) {
resultImgDiv.src = imageEncoded;
let imgWidthDiv = resultImgDiv.width
let imgHeightDiv = resultImgDiv.height
let xmax = coordinates.xmax
let xmin = coordinates.xmin
let ymax = coordinates.ymax
let ymin = coordinates.ymin
let leftPercent = (1.0*xmin)/(imgWidth*1.0)
let topPercent = (1.0*ymin)/(imgHeight*1.0)
let widthPercent = (xmax-xmin)/(imgWidth*1.0)
let heightPercent = (ymax-ymin)/(imgHeight*1.0)
resultRecDiv.style.left = `${imgWidthDiv * leftPercent}px`
resultRecDiv.style.top = `${imgHeightDiv * topPercent}px`
resultRecDiv.style.width = `${imgWidthDiv * widthPercent}px`
resultRecDiv.style.height = `${imgHeightDiv * heightPercent}px`
}
break;
}
}
}
}
/* const play_btn = container.querySelector("#play-btn")
play_btn.onclick = () => {
container.querySelector("#raw-video").play();
} */
box.injectNode(container)
return () => {
clearInterval(simInterval)
}
})
let mobileNotificationsGlobal = null;
widgets.register("Mobile",
(box) => {
const {printNotification} = MobileNotifications(
{
box:box,
backgroundColor: "rgb(0 80 114)"
}
)
mobileNotificationsGlobal = printNotification
})
let count = 0;
return {
load_data: async () => {
loadSpreadSheet()
},
mobile_notification: (message) => {
mobileNotificationsGlobal(message)
},
start_simulation: (time) => {
simInterval = setInterval(async () => {
const lat = parseFloat(await vehicle.CurrentLocation.Latitude.get())
const lng = parseFloat(await vehicle.CurrentLocation.Longitude.get())
setLocationGlobal({lat, lng})
if(count === 0) {
container.querySelector("#raw-video").play()
count++
}
await vehicle.Next.get()
}, time)
}
}
}
export default plugin;