Skip to content

Commit f147dcd

Browse files
VTT bug fixes and better at_type methods
1 parent 38334b6 commit f147dcd

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

ocr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ 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)
6970
cv2.imwrite(tf.name, frame_cap)
7071
# "id" is just the name of the temp image file
7172
frame["id"] = tf.name[12:]

utils.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def get_alignments(alignment_view):
3131
annotations = alignment_view.annotations
3232
# TODO: wanted to use "mmif.get_alignments(AnnotationTypes.TimeFrame, Uri.TOKEN)"
3333
# but that gave errors so I gave up on it
34-
token_idx = {a.id:a for a in annotations if "Token" in str(a.at_type)}
35-
timeframe_idx = {a.id:a for a in annotations if "TimeFrame" in str(a.at_type)}
36-
alignments = [a for a in annotations if "Alignment" in str(a.at_type)]
34+
token_idx = {a.id:a for a in annotations if a.at_type.shortname == "Token"}
35+
timeframe_idx = {a.id:a for a in annotations if a.at_type.shortname == "TimeFrame"}
36+
alignments = [a for a in annotations if a.at_type.shortname == "Alignment"]
3737
vtt_start = None
3838
texts = []
3939
for alignment in alignments:
@@ -46,10 +46,10 @@ def get_alignments(alignment_view):
4646
# Assuming here that start and end are in miliseconds
4747
start, end, text = start_end_text
4848
if not vtt_start:
49-
vtt_start = f'{start // 60000:02d}:{start % 60000 // 1000}.{start % 1000:03d}'
49+
vtt_start = f'{start // 60000:02d}:{start % 60000 // 1000:02d}.{start % 1000:03d}'
5050
texts.append(text)
5151
if len(texts) > 8:
52-
vtt_end = f'{end // 60000:02d}:{end % 60000 // 1000}.{end % 1000:03d}'
52+
vtt_end = f'{end // 60000:02d}:{end % 60000 // 1000:02d}.{end % 1000:03d}'
5353
vtt_file.write(f'{vtt_start} --> {vtt_end}\n{" ".join(texts)}\n\n')
5454
vtt_start = None
5555
texts = []
@@ -120,12 +120,9 @@ def get_boxes(mmif):
120120
def get_document_type_short_form(document):
121121
"""Returns 'Video', 'Text', 'Audio' or 'Image' from the document type of
122122
the document."""
123-
# Remove trailing "/v1" in newer @types
124-
at_type = re.sub(r"\/v\d*$", "", str(document.at_type))
125-
document_type = os.path.split(at_type)[1]
123+
document_type = document.at_type.shortname
126124
return document_type[:-8]
127125

128-
129126
def prep_annotations(mmif):
130127
"""Prepare annotations from the views, and return a list of pairs of tabname
131128
and tab content. The first tab is alway the full MMIF pretty print."""
@@ -170,7 +167,7 @@ def get_video_path(mmif):
170167
def create_info(mmif):
171168
s = StringIO('Howdy')
172169
for document in mmif.documents:
173-
at_type = str(document.at_type).rsplit('/', 1)[-1]
170+
at_type = document.at_type.shortname
174171
location = document.location
175172
s.write("%s %s\n" % (at_type, location))
176173
s.write('\n')
@@ -180,7 +177,7 @@ def create_info(mmif):
180177
s.write('%s %s %s %d\n' % (view.id, app, status, len(view.annotations)))
181178
if len(view.annotations) > 0:
182179
s.write('\n')
183-
types = Counter([str(a.at_type).rsplit('/', 1)[-1]
180+
types = Counter([a.at_type.shortname
184181
for a in view.annotations])
185182
for attype, count in types.items():
186183
s.write(' %4d %s\n' % (count, attype))
@@ -200,7 +197,7 @@ def create_annotation_tables(mmif):
200197
for annotation in view.annotations:
201198
s.write(' <tr>\n')
202199
s.write(' <td>%s</td>\n' % annotation.id)
203-
s.write(' <td>%s</td>\n' % str(annotation.at_type).split('/')[-1])
200+
s.write(' <td>%s</td>\n' % annotation.at_type.shortname)
204201
s.write(' <td>%s</td>\n' % limit_len(get_properties(annotation)))
205202
s.write(' </tr>\n')
206203
s.write("</table>\n")
@@ -212,7 +209,7 @@ def get_document_ids(view, annotation_type):
212209
metadata = view.metadata.contains.get(annotation_type)
213210
ids = set([metadata['document']]) if 'document' in metadata else set()
214211
for annotation in view.annotations:
215-
if str(annotation_type) in str(annotation.at_type):
212+
if annotation.at_type.shortname == str(annotation_type):
216213
try:
217214
ids.add(annotation.properties["document"])
218215
except KeyError:
@@ -298,20 +295,20 @@ def get_aligned_views(mmif):
298295
"""Return list of properly aligned views (for tree display)"""
299296
aligned_views = []
300297
for view in mmif.views:
301-
if any(["Alignment" in str(at_type) for at_type in view.metadata.contains]):
298+
if any([at_type.shortname == "Token" for at_type in view.metadata.contains]):
302299
if check_view_alignment(view.annotations) == True:
303300
aligned_views.append(view.id)
304301
return aligned_views
305302

306303
def check_view_alignment(annotations):
307304
anno_stack = []
308305
for annotation in annotations:
309-
if "Alignment" in str(annotation.at_type):
306+
if annotation.at_type.shortname == "Alignment":
310307
anno_stack.insert(0, annotation.properties)
311308
else:
312309
anno_stack.append(annotation.id)
313310
if len(anno_stack) == 3:
314-
if not (anno_stack[0]["source"] in anno_stack and anno_stack[0]["target"] in anno_stack):
311+
if type(anno_stack[0] == str) or not (anno_stack[0]["source"] in anno_stack and anno_stack[0]["target"] in anno_stack):
315312
return False
316313
anno_stack = []
317314
return True
@@ -357,17 +354,17 @@ def prepare_ocr_visualization(mmif, view):
357354
frames, text_docs, alignments = {}, {}, {}
358355
for anno in view.annotations:
359356
try:
360-
if "BoundingBox" in str(anno.at_type):
357+
if anno.at_type.shortname == "BoundingBox":
361358
frames = add_bounding_box(anno, frames)
362359

363-
elif "TextDocument" in str(anno.at_type):
360+
elif anno.at_type.shortname == "TextDocument":
364361
t = anno.properties["text_value"]
365362
if t:
366363
text_id = anno.properties["id"]
367364
# Format string so it is JSON-readable
368365
text_docs[text_id] = re.sub(r'([\\\/\|\"\'])', r'\1 ', t)
369366

370-
elif "Alignment" in str(anno.at_type):
367+
elif anno.at_type.shortname == "Alignment":
371368
source = anno.properties["source"]
372369
target = anno.properties["target"]
373370
alignments[source] = target

0 commit comments

Comments
 (0)