/
latest.html
203 lines (171 loc) · 6.74 KB
/
latest.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Object Detection</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script> <!-- Load tensorflow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"> </script> <!-- Load the pre-trained model -->
</head>
<body onload="init();">
<!-- Title -->
<div class="container-fluid">
<h2>Object Detection</h2>
</div>
<!-- Instructions -->
<div class="container-fluid">
<h4>Instructions:</h4>
0. If using a mobile device, please orient your phone in landscape mode <br>
1. Click on "Start Webcam" <br>
2. Point Camera at scene of interest and click "Take Snapshot" <br>
3. Click on "Stop Webcam" to stop webcam feed (optional)<br>
4. Scroll Down and click on "Detect Objects"
</div>
<!-- Buttons -->
<div class="container-fluid" style="padding-top: 1em; padding-bottom: 1em;">
<button class="btn btn-default" onclick="startWebcam();"> Start WebCam</button>
<button class="btn btn-default" onclick="snapshot();">Take Snapshot</button>
<button class="btn btn-default" onclick="stopWebcam();"> Stop WebCam</button>
</div>
<div class="container-fluid" style="padding-top: 1em; padding-bottom: 1em;">
<video onclick="snapshot(this);" width=80% id="video" controls autoplay></video>
</div>
<!-- Canvas -->
<div class="container-fluid">
<h4>Snapshot:</h4>
<canvas id="myCanvas" width=600 height=400></canvas>
</div>
<!-- Detect Objects Button -->
<div class="container-fluid" style="padding-top: 1em; padding-bottom: 1em;">
<button class="btn btn-primary btn-lg" onclick="detect_objects();"> Detect Objects</button>
</div>
<!-- Status -->
<div class="container-fluid">
<span id="status"> </span>
</div>
<!-- Results -->
<div class="container-fluid">
<p id="showPreds"> </p>
</div>
<!-- All the JS -->
<script>
//--------------------
// GET USER MEDIA CODE
//--------------------
let video;
let webcamStream;
let constraints = {
video: {facingMode : "environment"},
audio: false
};
function startWebcam() {
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia (constraints)
.then(function(localMediaStream) {
video = document.querySelector('video');
//video = document.createElement('video');
//video.src = window.URL.createObjectURL(localMediaStream);
video.srcObject = localMediaStream;
webcamStream = localMediaStream;
})
.catch(function(err) {
console.log("getUserMedia error" + err);
});
} else {
console.log("getUserMedia not supported");
}
}
function stopWebcam() {
webcamStream.getTracks()[0].stop();
}
//---------------------
// TAKE A SNAPSHOT CODE
//---------------------
let canvas, ctx;
function init() {
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext('2d');
}
function snapshot() {
clear();
ctx.drawImage(video, 0,0, canvas.width, canvas.height);
}
function status(statusText) {
document.getElementById('status').textContent = statusText;
}
//----------------
// Detect Objects
//----------------
function printObject(o) {
var out = '';
for (i = 0; i < o.length; i++) {
for (var p in o[i]) {
if (p != 'bbox') {
out += p + ': ' + o[i][p];
}
out +='<br>'
}
}
return out;
}
//----------------
// Text to speech
//----------------
function textToSpeech(o){
for (i = 0; i < o.length; i++) {
let text = o[i]['class'] + ' with probability ' + o[i]['score'].toFixed(2) + ' ';
let msg = new SpeechSynthesisUtterance(text)
//let voices = window.speechSynthesis.getVoices();
//msg.voice = voices[4]
speechSynthesis.speak(msg);
}
}
//----------------
// Draw Boxes
//----------------
function drawBoxes(result) {
const image = document.getElementById('myCanvas');
// const c = document.getElementById('canvas');
const context = image.getContext('2d');
context.drawImage(image, 0, 0);
context.font = '10px Arial';
console.log('number of detections: ', result.length);
for (let i = 0; i < result.length; i++) {
context.beginPath();
context.rect(...result[i].bbox);
context.lineWidth = 1;
context.strokeStyle = 'red';
context.fillStyle = 'red';
context.stroke();
context.fillText(
result[i].score.toFixed(3) + ' ' + result[i].class, result[i].bbox[0],
result[i].bbox[1] > 10 ? result[i].bbox[1] - 5 : 10);
}
}
function clear(){
document.getElementById("showPreds").innerHTML = "";
status("");
}
//---------------------------------
// Call model and make predictions
//---------------------------------
function detect_objects() {
status("Detecting Objects, Please Wait...")
const img = document.getElementById('myCanvas');
// Load the model.
cocoSsd.load().then(model => {
// detect objects in the image.
model.detect(img).then(predictions => {
status("");
drawBoxes(predictions);
document.getElementById("showPreds").innerHTML = printObject(predictions);
textToSpeech(predictions);
});
});
}
</script>
</body>
</html>