mirror of
https://github.com/hexahigh/games.git
synced 2025-12-11 20:15:38 +01:00
updated ai model
This commit is contained in:
parent
2fe5b26692
commit
12588fc65f
@ -3,13 +3,15 @@
|
||||
<head>
|
||||
<title>Audio Classification</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.8.4/dist/tf.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/speech-commands@0.4.0/dist/speech-commands.min.js"></script>
|
||||
<script src="script.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<div>Audio Model</div>
|
||||
<button type="button" onclick="init()">Start</button>
|
||||
<div id="label-container"></div>
|
||||
<h1>Audio Classification</h1>
|
||||
<form>
|
||||
<input type="file" id="audioFile" name="audioFile"/>
|
||||
<button type="submit">Upload</button>
|
||||
</form>
|
||||
<section id="result"></section>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
@ -1,48 +1,63 @@
|
||||
// more documentation available at
|
||||
// https://github.com/tensorflow/tfjs-models/tree/master/speech-commands
|
||||
// Load the TensorFlow.js model
|
||||
async function loadModel() {
|
||||
const model = await tf.loadLayersModel("model/model.json");
|
||||
return model;
|
||||
}
|
||||
|
||||
// the link to your model provided by Teachable Machine export panel
|
||||
const URL = "./model/";
|
||||
function preprocessAudio(filename) {
|
||||
// Load audio file
|
||||
const audio = new Audio(filename);
|
||||
|
||||
async function createModel() {
|
||||
const checkpointURL = URL + "model.json"; // model topology
|
||||
|
||||
const recognizer = speechCommands.create(
|
||||
"BROWSER_FFT", // fourier transform type, not useful to change
|
||||
undefined, // speech commands vocabulary feature, not useful for your models
|
||||
checkpointURL);
|
||||
|
||||
// check that model and metadata are loaded via HTTPS requests.
|
||||
await recognizer.ensureModelLoaded();
|
||||
|
||||
return recognizer;
|
||||
}
|
||||
|
||||
async function init() {
|
||||
const recognizer = await createModel();
|
||||
const classLabels = recognizer.wordLabels(); // get class labels
|
||||
const labelContainer = document.getElementById("label-container");
|
||||
for (let i = 0; i < classLabels.length; i++) {
|
||||
labelContainer.appendChild(document.createElement("div"));
|
||||
}
|
||||
|
||||
// listen() takes two arguments:
|
||||
// 1. A callback function that is invoked anytime a word is recognized.
|
||||
// 2. A configuration object with adjustable fields
|
||||
recognizer.listen(result => {
|
||||
const scores = result.scores; // probability of prediction for each class
|
||||
// render the probability scores per class
|
||||
for (let i = 0; i < classLabels.length; i++) {
|
||||
const classPrediction = classLabels[i] + ": " + result.scores[i].toFixed(2);
|
||||
labelContainer.childNodes[i].innerHTML = classPrediction;
|
||||
}
|
||||
}, {
|
||||
includeSpectrogram: true, // in case listen should return result.spectrogram
|
||||
probabilityThreshold: 0.75,
|
||||
invokeCallbackOnNoiseAndUnknown: true,
|
||||
overlapFactor: 0.50 // probably want between 0.5 and 0.75. More info in README
|
||||
// Generate a Mel-scaled spectrogram
|
||||
const spectrogram = tf.tidy(() => {
|
||||
const audioTensor = tf.browser.fromAudio(audio);
|
||||
const sampleRate = audioTensor.sampleRate();
|
||||
const melSpectrogram = tf.signal.melSpectrogram(audioTensor, {
|
||||
sampleRate: sampleRate,
|
||||
hopLength: 512,
|
||||
nMels: 128,
|
||||
fMin: 0,
|
||||
fMax: 8000
|
||||
});
|
||||
const logMelSpectrogram = tf.math.log(melSpectrogram.add(1e-6));
|
||||
const normalizedSpectrogram = logMelSpectrogram.div(tf.max(logMelSpectrogram));
|
||||
return normalizedSpectrogram;
|
||||
});
|
||||
|
||||
// Stop the recognition in 5 seconds.
|
||||
// setTimeout(() => recognizer.stopListening(), 5000);
|
||||
}
|
||||
return spectrogram;
|
||||
}
|
||||
|
||||
|
||||
// Classify the audio using the loaded model
|
||||
async function classifyAudio(model, audioData) {
|
||||
const preprocessedData = preprocessAudio(audioData);
|
||||
const audioTensor = tf.tensor(preprocessedData);
|
||||
const predictions = model.predict(audioTensor);
|
||||
const predictedClass = predictions.argMax().dataSync()[0];
|
||||
return predictedClass;
|
||||
}
|
||||
|
||||
// Handle form submission
|
||||
document.querySelector("form").addEventListener("submit", async function(event) {
|
||||
event.preventDefault();
|
||||
const fileInput = document.getElementById("audioFile");
|
||||
const file = fileInput.files[0];
|
||||
const fileReader = new FileReader();
|
||||
|
||||
fileReader.onload = async function(event) {
|
||||
const audioData = event.target.result;
|
||||
|
||||
// Load the model
|
||||
const model = await loadModel();
|
||||
|
||||
// Classify the audio
|
||||
const classificationResult = await classifyAudio(model, audioData);
|
||||
|
||||
// Display the classification result
|
||||
const resultSection = document.getElementById("result");
|
||||
resultSection.textContent = `Class: ${classificationResult}`;
|
||||
};
|
||||
|
||||
// Read the uploaded audio file
|
||||
fileReader.readAsArrayBuffer(file);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user