(output_dir)
| 1174 | |
| 1175 | |
| 1176 | def audio_dectection(output_dir): |
| 1177 | mp3_dir = os.path.join(output_dir, "mp3") |
| 1178 | wav_dir = os.path.join(output_dir, "wav") |
| 1179 | if os.path.isdir(mp3_dir): |
| 1180 | for filename in os.listdir(mp3_dir): |
| 1181 | f = os.path.join(mp3_dir, filename) |
| 1182 | # checking if it is a file |
| 1183 | if os.path.isfile(f): |
| 1184 | # print(f) |
| 1185 | src = f |
| 1186 | file_name = os.path.basename(f) |
| 1187 | file = os.path.splitext(file_name) |
| 1188 | dst = os.path.join(wav_dir, file[0] + ".wav") |
| 1189 | |
| 1190 | # convert wav to mp3 |
| 1191 | sound = AudioSegment.from_mp3(src) |
| 1192 | sound.export(dst, format="wav") |
| 1193 | |
| 1194 | if os.path.isdir(wav_dir): |
| 1195 | for filename in tqdm(os.listdir(wav_dir), desc="Audio detection test: "): |
| 1196 | f = os.path.join(wav_dir, filename) |
| 1197 | # checking if it is a file |
| 1198 | if os.path.isfile(f): |
| 1199 | # print(f) |
| 1200 | audio_path = f |
| 1201 | # Step 1: Load the audio file and generate spectrogram |
| 1202 | y, sr = librosa.load(audio_path, sr=None) |
| 1203 | D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) |
| 1204 | |
| 1205 | # Step 2: Save spectrogram as a high-resolution image (no display) |
| 1206 | base_name, _ = os.path.splitext(filename) |
| 1207 | spectrogram_path = f"{base_name}_spectrogram.png" # Need to change this |
| 1208 | librosa.display.specshow(D, sr=sr, x_axis="time", y_axis="log") |
| 1209 | plt.axis("off") |
| 1210 | plt.savefig(spectrogram_path) |
| 1211 | plt.close() |
| 1212 | |
| 1213 | # Step 3: Run YOLOv8 detection (silent execution) |
| 1214 | results = model(spectrogram_path) |
| 1215 | for result in results: |
| 1216 | if len(result.boxes) > 0: |
| 1217 | audio_dectection_dir = os.path.join(results_folder, "audio_dectection") |
| 1218 | os.makedirs(audio_dectection_dir, exist_ok=True) |
| 1219 | |
| 1220 | shutil.copy(spectrogram_path, |
| 1221 | f"{audio_dectection_dir}/{base_name}_spectrogram.png") # this needs to be tested further |
| 1222 | |
| 1223 | # Step 5: Run OCR on spectrogram (both handwritten and printed models) |
| 1224 | image = Image.open(spectrogram_path).convert("RGB") |
| 1225 | |
| 1226 | # Extract and filter text |
| 1227 | text_handwritten = extract_text(image, processor_handwritten, model_handwritten) |
| 1228 | text_printed = extract_text(image, processor_printed, model_printed) |
| 1229 | |
| 1230 | # Step 6: Print meaningful results only, Need to save this here |
| 1231 | if text_handwritten: |
| 1232 | audio_dectection_dir = os.path.join(results_folder, "audio_dectection") |
| 1233 | os.makedirs(audio_dectection_dir, exist_ok=True) |
no test coverage detected