|
11 | 11 | import uuid |
12 | 12 | import zipfile |
13 | 13 | from pydub import AudioSegment |
14 | | -from scipy.io import wavfile |
15 | 14 | import numpy as np |
16 | 15 | import os |
17 | 16 | from collections import OrderedDict |
@@ -920,9 +919,10 @@ def jobChunkPathGetter(db_data, start, stop, task_dimension, data_quality, data_ |
920 | 919 | if data_quality == 'compressed' else FrameProvider.Quality.ORIGINAL |
921 | 920 |
|
922 | 921 | path = os.path.realpath(frame_provider.get_chunk(number, quality)) |
923 | | - # pylint: disable=superfluous-parens |
924 | 922 |
|
925 | | - # return {"start_chunk" : start_chunk, "stop_chunk" : stop_chunk} |
| 923 | + # Check if the file with .mp3 exist or .wav |
| 924 | + if not os.path.exists(path): |
| 925 | + path = os.path.splitext(path)[0] + '.wav' |
926 | 926 |
|
927 | 927 | return path |
928 | 928 |
|
@@ -1015,34 +1015,34 @@ def create_gt_annotation_clips_zip(annotation_audio_chunk_file_paths, json_data, |
1015 | 1015 | shutil.move(zip_filename, dst_file) |
1016 | 1016 |
|
1017 | 1017 | def get_np_audio_array_from_job(job_id): |
| 1018 | + |
1018 | 1019 | with transaction.atomic(): |
1019 | 1020 | job = JobAnnotation(job_id) |
1020 | 1021 | job.init_from_db() |
1021 | 1022 |
|
1022 | | - db_job = job.db_job |
1023 | | - task_jobs = Job.objects.filter(segment__task__id=db_job.segment.task_id).order_by('id') |
1024 | | - |
1025 | | - data_quality = 'compressed' |
1026 | | - data_num = ( |
1027 | | - (len(task_jobs)-1) if (db_job.type == JobType.GROUND_TRUTH) # len(task_jobs) - 1 because of one GT job |
1028 | | - else job_id - task_jobs.first().id |
1029 | | - ) |
1030 | | - |
1031 | | - task_dimension = db_job.segment.task.dimension |
1032 | | - chunk_path = jobChunkPathGetter( |
1033 | | - db_job.segment.task.data, |
1034 | | - job.start_frame, |
1035 | | - job.stop_frame, |
1036 | | - task_dimension, |
1037 | | - data_quality, |
1038 | | - data_num, |
1039 | | - db_job |
1040 | | - ) |
1041 | | - |
1042 | | - _, audio_data = wavfile.read(chunk_path) |
1043 | | - audio_data_int16 = np.array(audio_data, dtype=np.int16) |
1044 | | - |
1045 | | - return audio_data_int16 |
| 1023 | + job_data_chunk_size = job.db_job.segment.task.data.chunk_size |
| 1024 | + task_dimension = job.db_job.segment.task.dimension |
| 1025 | + |
| 1026 | + start = job.start_frame / job_data_chunk_size |
| 1027 | + stop = job.stop_frame / job_data_chunk_size |
| 1028 | + |
| 1029 | + audio_array_buffer = [] |
| 1030 | + for i in range(math.trunc(start), math.trunc(stop) + 1): |
| 1031 | + db_job = job.db_job |
| 1032 | + data_num = i |
| 1033 | + data_quality = 'compressed' |
| 1034 | + |
| 1035 | + chunk_path = jobChunkPathGetter(job.db_job.segment.task.data, job.start_frame, job.stop_frame, task_dimension, data_quality, data_num, db_job) |
| 1036 | + |
| 1037 | + audio = AudioSegment.from_file(chunk_path) # Handles both MP3 and WAV |
| 1038 | + audio_data = np.array(audio.get_array_of_samples(), dtype=np.int16) |
| 1039 | + |
| 1040 | + audio_array_buffer.append(audio_data) |
| 1041 | + |
| 1042 | + # Concatenate all audio data into a single numpy array |
| 1043 | + concat_array = np.concatenate(audio_array_buffer, axis=0) |
| 1044 | + |
| 1045 | + return concat_array |
1046 | 1046 |
|
1047 | 1047 | def get_audio_job_export_data(job_id, dst_file, job, temp_dir_base, temp_dir): |
1048 | 1048 |
|
|
0 commit comments