1515 Annotations ,
1616 CompressedSpectrogram ,
1717 Recording ,
18+ RecordingAnnotation ,
1819 Species ,
1920 TemporalAnnotations ,
2021 colormap ,
@@ -61,6 +62,26 @@ class RecordingUploadSchema(Schema):
6162 unusual_occurrences : str = None
6263
6364
65+ class RecordingAnnotationSchema (Schema ):
66+ species : list [SpeciesSchema ] | None
67+ comments : str | None = None
68+ model : str | None = None
69+ owner : str
70+ confidence : float
71+ id : int | None = None
72+
73+ @classmethod
74+ def from_orm (cls , obj : RecordingAnnotation , ** kwargs ):
75+ return cls (
76+ species = [SpeciesSchema .from_orm (species ) for species in obj .species .all ()],
77+ owner = obj .owner .username ,
78+ confidence = obj .confidence ,
79+ comments = obj .comments ,
80+ model = obj .model ,
81+ id = obj .pk ,
82+ )
83+
84+
6485class AnnotationSchema (Schema ):
6586 start_time : int
6687 end_time : int
@@ -73,7 +94,7 @@ class AnnotationSchema(Schema):
7394 owner_email : str = None
7495
7596 @classmethod
76- def from_orm (cls , obj , owner_email = None , ** kwargs ):
97+ def from_orm (cls , obj : Annotations , owner_email = None , ** kwargs ):
7798 return cls (
7899 start_time = obj .start_time ,
79100 end_time = obj .end_time ,
@@ -215,6 +236,11 @@ def get_recordings(request: HttpRequest, public: bool | None = None):
215236 # TODO with larger dataset it may be better to do this in a queryset instead of python
216237 for recording in recordings :
217238 user = User .objects .get (id = recording ['owner_id' ])
239+ fileAnnotations = RecordingAnnotation .objects .filter (recording = recording ['id' ])
240+ recording ['fileAnnotations' ] = [
241+ RecordingAnnotationSchema .from_orm (fileAnnotation ).dict ()
242+ for fileAnnotation in fileAnnotations
243+ ]
218244 recording ['owner_username' ] = user .username
219245 recording ['audio_file_presigned_url' ] = default_storage .url (recording ['audio_file' ])
220246 recording ['hasSpectrogram' ] = Recording .objects .get (id = recording ['id' ]).has_spectrogram
@@ -227,9 +253,12 @@ def get_recordings(request: HttpRequest, public: bool | None = None):
227253 .count ()
228254 )
229255 recording ['userAnnotations' ] = unique_users_with_annotations
230- user_has_annotations = Annotations .objects .filter (
231- recording_id = recording ['id' ], owner = request .user
232- ).exists ()
256+ user_has_annotations = (
257+ Annotations .objects .filter (recording_id = recording ['id' ], owner = request .user ).exists ()
258+ or RecordingAnnotation .objects .filter (
259+ recording_id = recording ['id' ], owner = request .user
260+ ).exists ()
261+ )
233262 recording ['userMadeAnnotations' ] = user_has_annotations
234263
235264 return list (recordings )
@@ -249,17 +278,38 @@ def get_recording(request: HttpRequest, id: int):
249278 recording ['hasSpectrogram' ] = Recording .objects .get (id = recording ['id' ]).has_spectrogram
250279 if recording ['recording_location' ]:
251280 recording ['recording_location' ] = json .loads (recording ['recording_location' ].json )
252- unique_users_with_annotations = (
281+ annotation_owners = (
253282 Annotations .objects .filter (recording_id = recording ['id' ])
254- .values ('owner' )
283+ .values_list ('owner' , flat = True )
255284 .distinct ()
256- .count ()
285+ )
286+ recording_annotation_owners = (
287+ RecordingAnnotation .objects .filter (recording_id = recording ['id' ])
288+ .values_list ('owner' , flat = True )
289+ .distinct ()
290+ )
291+
292+ # Combine the sets of owners and count unique entries
293+ unique_users_with_annotations = len (
294+ set (annotation_owners ).union (set (recording_annotation_owners ))
257295 )
258296 recording ['userAnnotations' ] = unique_users_with_annotations
259- user_has_annotations = Annotations .objects .filter (
260- recording_id = recording ['id' ], owner = request .user
261- ).exists ()
297+ user_has_annotations = (
298+ Annotations .objects .filter (
299+ recording_id = recording ['id' ], owner = request .user
300+ ).exists ()
301+ or RecordingAnnotation .objects .filter (
302+ recording_id = recording ['id' ], owner = request .user
303+ ).exists ()
304+ )
262305 recording ['userMadeAnnotations' ] = user_has_annotations
306+ fileAnnotations = RecordingAnnotation .objects .filter (recording = id ).order_by (
307+ 'confidence'
308+ )
309+ recording ['fileAnnotations' ] = [
310+ RecordingAnnotationSchema .from_orm (fileAnnotation ).dict ()
311+ for fileAnnotation in fileAnnotations
312+ ]
263313
264314 return recording
265315 else :
@@ -268,6 +318,18 @@ def get_recording(request: HttpRequest, id: int):
268318 return {'error' : 'Recording not found' }
269319
270320
321+ @router .get ('/{recording_id}/recording-annotations' )
322+ def get_recording_annotations (request : HttpRequest , recording_id : int ):
323+ fileAnnotations = RecordingAnnotation .objects .filter (recording = recording_id ).order_by (
324+ 'confidence'
325+ )
326+ output = [
327+ RecordingAnnotationSchema .from_orm (fileAnnotation ).dict ()
328+ for fileAnnotation in fileAnnotations
329+ ]
330+ return output
331+
332+
271333@router .get ('/{id}/spectrogram' )
272334def get_spectrogram (request : HttpRequest , id : int ):
273335 try :
0 commit comments