-
Notifications
You must be signed in to change notification settings - Fork 56
/
TeachableMachine_video_localfile.html
154 lines (142 loc) · 4.61 KB
/
TeachableMachine_video_localfile.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
<!--
Author : ChungYi Fu (Kaohsiung, Taiwan) 2021/8/7 17:00
https://www.facebook.com/francefu
Try it!
https://fustyles.github.io/webduino/TensorFlow/TeachableMachine_video/TeachableMachine_video_localfile.html
-->
<!DOCTYPE html>
<head>
<title>Teachable Machine</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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://cdn.jsdelivr.net/npm/@teachablemachine/pose@0.8/dist/teachablemachine-pose.min.js"></script>
</head>
<body>
Local video file : <input id="selectVideo" type="file" accept="video/*"/>
<br>
<video id="video" width="320" height="240" src="" preload autoplay loop muted controls></video>
<br>
<br>
<canvas id="canvas" style="display:none"></canvas>
<br>
<table>
<tr>
<td>Model:</td>
<td>
<select id="Type">
<option value="pose">pose</option>
<option value="image">image</option>
</select>
</td>
</tr>
<tr>
<td>Model URL:</td>
<td><input type="text" id="modelPath" size="40" value=""></td>
</tr>
<tr>
<td>Flip Horizontal:</td>
<td><input type="checkbox" id="flipHorizontal"></td>
</tr>
<tr>
<td><a href="https://teachablemachine.withgoogle.com/train/" target="_blank">Train Model</a></td>
<td><button type="button" onclick="LoadModel()">Start Recognition</button></td>
</tr>
</table>
<br>
<div id="message"></div>
<div id="result" style="color:red"></div>
<script type="text/javascript">
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var selectVideo = document.getElementById('selectVideo');
var modelPath = document.getElementById('modelPath');
var result = document.getElementById('result');
var Type = document.getElementById('Type');
var flipHorizontal = document.getElementById('flipHorizontal');
let model;
async function LoadModel() {
if (modelPath.value=="") {
result.innerHTML = "Please input Model Link.";
return;
}
else
result.innerHTML = "Please wait for loading model.";
const URL = modelPath.value;
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
try {
if (Type.value=="image") {
model = await tmImage.load(modelURL, metadataURL);
}
else if (Type.value=="pose") {
model = await tmPose.load(modelURL, metadataURL);
}
maxPredictions = model.getTotalClasses();
result.innerHTML = "";
predict();
}
catch (e){
result.innerHTML = "Loading model failed.";
}
}
async function predict() {
if (flipHorizontal.checked) {
context.translate((canvas.width + video.width) / 2, 0);
context.scale(-1, 1);
context.drawImage(video, 0, 0, video.width, video.height);
context.setTransform(1, 0, 0, 1, 0, 0);
}
else
context.drawImage(video, 0, 0, video.width, video.height);
var data = "";
var maxClassName = "";
var maxProbability = "";
if (Type.value=="image")
var prediction = await model.predict(canvas);
else if (Type.value=="pose") {
var { pose, posenetOutput } = await model.estimatePose(canvas);
var prediction = await model.predict(posenetOutput);
}
if (maxPredictions>0) {
for (let i = 0; i < maxPredictions; i++) {
if (i==0) {
maxClassName = prediction[i].className;
maxProbability = prediction[i].probability;
}
else {
if (prediction[i].probability>maxProbability) {
maxClassName = prediction[i].className;
maxProbability = prediction[i].probability;
}
}
data += prediction[i].className + ": " + prediction[i].probability.toFixed(2) + "<br>";
}
result.innerHTML = data + "<br>Max Probability : <br>" + maxClassName + ", " + maxProbability.toFixed(2);
}
else
result.innerHTML = "";
setTimeout(function(){predict(); }, 100);
}
selectVideo.onchange = function (event) {
var target = event.target || window.event.srcElement;
var files = target.files;
if (files && files.length) {
var file = files[0];
if (video.canPlayType(file.type)!="") {
var fileURL = URL.createObjectURL(file);
video.src = fileURL;
}
else
result.innerHTML = "The file type is not supported.";
}
}
video.addEventListener( "loadedmetadata", function () {
canvas.setAttribute("width", video.videoWidth);
canvas.setAttribute("height", video.videoHeight);
}, false );
</script>
</body>
</html>