diff --git a/bats_ai/core/models/recording.py b/bats_ai/core/models/recording.py index 0c4484e..1942730 100644 --- a/bats_ai/core/models/recording.py +++ b/bats_ai/core/models/recording.py @@ -34,7 +34,7 @@ def generate_spectrogram(self): # Plot and save the spectrogram plt.figure(figsize=(10, 4)) - librosa.display.specshow(D, sr=sr, x_axis='time', y_axis='log') + librosa.display.specshow(D, sr=sr, x_axis='time', y_axis='linear') plt.colorbar(format='%+2.0f dB') plt.title('Spectrogram') plt.xlabel('Time') @@ -49,4 +49,22 @@ def generate_spectrogram(self): plt.close() - return base64_image + start_time = 0.0 + end_time = librosa.get_duration(y=y, sr=sr) * 1000 # in milliseconds + low_frequency = 0 # Set your desired low frequency + high_frequency = sr / 2 # Set your desired high frequency + image_width = 10 * 100 # 10 inches at 100 dpi + image_height = 4 * 100 # 4 inches at 100 dpi + + # Return dictionary with all required fields + return { + 'base64_spectrogram': base64_image, + 'spectroInfo': { + 'width': image_width, + 'height': image_height, + 'start_time': start_time, + 'end_time': end_time, + 'low_freq': low_frequency, + 'high_freq': high_frequency, + }, + } diff --git a/bats_ai/core/views/annotations.py b/bats_ai/core/views/annotations.py new file mode 100644 index 0000000..e647e56 --- /dev/null +++ b/bats_ai/core/views/annotations.py @@ -0,0 +1,38 @@ +from datetime import datetime +import logging + +from django.contrib.auth.models import User +from django.core.files.storage import default_storage +from django.http import HttpRequest +from ninja import File, Form, Schema +from ninja.errors import HttpError +from ninja.files import UploadedFile +from ninja.pagination import RouterPaginated +from oauth2_provider.models import AccessToken + +from bats_ai.core.models import Annotations + +logger = logging.getLogger(__name__) + + +router = RouterPaginated() + + +class AnnotationSchema(Schema): + recording: int # Foreign Key to index + owner_username: str + start_time: int + end_time: int + low_freq: int + high_freq: int + species: list[int] + comments: str + + +def get_owner_id(request: HttpRequest): + token = request.headers.get('Authorization').replace('Bearer ', '') + token_found = AccessToken.objects.get(token=token) + if not token_found: + raise HttpError(401, 'Authentication credentials were not provided.') + + return token_found.user.pk diff --git a/bats_ai/core/views/recording.py b/bats_ai/core/views/recording.py index 6578fad..faff64a 100644 --- a/bats_ai/core/views/recording.py +++ b/bats_ai/core/views/recording.py @@ -45,6 +45,7 @@ class AnnotationSchema(Schema): high_freq: int species: list[SpeciesSchema] comments: str + id: int def get_user(request: HttpRequest): @@ -98,14 +99,22 @@ def get_recordings(request: HttpRequest): @router.get('/{id}/spectrogram') def get_spectrogram(request: HttpRequest, id: int): + user_id = get_user(request) try: recording = Recording.objects.get(pk=id) except Recording.DoesNotExist: return {'error': 'Recording not found'} - base64_spectrogram = recording.generate_spectrogram() + spectro_data = recording.generate_spectrogram() + + annotations_qs = Annotations.objects.filter(recording=recording, owner=user_id) - return {'base64_spectrogram': base64_spectrogram} + # Serialize the annotations using AnnotationSchema + annotations_data = [ + AnnotationSchema.from_orm(annotation).dict() for annotation in annotations_qs + ] + spectro_data['annotations'] = annotations_data + return spectro_data @router.get('/{id}/annotations') @@ -118,7 +127,7 @@ def get_annotations(request: HttpRequest, id: int): return {'error': 'Recording not found'} # Query annotations associated with the recording - annotations_qs = Annotations.objects.filter(recording=recording) + annotations_qs = Annotations.objects.filter(recording=recording, owner=user_id) # Serialize the annotations using AnnotationSchema annotations_data = [ diff --git a/bats_ai/core/views/species.py b/bats_ai/core/views/species.py index fe0bae1..fe45fd8 100644 --- a/bats_ai/core/views/species.py +++ b/bats_ai/core/views/species.py @@ -19,6 +19,7 @@ class SpeciesSchema(Schema): species: str | None common_name: str | None species_code_6: str | None + pk: int = None @router.get('/') diff --git a/client/src/App.vue b/client/src/App.vue index ec260bc..c31c389 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -1,11 +1,9 @@