11import datetime
22import json
3- import os
43import tempfile
54from typing import Dict
65
76import mmif
87from flask import url_for
98from mmif import AnnotationTypes , DocumentTypes , Mmif
9+ from mmif .utils import video_document_helper as vdh
1010
1111import cache
12+ import utils
1213
1314
1415def generate_iiif_manifest (in_mmif : mmif .Mmif , viz_id ):
@@ -27,18 +28,20 @@ def generate_iiif_manifest(in_mmif: mmif.Mmif, viz_id):
2728 ],
2829 "structures" : []
2930 }
30- add_canvas_from_documents (in_mmif , iiif_json )
31+ add_canvas_from_documents (viz_id , in_mmif , iiif_json )
3132 add_structure_from_timeframe (in_mmif , iiif_json )
3233 return save_manifest (iiif_json , viz_id )
3334
3435
35- def add_canvas_from_documents (in_mmif , iiif_json ):
36+ def add_canvas_from_documents (viz_id , in_mmif , iiif_json ):
3637 video_documents = in_mmif .get_documents_by_type (DocumentTypes .VideoDocument )
3738 audio_documents = in_mmif .get_documents_by_type (DocumentTypes .AudioDocument )
3839 image_documents = in_mmif .get_documents_by_type (DocumentTypes .ImageDocument )
3940 all_documents = video_documents + audio_documents + image_documents
4041 document_canvas_dict = {}
4142 for _id , document in enumerate (all_documents , start = 1 ):
43+ canvas_media_path = url_for (
44+ 'static' , filename = f"{ cache ._CACHE_DIR_SUFFIX } /{ viz_id } /{ utils .get_src_media_symlink_basename (document )} " )
4245 document_canvas_dict [document .id ] = _id
4346 canvas = {
4447 "id" : f"http://0.0.0.0:5000/mmif_example_manifest.json/canvas/{ _id } " ,
@@ -62,7 +65,7 @@ def add_canvas_from_documents(in_mmif, iiif_json):
6265 "choiceHint" : "user" ,
6366 "items" : [
6467 {
65- "id" : build_document_url ( document ) ,
68+ "id" : canvas_media_path ,
6669 "type" : get_iiif_type (document ),
6770 "label" : "" ,
6871 "format" : get_iiif_format (document )
@@ -76,34 +79,37 @@ def add_canvas_from_documents(in_mmif, iiif_json):
7679 }
7780 ],
7881 }
79- # if not os.path.isfile(f"static{document.location_path()}"):
80- # shutil.copyfile(
81- # f"{document.location_path()}",
82- # f"static{os.path.basename(document.location_path())}"
83- # )
8482 iiif_json ["sequences" ][0 ]["canvases" ].append (canvas )
8583 break # todo currently only supports single document, needs more work to align canvas values
8684
8785
88- def build_document_url (document ):
89- """
90- This trims off all of the path to the document except the filename then prepends data/video/. This is so
91- mmif's from running locally can still be found if the viewe
92- r is run in docker, assuming the volume mount or
93- symlink is correctly set.
94- """
95- location = document .location
96- if location .startswith ("file://" ):
97- location = document .location [7 :]
98- file_path = os .path .join ("data" , "video" , os .path .basename (location ))
99- return url_for ('static' , filename = file_path )
100-
101-
10286def add_structure_from_timeframe (in_mmif : Mmif , iiif_json : Dict ):
10387 # # get all views with timeframe annotations from mmif obj
10488 tf_views = in_mmif .get_views_contain (AnnotationTypes .TimeFrame )
10589 for range_id , view in enumerate (tf_views , start = 1 ):
106- view_range = tf_view_to_iiif_range (range_id , view )
90+ view_range = {
91+ "id" : f"http://0.0.0.0:5000/mmif_example_manifest.json/range/{ range_id } " ,
92+ "type" : "Range" ,
93+ "label" : f"View: { view .id } " ,
94+ "members" : []
95+ }
96+ for ann in view .get_annotations (AnnotationTypes .TimeFrame ):
97+ label = ann .get_property ('label' )
98+ s , e = vdh .convert_timeframe (in_mmif , ann , "seconds" )
99+
100+ structure = {
101+ "id" : f"http://0.0.0.0:5000/mmif_example_manifest.json/range/{ range_id } " ,
102+ "type" : "Range" ,
103+ "label" : f"{ label .capitalize ()} " ,
104+ "members" : [
105+ {
106+ "id" : f"http://0.0.0.0:5000/mmif_example_manifest.json/canvas/{ 1 } #t={ s } ,{ e } " ,
107+ # need to align id here to support more than one document
108+ "type" : "Canvas"
109+ }
110+ ]
111+ }
112+ view_range ["members" ].append (structure )
107113 iiif_json ["structures" ].append (view_range )
108114
109115
@@ -115,55 +121,6 @@ def save_manifest(iiif_json: Dict, viz_id) -> str:
115121 return manifest .name
116122
117123
118- def tf_view_to_iiif_range (range_id , view ):
119- view_range = {
120- "id" : f"http://0.0.0.0:5000/mmif_example_manifest.json/range/{ range_id } " ,
121- "type" : "Range" ,
122- "label" : f"View: { view .id } " ,
123- "members" : []
124- }
125- # for annotation in view.annotations:
126- # # TODO: TimeUnits generated by Kaldi have no "timeUnit" or "unit" property.
127- # The mmif documentation does specify a "unit" property, but the Kaldi
128- # ASR doesn't seem to include that in annotations.
129-
130- # if annotation.at_type == AnnotationTypes.TimeFrame:
131- # if 'unit' in annotation.properties:
132- # annotation_unit = annotation.properties['unit']
133- # elif 'unit' in view.metadata.parameters:
134- # annotation_unit = view.metadata.parameters['unit']
135- # else:
136- # raise Exception("Error finding timeframe unit.")
137- # frame_type = annotation.properties["frameType"]
138- # if annotation_unit == "frame":
139- # start_fn = int(annotation.properties["start"])
140- # end_fn = int(annotation.properties["end"])
141- # frame_rate = 29.97
142- # start_sec = int(start_fn // frame_rate)
143- # end_sec = int(end_fn // frame_rate)
144- # elif annotation_unit == "milliseconds":
145- # start_milli = int(annotation.properties["start"])
146- # end_milli = int(annotation.properties["end"])
147- # start_sec = int(start_milli // 1000)
148- # end_sec = int(end_milli // 1000)
149- # else:
150- # continue
151- # structure = {
152- # "id": f"http://0.0.0.0:5000/mmif_example_manifest.json/range/{range_id}",
153- # "type": "Range",
154- # "label": f"{frame_type.capitalize()}",
155- # "members": [
156- # {
157- # "id": f"http://0.0.0.0:5000/mmif_example_manifest.json/canvas/{1}#t={start_sec},{end_sec}",
158- # # need to align id here to support more than one document
159- # "type": "Canvas"
160- # }
161- # ]
162- # }
163- # view_range["members"].append(structure)
164- return view_range
165-
166-
167124def get_iiif_format (document ):
168125 if document .is_type (DocumentTypes .VideoDocument ):
169126 return 'video/mp4'
0 commit comments