// HTML CODE:
<!DOCTYPE html>
<html>
<head>
<title>Gunshot Detection - Teachable Machine</title>
</head>
<body>
<h2>Gunshot Detection System</h2>
<button type="button" onclick="init()">Start Detection</button>
<button onclick="connectArduino()">Connect Arduino</button>
<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/@tensorflow-models/speech-commands@0.4.0/dist/speech-commands.min.js"></script>
<script type="text/javascript">
const URL = "https://teachablemachine.withgoogle.com/models/iYLCbYaAV/";
async function createModel() {
const checkpointURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
const recognizer = speechCommands.create(
"BROWSER_FFT",
undefined,
checkpointURL,
metadataURL
);
await recognizer.ensureModelLoaded();
return recognizer;
}
async function init() {
const recognizer = await createModel();
const classLabels = recognizer.wordLabels();
const labelContainer = document.getElementById("label-container");
for (let i = 0; i < classLabels.length; i++) {
labelContainer.appendChild(document.createElement("div"));
}
recognizer.listen(result => {
const scores = result.scores;
for (let i = 0; i < classLabels.length; i++) {
labelContainer.childNodes[i].innerHTML =
classLabels[i] + ": " + scores[i].toFixed(2);
}
// Case-insensitive check for "Gunshot"
const gunshotIndex = classLabels.findIndex(label =>
label.toLowerCase().includes("gunshot")
);
if (gunshotIndex !== -1 && scores[gunshotIndex] > 0.75) {
console.log("Gunshot Detected!");
if (window.serialPort) {
sendToArduino('G'); // Send 'G' to Arduino
}
}
}, {
includeSpectrogram: true,
probabilityThreshold: 0.75,
invokeCallbackOnNoiseAndUnknown: true,
overlapFactor: 0.5
});
}
async function connectArduino() {
try {
window.serialPort = await navigator.serial.requestPort();
await window.serialPort.open({ baudRate: 9600 });
console.log("✅ Arduino connected via Serial!");
} catch (err) {
console.error("❌ Serial connection failed:", err);
}
}
async function sendToArduino(character) {
const encoder = new TextEncoder();
const writer = window.serialPort.writable.getWriter();
await writer.write(encoder.encode(character));
writer.releaseLock();
}
</script>
</body>
</html>
// Arduino code
#include <SoftwareSerial.h>
SoftwareSerial gsm(7, 8); // RX, TX (Connect GSM TX→7, RX→8)
void setup() {
Serial.begin(9600);
gsm.begin(9600);
Serial.println("System Ready - Waiting for Gunshot Signal");
}
void loop() {
if (Serial.available()) {
char data = Serial.read();
if (data == 'G') {
Serial.println("Gunshot Detected - Sending SMS and Making Call...");
sendSMS();
makeCall();
}
}
}
void sendSMS() {
gsm.println("AT+CMGF=1"); // Set SMS mode
delay(1000);
gsm.println("AT+CMGS=\"+917973743240\""); // Replace with your phone number
delay(1000);
gsm.println("Alert! Gunshot detected in your area!");
delay(500);
gsm.write(26); // CTRL+Z to send SMS
delay(5000);
Serial.println("SMS Sent Successfully!");
}
void makeCall() {
gsm.println("ATD+917973743240;"); // Make call (replace with your number)
delay(1000);
Serial.println("Calling the number...");
// Wait some time for the call to go through (optional)
delay(10000);
gsm.println("ATH"); // Hang up the call after 10 seconds
Serial.println("Call ended.");
}
No comments:
Post a Comment