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>
|
<head>
|
||||||
<title>Audio Classification</title>
|
<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/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>
|
<script src="script.js" defer></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>Audio Model</div>
|
<h1>Audio Classification</h1>
|
||||||
<button type="button" onclick="init()">Start</button>
|
<form>
|
||||||
<div id="label-container"></div>
|
<input type="file" id="audioFile" name="audioFile"/>
|
||||||
|
<button type="submit">Upload</button>
|
||||||
|
</form>
|
||||||
|
<section id="result"></section>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@ -1,48 +1,63 @@
|
|||||||
// more documentation available at
|
// Load the TensorFlow.js model
|
||||||
// https://github.com/tensorflow/tfjs-models/tree/master/speech-commands
|
async function loadModel() {
|
||||||
|
const model = await tf.loadLayersModel("model/model.json");
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
// the link to your model provided by Teachable Machine export panel
|
function preprocessAudio(filename) {
|
||||||
const URL = "./model/";
|
// Load audio file
|
||||||
|
const audio = new Audio(filename);
|
||||||
|
|
||||||
async function createModel() {
|
// Generate a Mel-scaled spectrogram
|
||||||
const checkpointURL = URL + "model.json"; // model topology
|
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;
|
||||||
|
});
|
||||||
|
|
||||||
const recognizer = speechCommands.create(
|
return spectrogram;
|
||||||
"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;
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
async function init() {
|
// Handle form submission
|
||||||
const recognizer = await createModel();
|
document.querySelector("form").addEventListener("submit", async function(event) {
|
||||||
const classLabels = recognizer.wordLabels(); // get class labels
|
event.preventDefault();
|
||||||
const labelContainer = document.getElementById("label-container");
|
const fileInput = document.getElementById("audioFile");
|
||||||
for (let i = 0; i < classLabels.length; i++) {
|
const file = fileInput.files[0];
|
||||||
labelContainer.appendChild(document.createElement("div"));
|
const fileReader = new FileReader();
|
||||||
}
|
|
||||||
|
|
||||||
// listen() takes two arguments:
|
fileReader.onload = async function(event) {
|
||||||
// 1. A callback function that is invoked anytime a word is recognized.
|
const audioData = event.target.result;
|
||||||
// 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
|
|
||||||
});
|
|
||||||
|
|
||||||
// Stop the recognition in 5 seconds.
|
// Load the model
|
||||||
// setTimeout(() => recognizer.stopListening(), 5000);
|
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