-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
68 lines (57 loc) · 2.8 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from medkit.core.audio import MemoryAudioBuffer, AudioDocument
from medkit.audio.segmentation.pa_speaker_detector import PASpeakerDetector
from medkit.audio.transcription.sb_transcriber import SBTranscriber
import librosa
import numpy as np
import os
# Chemins vers les dossiers
dossier_audio = "simsamu_wav"
dossier_transcriptions = "transcription_simsamu_CTC_finetuned"
# Créer le dossier de transcriptions s'il n'existe pas déjà
if not os.path.exists(dossier_transcriptions):
os.makedirs(dossier_transcriptions)
# Initialisation du détecteur de locuteurs
speaker_detector = PASpeakerDetector(
model="medkit/simsamu-diarization",
device=-1, # Exécution sur CPU
segmentation_batch_size=10,
embedding_batch_size=10,
output_label="speaker"
)
# Initialisation du transcripteur
transcriber = SBTranscriber(
model="medkit/simsamu-transcription",
needs_decoder=False,
output_label="transcription",
device=-1, # Exécution sur CPU
batch_size=10,
)
# Parcourir chaque fichier WAV dans le dossier audio
for fichier in os.listdir(dossier_audio):
chemin_fichier_wav = os.path.join(dossier_audio, fichier)
nom_fichier_texte = fichier.replace('.wav', '.txt')
chemin_fichier_texte = os.path.join(dossier_transcriptions, nom_fichier_texte)
# Vérifier si la transcription existe déjà
if not os.path.isfile(chemin_fichier_texte):
if os.path.isfile(chemin_fichier_wav) and fichier.endswith('.wav'):
y, sr = librosa.load(chemin_fichier_wav, sr=None)
y_resampled = librosa.resample(y, orig_sr=sr, target_sr=16000)
# Assurez-vous que le tableau est en deux dimensions [nb_channels, nb_samples]
if y_resampled.ndim == 1:
y_resampled = y_resampled[np.newaxis, :] # Ajoute une dimension pour les canaux
# Créer un MemoryAudioBuffer avec le signal resamplé
audio_buffer = MemoryAudioBuffer(signal=y_resampled, sample_rate=16000)
audio_doc = AudioDocument(audio=audio_buffer)
# Appliquer la diarisation et la transcription
speech_segments = speaker_detector.run([audio_doc.raw_segment])
transcriptions = transcriber.run(speech_segments)
# Écrire les transcriptions dans un fichier texte
with open(chemin_fichier_texte, 'w') as fichier_texte:
for speech_seg in speech_segments:
transcription_attr = speech_seg.attrs.get(label="transcription")[0]
fichier_texte.write(f"{transcription_attr.value}\n")
print(f"Transcription enregistrée : {chemin_fichier_texte}")
else:
print(f"Le fichier {chemin_fichier_wav} n'est pas valide ou n'existe pas.")
else:
print(f"La transcription de {chemin_fichier_wav} existe déjà : {chemin_fichier_texte}")