-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv.html
144 lines (120 loc) · 4.33 KB
/
cv.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ramudroid</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/[email protected]/dist/teachablemachine-image.min.js"></script>
</head>
<body>
<h2>Garbage Identification and Classification Neural Network model for Ramudroid</h2>
<table>
<td>
<input type='file'/>
<video controls height="500px" id="sample_video"></video>
<canvas id="sample_canvas"></canvas>
</td>
<td>
<h3> catagory :
<div id="catagory"></div>
</h3>
<div id="label-container"></div>
</td>
</table>
<script type="text/javascript">
// document.addEventListener('DOMContentLoaded', function(){
window.addEventListener('load', function () {
var canvas = document.getElementById('sample_canvas');
var context = canvas.getContext('2d');
var cw = Math.floor(canvas.clientWidth / 100);
var ch = Math.floor(canvas.clientHeight / 100);
canvas.width = cw;
canvas.height = ch;
document.querySelector('input[type="file"]').addEventListener('change', function () {
var video = document.getElementById('sample_video');
if (this.files && this.files[0]) {
var URL = window.URL || window.webkitURL
video.src = URL.createObjectURL(this.files[0]); // set src to blob url
}
video.addEventListener('play', function () {
draw(this, context, cw, ch);
predict();
}, false);
video.play();
});
});
function draw(v, c, w, h) {
if (v.paused || v.ended) return false;
c.drawImage(v, 0, 0, w, h);
predict();
setTimeout(draw, 30, v, c, w, h);
}
// the link to your model provided by Teachable Machine export panel
const URL = "./Model_Litter_Identification_classification/";
let model, labelContainer, maxPredictions;
// load the model and metadata
async function loadmodel() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
}
(async () => {
await loadmodel();
maxPredictions = model.getTotalClasses();
labelContainer = document.getElementById("label-container");
for (let i = 0; i < maxPredictions; i++) {
labelContainer.appendChild(document.createElement("div"));
}
})();
var catagorytxt = document.getElementById('catagory');
async function predict() {
// predict can take in an image, video or canvas html element
const prediction = await model.predict(sample_canvas);
// console.log(" prediction => ", prediction);
var clspred = {};
for (let i = 0; i < maxPredictions; i++) {
var c= prediction[i].className;
if(!clspred.hasOwnProperty(c))
clspred[c]=0;
var val = prediction[i].probability.toFixed(2);
clspred[c].name=c;
clspred[c].val=val;
//showMaxCatagory(clspred);
// console.log("prediction.className ", cls);
const classPrediction = c + ": " + val;
labelContainer.childNodes[i].innerHTML = classPrediction;
}
}
function showMaxCatagory(clspred){
var maxpred=0;
var maxcls="O";
for( var obj in clspred){
let maxpred_before=maxpred;
maxpred = Math.max(maxpred, obj.val);
if(maxpred > maxpred_before)
maxcls = obj;
}
var catagory = maxcls.substr(0, 1);
switch (catagory) {
case "O":
catagorytxt.innerHTML = "Organic Waste";
break;
case "R":
catagorytxt.innerHTML = "Recyclable Waste";
break;
case "N":
catagorytxt.innerHTML = "Non-Recyclable Waste";
break;
case "M":
catagorytxt.innerHTML = "Medical Waste";
break;
case "E":
catagorytxt.innerHTML = "Electronic Waste";
break;
default:
break;
}
}
</script>
</body>
</html>