Skip to content

Commit

Permalink
coughing using teachable machine
Browse files Browse the repository at this point in the history
  • Loading branch information
ToonTalk committed Sep 7, 2024
1 parent 91dcf1f commit cc07f02
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/coughing/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tfjsSpeechCommandsVersion":"0.4.0","modelName":"TMv2","timeStamp":"2024-09-07T13:40:30.200Z","wordLabels":["Background Noise","cough"]}
1 change: 1 addition & 0 deletions apps/coughing/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"modelTopology":{"class_name":"Model","config":{"name":"model1","layers":[{"name":"conv2d_1_input","class_name":"InputLayer","config":{"batch_input_shape":[null,43,232,1],"dtype":"float32","sparse":false,"name":"conv2d_1_input"},"inbound_nodes":[]},{"name":"conv2d_1","class_name":"Conv2D","config":{"filters":8,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":null}},"kernel_regularizer":null,"kernel_constraint":null,"kernel_size":[2,8],"strides":[1,1],"padding":"valid","data_format":"channels_last","dilation_rate":[1,1],"activation":"relu","use_bias":true,"bias_initializer":{"class_name":"Zeros","config":{}},"bias_regularizer":null,"activity_regularizer":null,"bias_constraint":null,"name":"conv2d_1","trainable":false,"batch_input_shape":[null,43,232,1],"dtype":"float32"},"inbound_nodes":[[["conv2d_1_input",0,0,{}]]]},{"name":"max_pooling2d_1","class_name":"MaxPooling2D","config":{"pool_size":[2,2],"padding":"valid","strides":[2,2],"data_format":"channels_last","name":"max_pooling2d_1","trainable":false},"inbound_nodes":[[["conv2d_1",0,0,{}]]]},{"name":"conv2d_2","class_name":"Conv2D","config":{"filters":32,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":null}},"kernel_regularizer":null,"kernel_constraint":null,"kernel_size":[2,4],"strides":[1,1],"padding":"valid","data_format":"channels_last","dilation_rate":[1,1],"activation":"relu","use_bias":true,"bias_initializer":{"class_name":"Zeros","config":{}},"bias_regularizer":null,"activity_regularizer":null,"bias_constraint":null,"name":"conv2d_2","trainable":false},"inbound_nodes":[[["max_pooling2d_1",0,0,{}]]]},{"name":"max_pooling2d_2","class_name":"MaxPooling2D","config":{"pool_size":[2,2],"padding":"valid","strides":[2,2],"data_format":"channels_last","name":"max_pooling2d_2","trainable":false},"inbound_nodes":[[["conv2d_2",0,0,{}]]]},{"name":"conv2d_3","class_name":"Conv2D","config":{"filters":32,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":null}},"kernel_regularizer":null,"kernel_constraint":null,"kernel_size":[2,4],"strides":[1,1],"padding":"valid","data_format":"channels_last","dilation_rate":[1,1],"activation":"relu","use_bias":true,"bias_initializer":{"class_name":"Zeros","config":{}},"bias_regularizer":null,"activity_regularizer":null,"bias_constraint":null,"name":"conv2d_3","trainable":false},"inbound_nodes":[[["max_pooling2d_2",0,0,{}]]]},{"name":"max_pooling2d_3","class_name":"MaxPooling2D","config":{"pool_size":[2,2],"padding":"valid","strides":[2,2],"data_format":"channels_last","name":"max_pooling2d_3","trainable":false},"inbound_nodes":[[["conv2d_3",0,0,{}]]]},{"name":"conv2d_4","class_name":"Conv2D","config":{"filters":32,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":null}},"kernel_regularizer":null,"kernel_constraint":null,"kernel_size":[2,4],"strides":[1,1],"padding":"valid","data_format":"channels_last","dilation_rate":[1,1],"activation":"relu","use_bias":true,"bias_initializer":{"class_name":"Zeros","config":{}},"bias_regularizer":null,"activity_regularizer":null,"bias_constraint":null,"name":"conv2d_4","trainable":false},"inbound_nodes":[[["max_pooling2d_3",0,0,{}]]]},{"name":"max_pooling2d_4","class_name":"MaxPooling2D","config":{"pool_size":[2,2],"padding":"valid","strides":[1,2],"data_format":"channels_last","name":"max_pooling2d_4","trainable":false},"inbound_nodes":[[["conv2d_4",0,0,{}]]]},{"name":"flatten_1","class_name":"Flatten","config":{"name":"flatten_1","trainable":false},"inbound_nodes":[[["max_pooling2d_4",0,0,{}]]]},{"name":"dropout_1","class_name":"Dropout","config":{"rate":0.25,"noise_shape":null,"seed":null,"name":"dropout_1","trainable":false},"inbound_nodes":[[["flatten_1",0,0,{}]]]},{"name":"dense_1","class_name":"Dense","config":{"units":2000,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_1","trainable":false},"inbound_nodes":[[["dropout_1",0,0,{}]]]},{"name":"sequential_3","class_name":"Sequential","config":{"name":"sequential_3","layers":[{"class_name":"Dense","config":{"units":2,"activation":"softmax","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"NewHeadDense","trainable":true,"batch_input_shape":[null,2000],"dtype":"float32"}}]},"inbound_nodes":[[["dense_1",0,0,{}]]]}],"input_layers":[["conv2d_1_input",0,0]],"output_layers":[["sequential_3",1,0]]},"keras_version":"tfjs-layers 1.3.1","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["weights.bin"],"weights":[{"name":"NewHeadDense/kernel","shape":[2000,2],"dtype":"float32"},{"name":"NewHeadDense/bias","shape":[2],"dtype":"float32"},{"name":"conv2d_1/kernel","shape":[2,8,1,8],"dtype":"float32"},{"name":"conv2d_1/bias","shape":[8],"dtype":"float32"},{"name":"conv2d_2/kernel","shape":[2,4,8,32],"dtype":"float32"},{"name":"conv2d_2/bias","shape":[32],"dtype":"float32"},{"name":"conv2d_3/kernel","shape":[2,4,32,32],"dtype":"float32"},{"name":"conv2d_3/bias","shape":[32],"dtype":"float32"},{"name":"conv2d_4/kernel","shape":[2,4,32,32],"dtype":"float32"},{"name":"conv2d_4/bias","shape":[32],"dtype":"float32"},{"name":"dense_1/kernel","shape":[704,2000],"dtype":"float32"},{"name":"dense_1/bias","shape":[2000],"dtype":"float32"}]}]}
106 changes: 106 additions & 0 deletions apps/coughing/teachable_machine_cough_detection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cough Detection App - Teachable Machine</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 20px;
background-color: #f0f8ff;
}
h1 {
color: #333;
}
.container {
margin-top: 50px;
}
button {
background-color: #4CAF50;
color: white;
padding: 15px 32px;
text-align: center;
font-size: 16px;
margin: 10px 0;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#log {
margin-top: 20px;
max-height: 300px;
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
background-color: #fff;
}
.log-entry {
padding: 5px 0;
}
</style>
</head>
<body>
<h1>Cough Detection App</h1>
<div class="container">
<button onclick="startListening()">Start Listening</button>
<div id="log">
<h3>Log of Detected Coughs</h3>
<div id="cough-log"></div>
</div>
<div>
<h2>Total Detected Cough Events: <span id="cough-count">0</span></h2>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/speech-commands"></script>

<script>
let recognizer;
let coughCount = 0;

async function loadModel() {
const URL = ''; // Update with the path to your model files
recognizer = speechCommands.create('BROWSER_FFT', undefined, URL + 'model.json', URL + 'metadata.json');
await recognizer.ensureModelLoaded();
}

async function startListening() {
await loadModel();
recognizer.listen(result => {
const scores = result.scores;
const labels = recognizer.wordLabels();
const coughScore = scores[labels.indexOf('Cough')];

if (coughScore > 0.75) { // Adjust the threshold as necessary
logCough();
}
}, {
probabilityThreshold: 0.75,
overlapFactor: 0.5 // Adjust for responsiveness
});
}

function logCough() {
const now = new Date();
const log = document.getElementById('cough-log');
const newEntry = document.createElement('div');
newEntry.className = 'log-entry';
newEntry.textContent = `Cough detected at: ${now.toLocaleTimeString()}`;
log.appendChild(newEntry);
updateCount();
}

function updateCount() {
coughCount++;
document.getElementById('cough-count').textContent = coughCount;
}
</script>
</body>
</html>
Binary file added apps/coughing/weights.bin
Binary file not shown.

0 comments on commit cc07f02

Please sign in to comment.