Skip to content

Commit

Permalink
Mem-profile generate data
Browse files Browse the repository at this point in the history
  • Loading branch information
csaudiodesign committed Dec 10, 2023
1 parent ad8e9e9 commit 36208e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
22 changes: 12 additions & 10 deletions batdetect2_gui/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import audio_utils as au
import wavfile
import tempfile

from memory_profiler import profile

@profile
def compute_audio_data(annotation, audio_dir, playback_time_expansion):
"""returns the metadata, raw audio samples and base64 encoded wav file
Expand Down Expand Up @@ -49,8 +49,8 @@ def compute_audio_data(annotation, audio_dir, playback_time_expansion):

return sampling_rate, audio_raw, aud_data, duration


def compute_image_data(audio_raw, sampling_rate, spec_params):
@profile
def compute_image_data(audio_raw, sampling_rate, spec_params, reference):
"""computes and saves spectrogram images to files
:param audio_raw (array): audio samples
Expand All @@ -65,9 +65,11 @@ def compute_image_data(audio_raw, sampling_rate, spec_params):
spec_raw /= spec_raw.max()
cmap = plt.get_cmap("inferno")
spec = (cmap(spec_raw)[:, :, :3] * 255).astype(np.uint8)

del spec_raw

n_segments = 8
segment_width = spec.shape[1] // n_segments
dims = (spec.shape[0], spec.shape[1])

# split spec into multiple parts along the x axis
im_paths = []
Expand All @@ -80,11 +82,11 @@ def compute_image_data(audio_raw, sampling_rate, spec_params):
im = Image.fromarray(segment)

# Save the image to a temporary file
with tempfile.NamedTemporaryFile(suffix=".jpg", dir='data', delete=False) as temp_file:
temp_file_path = os.path.relpath(temp_file.name)
im.save(temp_file_path, "JPEG", quality=90)
filename = f'data/{os.path.basename(reference)}_{i}.jpg'
im.save(filename, "JPEG", quality=90)

# Append the URL of the saved image file
im_paths.append(temp_file_path)
im_paths.append(filename)

return im_paths, spec.shape
del spec
return im_paths, dims
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Pillow==9.3.0
werkzeug==2.3.7
psycopg2-binary==2.9.9
minio==7.2.0
gunicorn==21.2.0
gunicorn==21.2.0
memory_profiler

0 comments on commit 36208e9

Please sign in to comment.