Abraham E. Tavarez
commited on
Commit
·
fe506fe
1
Parent(s):
4c7f58c
voice detertor tool
Browse files- detector/voice.py +31 -0
detector/voice.py
CHANGED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from resemblyzer import VoiceEncoder, preprocess_wav
|
2 |
+
import numpy as np
|
3 |
+
import os
|
4 |
+
|
5 |
+
encoder = VoiceEncoder()
|
6 |
+
|
7 |
+
|
8 |
+
def verify_voices(audio_path1, audio_path2, threshold=0.75):
|
9 |
+
"""
|
10 |
+
Compares two audio files and returns whether they belong to the same speaker.
|
11 |
+
"""
|
12 |
+
try:
|
13 |
+
audio1 = preprocess_wav(audio_path1)
|
14 |
+
audio2 = preprocess_wav(audio_path2)
|
15 |
+
|
16 |
+
embedding1 = encoder.embed_utterance(audio1)
|
17 |
+
embedding2 = encoder.embed_utterance(audio2)
|
18 |
+
|
19 |
+
similarity = np.dot(embedding1, embedding2) / (
|
20 |
+
np.linalg.norm(embedding1) * np.linalg.norm(embedding2)
|
21 |
+
)
|
22 |
+
|
23 |
+
match = similarity > threshold
|
24 |
+
|
25 |
+
return {
|
26 |
+
"match": match,
|
27 |
+
"similarity": round(float(similarity), 4),
|
28 |
+
"threshold": threshold,
|
29 |
+
}
|
30 |
+
except Exception as e:
|
31 |
+
return {"error": str(e)}
|