Created
March 28, 2025 21:37
-
-
Save vorandrew/e47af3f64837568dfe944cf12b847328 to your computer and use it in GitHub Desktop.
Drone detect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import librosa | |
import sounddevice as sd | |
import pygame | |
import time | |
# ПАРАМЕТРЫ | |
SAMPLE_RATE = 16000 | |
DURATION = 3 | |
THRESHOLD = 0.90 | |
# ФУНКЦИИ | |
def load_audio(file_path): | |
y, sr = librosa.load(file_path, sr=SAMPLE_RATE) | |
return librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) | |
def record_audio(duration): | |
print("Запись звука...") | |
audio = sd.rec(int(duration * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1, dtype=np.float32) | |
sd.wait() | |
return np.squeeze(audio) | |
def calculate_similarity(sample_mfcc, recorded_audio): | |
recorded_mfcc = librosa.feature.mfcc(y=recorded_audio, sr=SAMPLE_RATE, n_mfcc=13) | |
if recorded_mfcc.shape[1] != sample_mfcc.shape[1]: | |
min_len = min(recorded_mfcc.shape[1], sample_mfcc.shape[1]) | |
recorded_mfcc = recorded_mfcc[:, :min_len] | |
sample_mfcc = sample_mfcc[:, :min_len] | |
correlation = np.corrcoef(sample_mfcc.flatten(), recorded_mfcc.flatten())[0, 1] | |
return correlation | |
def play_alert(): | |
print("Проигрываю тревогу...") | |
pygame.mixer.init() | |
try: | |
pygame.mixer.music.load("alarm.wav") | |
pygame.mixer.music.play() | |
time.sleep(3) # Ждем завершения звука | |
pygame.mixer.music.stop() | |
except Exception as e: | |
print(f"Ошибка при проигрывании звука: {e}") | |
print("Загрузка образца звука...") | |
sample_mfcc = load_audio("drone_sample.wav") | |
while True: | |
recorded_audio = record_audio(DURATION) | |
similarity = calculate_similarity(sample_mfcc, recorded_audio) | |
print(f"Сходство: {similarity:.2f}") | |
if similarity > THRESHOLD: | |
print("Обнаружен дрон! Пройдите в укрытие!") | |
play_alert() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment