Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,32 @@ from transformers import pipeline
|
|
| 3 |
import librosa
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
classifier = pipeline(
|
| 8 |
-
task="
|
| 9 |
-
model="
|
| 10 |
)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def analyze_barking(audio_path):
|
|
@@ -29,7 +41,7 @@ def analyze_barking(audio_path):
|
|
| 29 |
|
| 30 |
bark_windows = []
|
| 31 |
|
| 32 |
-
# Überlappende Fenster
|
| 33 |
t = 0.0
|
| 34 |
while t < duration:
|
| 35 |
start = t
|
|
@@ -38,62 +50,54 @@ def analyze_barking(audio_path):
|
|
| 38 |
end_idx = int(end * sr)
|
| 39 |
segment = y[start_idx:end_idx]
|
| 40 |
|
| 41 |
-
#
|
| 42 |
if len(segment) == 0 or np.mean(np.abs(segment)) < 1e-4:
|
| 43 |
t += HOP_SECONDS
|
| 44 |
continue
|
| 45 |
|
| 46 |
-
|
| 47 |
-
result = classifier(
|
| 48 |
-
{"array": segment, "sampling_rate": sr},
|
| 49 |
-
candidate_labels=[DOG_LABEL],
|
| 50 |
-
multi_label=False
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
score = result[0]["score"] # Wahrscheinlichkeit für "dog barking"
|
| 54 |
|
| 55 |
if score >= BARK_THRESHOLD:
|
| 56 |
-
|
| 57 |
-
bark_windows.append((start, end))
|
| 58 |
|
| 59 |
t += HOP_SECONDS
|
| 60 |
|
| 61 |
if not bark_windows:
|
| 62 |
-
return
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
# Fenster zu Episoden zusammenfassen
|
| 65 |
-
# Wenn zwischen Fenstern > MAX_PAUSE_BETWEEN_BARKS Sekunden Pause ist,
|
| 66 |
-
# startet eine neue Bell-Episode.
|
| 67 |
episodes = []
|
| 68 |
-
current_start, current_end = bark_windows[0]
|
| 69 |
|
| 70 |
-
for start, end in bark_windows[1:]:
|
| 71 |
if start - current_end <= MAX_PAUSE_BETWEEN_BARKS:
|
| 72 |
-
#
|
| 73 |
current_end = max(current_end, end)
|
| 74 |
else:
|
| 75 |
-
#
|
| 76 |
episodes.append((current_start, current_end))
|
| 77 |
current_start, current_end = start, end
|
| 78 |
|
| 79 |
-
# Letzte Episode hinzufügen
|
| 80 |
episodes.append((current_start, current_end))
|
| 81 |
|
| 82 |
-
# Kennzahlen berechnen
|
| 83 |
count_episodes = len(episodes)
|
| 84 |
total_bark_duration = sum(e_end - e_start for e_start, e_end in episodes)
|
| 85 |
|
| 86 |
-
# Schöne Textausgabe bauen
|
| 87 |
lines = []
|
| 88 |
lines.append(f"**A: Anzahl der Bell-Ereignisse:** {count_episodes}")
|
| 89 |
lines.append(f"**B: Gesamtdauer des Bellens:** {total_bark_duration:.1f} Sekunden")
|
| 90 |
-
|
|
|
|
| 91 |
lines.append("\n**Details je Bell-Ereignis:**")
|
|
|
|
| 92 |
for i, (e_start, e_end) in enumerate(episodes, start=1):
|
| 93 |
dur = e_end - e_start
|
| 94 |
lines.append(
|
| 95 |
f"- Ereignis {i}: von {e_start:.1f}s bis {e_end:.1f}s "
|
| 96 |
-
f"
|
| 97 |
)
|
| 98 |
|
| 99 |
return "\n".join(lines)
|
|
@@ -103,16 +107,19 @@ demo = gr.Interface(
|
|
| 103 |
fn=analyze_barking,
|
| 104 |
inputs=gr.Audio(type="filepath", label="Audio hochladen (.wav, .mp3)"),
|
| 105 |
outputs=gr.Markdown(),
|
| 106 |
-
title="Barking Episode Analyzer",
|
| 107 |
description=(
|
| 108 |
"Analysiert Hundebellen in einer Aufnahme.\n\n"
|
| 109 |
-
"
|
| 110 |
-
"-
|
| 111 |
-
"-
|
| 112 |
-
"
|
| 113 |
-
"- Ausgabe
|
|
|
|
|
|
|
| 114 |
),
|
| 115 |
)
|
| 116 |
|
| 117 |
-
if __name__ ==
|
| 118 |
demo.launch()
|
|
|
|
|
|
| 3 |
import librosa
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# Audio-Klassifikationsmodell (AudioSet, erkennt u.a. "Dog_bark")
|
| 7 |
classifier = pipeline(
|
| 8 |
+
task="audio-classification",
|
| 9 |
+
model="MIT/ast-finetuned-audioset-10-10-0.4593"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# Parameter
|
| 13 |
+
WINDOW_SECONDS = 1.5 # Länge eines Analysefensters
|
| 14 |
+
HOP_SECONDS = 0.75 # Schrittweite zwischen Fenstern
|
| 15 |
+
BARK_THRESHOLD = 0.5 # ab welchem Score gilt es als Bellen
|
| 16 |
+
MAX_PAUSE_BETWEEN_BARKS = 3.0 # >3 s Pause = neues Ereignis
|
| 17 |
|
| 18 |
+
|
| 19 |
+
def bark_score_for_segment(segment, sr):
|
| 20 |
+
"""
|
| 21 |
+
Liefert den höchsten Score für ein Label, das "dog" und/oder "bark" enthält.
|
| 22 |
+
"""
|
| 23 |
+
results = classifier({"array": segment, "sampling_rate": sr})
|
| 24 |
+
|
| 25 |
+
bark_score = 0.0
|
| 26 |
+
for r in results:
|
| 27 |
+
label = r["label"].lower()
|
| 28 |
+
if "dog" in label or "bark" in label:
|
| 29 |
+
if r["score"] > bark_score:
|
| 30 |
+
bark_score = float(r["score"])
|
| 31 |
+
return bark_score
|
| 32 |
|
| 33 |
|
| 34 |
def analyze_barking(audio_path):
|
|
|
|
| 41 |
|
| 42 |
bark_windows = []
|
| 43 |
|
| 44 |
+
# Überlappende Fenster
|
| 45 |
t = 0.0
|
| 46 |
while t < duration:
|
| 47 |
start = t
|
|
|
|
| 50 |
end_idx = int(end * sr)
|
| 51 |
segment = y[start_idx:end_idx]
|
| 52 |
|
| 53 |
+
# Sehr leise/leer überspringen
|
| 54 |
if len(segment) == 0 or np.mean(np.abs(segment)) < 1e-4:
|
| 55 |
t += HOP_SECONDS
|
| 56 |
continue
|
| 57 |
|
| 58 |
+
score = bark_score_for_segment(segment, sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
if score >= BARK_THRESHOLD:
|
| 61 |
+
bark_windows.append((start, end, score))
|
|
|
|
| 62 |
|
| 63 |
t += HOP_SECONDS
|
| 64 |
|
| 65 |
if not bark_windows:
|
| 66 |
+
return (
|
| 67 |
+
"Es wurde kein Hundebellen mit ausreichend hoher Sicherheit erkannt.\n\n"
|
| 68 |
+
f"(Schwellwert BARK_THRESHOLD = {BARK_THRESHOLD})"
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# Fenster zu Bell-Episoden zusammenfassen
|
|
|
|
|
|
|
| 72 |
episodes = []
|
| 73 |
+
current_start, current_end, _ = bark_windows[0]
|
| 74 |
|
| 75 |
+
for start, end, _ in bark_windows[1:]:
|
| 76 |
if start - current_end <= MAX_PAUSE_BETWEEN_BARKS:
|
| 77 |
+
# gleiches Ereignis, Ende verlängern
|
| 78 |
current_end = max(current_end, end)
|
| 79 |
else:
|
| 80 |
+
# neues Ereignis
|
| 81 |
episodes.append((current_start, current_end))
|
| 82 |
current_start, current_end = start, end
|
| 83 |
|
|
|
|
| 84 |
episodes.append((current_start, current_end))
|
| 85 |
|
|
|
|
| 86 |
count_episodes = len(episodes)
|
| 87 |
total_bark_duration = sum(e_end - e_start for e_start, e_end in episodes)
|
| 88 |
|
|
|
|
| 89 |
lines = []
|
| 90 |
lines.append(f"**A: Anzahl der Bell-Ereignisse:** {count_episodes}")
|
| 91 |
lines.append(f"**B: Gesamtdauer des Bellens:** {total_bark_duration:.1f} Sekunden")
|
| 92 |
+
lines.append("")
|
| 93 |
+
lines.append(f"_Regel: > {MAX_PAUSE_BETWEEN_BARKS:.0f} Sekunden Pause = neues Ereignis._")
|
| 94 |
lines.append("\n**Details je Bell-Ereignis:**")
|
| 95 |
+
|
| 96 |
for i, (e_start, e_end) in enumerate(episodes, start=1):
|
| 97 |
dur = e_end - e_start
|
| 98 |
lines.append(
|
| 99 |
f"- Ereignis {i}: von {e_start:.1f}s bis {e_end:.1f}s "
|
| 100 |
+
f"(Dauer: {dur:.1f}s)"
|
| 101 |
)
|
| 102 |
|
| 103 |
return "\n".join(lines)
|
|
|
|
| 107 |
fn=analyze_barking,
|
| 108 |
inputs=gr.Audio(type="filepath", label="Audio hochladen (.wav, .mp3)"),
|
| 109 |
outputs=gr.Markdown(),
|
| 110 |
+
title="Barking Episode Analyzer (AudioSet)",
|
| 111 |
description=(
|
| 112 |
"Analysiert Hundebellen in einer Aufnahme.\n\n"
|
| 113 |
+
"Logik:\n"
|
| 114 |
+
"- Das Audio wird in überlappende Fenster geteilt.\n"
|
| 115 |
+
"- In jedem Fenster wird geprüft, ob ein Label mit 'dog'/'bark' hoch genug ist.\n"
|
| 116 |
+
"- Bellen-Fenster, die weniger als 3 Sekunden auseinander liegen, werden zu einem Ereignis zusammengefasst.\n"
|
| 117 |
+
"- Ausgabe:\n"
|
| 118 |
+
" A) Anzahl der Bell-Ereignisse\n"
|
| 119 |
+
" B) Gesamtdauer des Bellens"
|
| 120 |
),
|
| 121 |
)
|
| 122 |
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
demo.launch()
|
| 125 |
+
|