Skip to content

Commit

Permalink
More cough detection versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ToonTalk committed Sep 7, 2024
1 parent cc07f02 commit f972936
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 5 deletions.
10 changes: 5 additions & 5 deletions apps/coughing/teachable_machine_cough_detection.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h2>Total Detected Cough Events: <span id="cough-count">0</span></h2>
let coughCount = 0;

async function loadModel() {
const URL = ''; // Update with the path to your model files
const URL = 'https://toontalk.github.io/AI/apps/coughing/'; // Update with the path to your model files
recognizer = speechCommands.create('BROWSER_FFT', undefined, URL + 'model.json', URL + 'metadata.json');
await recognizer.ensureModelLoaded();
}
Expand All @@ -76,13 +76,13 @@ <h2>Total Detected Cough Events: <span id="cough-count">0</span></h2>
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
const coughScore = scores[labels.indexOf('cough')];
if (coughScore > 0.4 && coughScore < 0.67) console.log(coughScore);
if (coughScore > 0.67) { // Adjust the threshold as necessary
logCough();
}
}, {
probabilityThreshold: 0.75,
probabilityThreshold: 0.67,
overlapFactor: 0.5 // Adjust for responsiveness
});
}
Expand Down
124 changes: 124 additions & 0 deletions apps/coughing/teachable_machine_cough_detection_with_updates.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

<!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 and Near Misses</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;
let lastCoughTime = null;
const COUGH_TIME_THRESHOLD = 5000; // 5 seconds

async function loadModel() {
const URL = 'https://toontalk.github.io/AI/apps/coughing/'; // 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.67) {
logCough(coughScore);
} else if (coughScore >= 0.5) { // Near miss threshold
logNearMiss(coughScore);
}
}, {
probabilityThreshold: 0.5, // Capture near misses as well
overlapFactor: 0.5 // Adjust for responsiveness
});
}

function logCough(score) {
const now = new Date();

// Log only if the last cough was more than 5 seconds ago
if (!lastCoughTime || now - lastCoughTime > COUGH_TIME_THRESHOLD) {
lastCoughTime = now;
updateLog(now, score, 'Cough');
updateCount();
}
}

function logNearMiss(score) {
const now = new Date();
updateLog(now, score, 'Near Miss');
}

function updateLog(time, score, eventType) {
const log = document.getElementById('cough-log');
const newEntry = document.createElement('div');
newEntry.className = 'log-entry';
newEntry.textContent = `${eventType} detected at: ${time.toLocaleTimeString()} (Score: ${score.toFixed(2)})`;
log.appendChild(newEntry);
}

function updateCount() {
coughCount++;
document.getElementById('cough-count').textContent = coughCount;
}
</script>
</body>
</html>

0 comments on commit f972936

Please sign in to comment.