18
18
from lapps .discriminators import Uri
19
19
from iiif_utils import generate_iiif_manifest
20
20
from ocr import *
21
+ from datetime import timedelta
21
22
22
23
23
24
# Get Properties from MMIF file ---
27
28
28
29
def get_alignments (alignment_view ):
29
30
vtt_file = tempfile .NamedTemporaryFile ('w' , dir = "static/" , suffix = '.vtt' , delete = False )
30
- vtt_file .write ("WebVTT \n \n " )
31
+ vtt_file .write ("WEBVTT \n \n " )
31
32
annotations = alignment_view .annotations
33
+ timeframe_at_type = [at_type for at_type in alignment_view .metadata .contains if at_type .shortname == "TimeFrame" ][0 ]
34
+ timeunit = alignment_view .metadata .contains [timeframe_at_type ]["timeUnit" ]
32
35
# TODO: wanted to use "mmif.get_alignments(AnnotationTypes.TimeFrame, Uri.TOKEN)"
33
36
# but that gave errors so I gave up on it
34
37
token_idx = {a .id :a for a in annotations if a .at_type .shortname == "Token" }
@@ -45,11 +48,13 @@ def get_alignments(alignment_view):
45
48
# ISO format can have up to 6 below the decimal point, on the other hand
46
49
# Assuming here that start and end are in miliseconds
47
50
start , end , text = start_end_text
51
+ start_kwarg , end_kwarg = {timeunit : float (start )}, {timeunit : float (end )}
52
+ start , end = timedelta (** start_kwarg ), timedelta (** end_kwarg )
48
53
if not vtt_start :
49
- vtt_start = f'{ start // 60000 :02d} :{ start % 60000 // 1000 :02d} .{ start % 1000 :03d} '
54
+ vtt_start = f'{ start . seconds // 3600 :02d} :{ start . seconds :02d} .{ start . microseconds // 1000 :03d} '
50
55
texts .append (text )
51
56
if len (texts ) > 8 :
52
- vtt_end = f'{ end // 60000 :02d} :{ end % 60000 // 1000 :02d} .{ end % 1000 :03d} '
57
+ vtt_end = f'{ end . seconds // 3600 :02d} :{ end . seconds :02d} .{ end . microseconds // 1000 :03d} '
53
58
vtt_file .write (f'{ vtt_start } --> { vtt_end } \n { " " .join (texts )} \n \n ' )
54
59
vtt_start = None
55
60
texts = []
@@ -224,7 +229,7 @@ def get_alignment_views(mmif):
224
229
needed_types = set (['TextDocument' , 'Token' , 'TimeFrame' , 'Alignment' ])
225
230
for view in mmif .views :
226
231
annotation_types = view .metadata .contains .keys ()
227
- annotation_types = [os . path . split ( str ( at ))[ - 1 ] for at in annotation_types ]
232
+ annotation_types = [at . shortname for at in annotation_types ]
228
233
if needed_types .issubset (annotation_types ):
229
234
views .append (view )
230
235
return views
@@ -243,7 +248,7 @@ def html_video(vpath, vtt_srcview=None):
243
248
# use only basename because "static" directory is mapped to '' route by
244
249
# `static_url_path` param
245
250
src = os .path .basename (vtt_path )
246
- html .write (f' <track kind="subtitles" srclang="en" src="{ src } " default>\n ' )
251
+ html .write (f' <track kind="subtitles" srclang="en" src="{ src } " label="English" default>\n ' )
247
252
html .write ("</video>\n " )
248
253
return html .getvalue ()
249
254
@@ -295,7 +300,7 @@ def get_aligned_views(mmif):
295
300
"""Return list of properly aligned views (for tree display)"""
296
301
aligned_views = []
297
302
for view in mmif .views :
298
- if any ([at_type .shortname == "Token " for at_type in view .metadata .contains ]):
303
+ if any ([at_type .shortname == "Alignment " for at_type in view .metadata .contains ]):
299
304
if check_view_alignment (view .annotations ) == True :
300
305
aligned_views .append (view .id )
301
306
return aligned_views
@@ -308,7 +313,7 @@ def check_view_alignment(annotations):
308
313
else :
309
314
anno_stack .append (annotation .id )
310
315
if len (anno_stack ) == 3 :
311
- if type (anno_stack [0 ] == str ) or not (anno_stack [0 ]["source" ] in anno_stack and anno_stack [0 ]["target" ] in anno_stack ):
316
+ if type (anno_stack [0 ]) == str or not (anno_stack [0 ]["source" ] in anno_stack and anno_stack [0 ]["target" ] in anno_stack ):
312
317
return False
313
318
anno_stack = []
314
319
return True
0 commit comments