Skip to content

Commit 54ba622

Browse files
Write VTTs based on view TimeUnit
1 parent f147dcd commit 54ba622

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ tags
7474
# static archival files
7575
static/tmp*
7676

77+
# VSCode
78+
.devcontainer
79+
devcontainer.json

ocr.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def render_ocr(vid_path, frames_pages, page_number):
6666
_, frame_cap = cv2_vid.read()
6767
with tempfile.NamedTemporaryFile(
6868
prefix="/app/static/tmp/", suffix=".jpg", delete=False) as tf:
69-
print(tf.name)
7069
cv2.imwrite(tf.name, frame_cap)
7170
# "id" is just the name of the temp image file
7271
frame["id"] = tf.name[12:]

utils.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from lapps.discriminators import Uri
1919
from iiif_utils import generate_iiif_manifest
2020
from ocr import *
21+
from datetime import timedelta
2122

2223

2324
# Get Properties from MMIF file ---
@@ -27,8 +28,10 @@
2728

2829
def get_alignments(alignment_view):
2930
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")
3132
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"]
3235
# TODO: wanted to use "mmif.get_alignments(AnnotationTypes.TimeFrame, Uri.TOKEN)"
3336
# but that gave errors so I gave up on it
3437
token_idx = {a.id:a for a in annotations if a.at_type.shortname == "Token"}
@@ -45,11 +48,13 @@ def get_alignments(alignment_view):
4548
# ISO format can have up to 6 below the decimal point, on the other hand
4649
# Assuming here that start and end are in miliseconds
4750
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)
4853
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}'
5055
texts.append(text)
5156
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}'
5358
vtt_file.write(f'{vtt_start} --> {vtt_end}\n{" ".join(texts)}\n\n')
5459
vtt_start = None
5560
texts = []
@@ -224,7 +229,7 @@ def get_alignment_views(mmif):
224229
needed_types = set(['TextDocument', 'Token', 'TimeFrame', 'Alignment'])
225230
for view in mmif.views:
226231
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]
228233
if needed_types.issubset(annotation_types):
229234
views.append(view)
230235
return views
@@ -243,7 +248,7 @@ def html_video(vpath, vtt_srcview=None):
243248
# use only basename because "static" directory is mapped to '' route by
244249
# `static_url_path` param
245250
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')
247252
html.write("</video>\n")
248253
return html.getvalue()
249254

@@ -295,7 +300,7 @@ def get_aligned_views(mmif):
295300
"""Return list of properly aligned views (for tree display)"""
296301
aligned_views = []
297302
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]):
299304
if check_view_alignment(view.annotations) == True:
300305
aligned_views.append(view.id)
301306
return aligned_views
@@ -308,7 +313,7 @@ def check_view_alignment(annotations):
308313
else:
309314
anno_stack.append(annotation.id)
310315
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):
312317
return False
313318
anno_stack = []
314319
return True

0 commit comments

Comments
 (0)