Hi Everyone! Not sure if this is the right place to ask this, but I’m trying to use a local video instead of the webcam for this Teachable Machine/TF js script. File2 is functional code that uses the webcam and passes the video through the model. File1 is my attempt at using a local video instead of the webcam, but doesn’t currently work. I’m seeking help with passing the video through the model in File1.
I’m still a JS beginner, so any help is greatly appreciated!
Thanks!
File 1:
<input type="file" name="file" id="fileItem" onchange="onChange()">
<button type="button" onclick="init()">Start</button>
<video id="webcam-container" controls></video>
<div id="label-container"></div>
<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 type="text/javascript">
const URL = "https://teachablemachine.withgoogle.com/models/fakemodel/";
var URLA = window.URL || window.webkitURL;
var video1 = document.getElementsByTagName('video')[0];
function onChange() {
var fileItem = document.getElementById('fileItem');
var files = fileItem.files;
var file = files[0];
var urli = URLA.createObjectURL(file);
console.log(file);
console.log(urli);
document.querySelector('#webcam-container').src = urli;
video1.load();
video1.onloadeddata = function() {
video1.play();
}
}
let model, webcam, labelContainer, maxPredictions;
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
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 delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function loop() {
webcam.update();
await delay(2000);
await predict();
window.requestAnimationFrame(loop);
}
async function predict() {
//sample user
var sampleusr = "Liam656656"
//add date
var now = new Date();
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
const classPrediction =
"Prediction" + ":" + prediction[i].className + "-" + prediction[i].probability.toFixed(2) + "; " + "Timestamp:" + now.toUTCString() + "; " + "User:" + sampleusr;
labelContainer.childNodes[i].innerHTML = classPrediction;
console.log(classPrediction);
}
}
</script>
File 2: (Functional)
<button type="button" onclick="init()">Start</button>
<div id="webcam-container"></div>
<div id="label-container"></div>
<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 type="text/javascript">
const URL = "https://teachablemachine.withgoogle.com/models/fakemodel/";
let model, webcam, labelContainer, maxPredictions;
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
const flip = true;
webcam = new tmImage.Webcam(200, 200, flip);
await webcam.setup();
await webcam.play();
window.requestAnimationFrame(loop);
document.getElementById("webcam-container").appendChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
for (let i = 0; i < maxPredictions; i++) {
labelContainer.appendChild(document.createElement("div"));
}
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function loop() {
webcam.update();
await delay(2000);
await predict();
window.requestAnimationFrame(loop);
}
async function predict() {
//sample user
var sampleusr = "Liam656656"
//add date
var now = new Date();
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
const classPrediction =
"Prediction" + ":" + prediction[i].className + "-" + prediction[i].probability.toFixed(2) + "; " + "Timestamp:" + now.toUTCString() + "; " + "User:" + sampleusr;
labelContainer.childNodes[i].innerHTML = classPrediction;
console.log(classPrediction);
}
}
</script>