Spaces:
Sleeping
Sleeping
- main.py +9 -9
- templates/index.html +3 -2
main.py
CHANGED
@@ -1,16 +1,14 @@
|
|
1 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import JSONResponse, HTMLResponse
|
4 |
-
import sounddevice as sd
|
5 |
import numpy as np
|
6 |
import librosa
|
|
|
7 |
import joblib
|
8 |
import uvicorn
|
9 |
-
import threading
|
10 |
-
import asyncio
|
11 |
import logging
|
12 |
import io
|
13 |
-
|
14 |
from typing import List
|
15 |
|
16 |
logging.basicConfig(level=logging.INFO)
|
@@ -79,10 +77,13 @@ def extract_features(audio):
|
|
79 |
|
80 |
async def process_audio_data(audio_data):
|
81 |
try:
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
86 |
logger.error(f"Failed to read audio data: {e}")
|
87 |
return
|
88 |
|
@@ -98,7 +99,6 @@ async def process_audio_data(audio_data):
|
|
98 |
|
99 |
await manager.send_message(result)
|
100 |
|
101 |
-
|
102 |
@app.post("/start_detection")
|
103 |
async def start_detection():
|
104 |
global is_detecting
|
|
|
1 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import JSONResponse, HTMLResponse
|
|
|
4 |
import numpy as np
|
5 |
import librosa
|
6 |
+
import soundfile as sf
|
7 |
import joblib
|
8 |
import uvicorn
|
|
|
|
|
9 |
import logging
|
10 |
import io
|
11 |
+
from pydub import AudioSegment
|
12 |
from typing import List
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
|
|
77 |
|
78 |
async def process_audio_data(audio_data):
|
79 |
try:
|
80 |
+
# Convert audio data from webm/ogg to wav format using pydub
|
81 |
+
audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format="webm")
|
82 |
+
wav_io = io.BytesIO()
|
83 |
+
audio_segment.export(wav_io, format="wav")
|
84 |
+
wav_io.seek(0)
|
85 |
+
audio, sr = sf.read(wav_io, dtype='float32')
|
86 |
+
except Exception as e:
|
87 |
logger.error(f"Failed to read audio data: {e}")
|
88 |
return
|
89 |
|
|
|
99 |
|
100 |
await manager.send_message(result)
|
101 |
|
|
|
102 |
@app.post("/start_detection")
|
103 |
async def start_detection():
|
104 |
global is_detecting
|
templates/index.html
CHANGED
@@ -63,7 +63,7 @@
|
|
63 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
64 |
logMessage('Microphone access granted', 'info');
|
65 |
|
66 |
-
mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/
|
67 |
mediaRecorder.ondataavailable = function(event) {
|
68 |
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
69 |
websocket.send(event.data);
|
@@ -81,7 +81,7 @@
|
|
81 |
const result = await response.json();
|
82 |
logMessage(`Detection started: ${result.status}`, 'info');
|
83 |
|
84 |
-
websocket = new WebSocket(`
|
85 |
websocket.onmessage = function(event) {
|
86 |
const data = event.data;
|
87 |
logMessage(`Detected ${data} audio`, data);
|
@@ -97,6 +97,7 @@
|
|
97 |
}
|
98 |
|
99 |
|
|
|
100 |
async function stopDetection() {
|
101 |
if (!isDetecting) {
|
102 |
logMessage('Detection is not running...', 'info');
|
|
|
63 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
64 |
logMessage('Microphone access granted', 'info');
|
65 |
|
66 |
+
mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' });
|
67 |
mediaRecorder.ondataavailable = function(event) {
|
68 |
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
69 |
websocket.send(event.data);
|
|
|
81 |
const result = await response.json();
|
82 |
logMessage(`Detection started: ${result.status}`, 'info');
|
83 |
|
84 |
+
websocket = new WebSocket(`wss://${window.location.host}/ws`);
|
85 |
websocket.onmessage = function(event) {
|
86 |
const data = event.data;
|
87 |
logMessage(`Detected ${data} audio`, data);
|
|
|
97 |
}
|
98 |
|
99 |
|
100 |
+
|
101 |
async function stopDetection() {
|
102 |
if (!isDetecting) {
|
103 |
logMessage('Detection is not running...', 'info');
|