1
1
import datetime
2
2
import json
3
- import os
4
3
import tempfile
5
4
from typing import Dict
6
5
7
6
import mmif
8
7
from flask import url_for
9
8
from mmif import AnnotationTypes , DocumentTypes , Mmif
9
+ from mmif .utils import video_document_helper as vdh
10
10
11
11
import cache
12
+ import utils
12
13
13
14
14
15
def generate_iiif_manifest (in_mmif : mmif .Mmif , viz_id ):
@@ -27,18 +28,20 @@ def generate_iiif_manifest(in_mmif: mmif.Mmif, viz_id):
27
28
],
28
29
"structures" : []
29
30
}
30
- add_canvas_from_documents (in_mmif , iiif_json )
31
+ add_canvas_from_documents (viz_id , in_mmif , iiif_json )
31
32
add_structure_from_timeframe (in_mmif , iiif_json )
32
33
return save_manifest (iiif_json , viz_id )
33
34
34
35
35
- def add_canvas_from_documents (in_mmif , iiif_json ):
36
+ def add_canvas_from_documents (viz_id , in_mmif , iiif_json ):
36
37
video_documents = in_mmif .get_documents_by_type (DocumentTypes .VideoDocument )
37
38
audio_documents = in_mmif .get_documents_by_type (DocumentTypes .AudioDocument )
38
39
image_documents = in_mmif .get_documents_by_type (DocumentTypes .ImageDocument )
39
40
all_documents = video_documents + audio_documents + image_documents
40
41
document_canvas_dict = {}
41
42
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 )} " )
42
45
document_canvas_dict [document .id ] = _id
43
46
canvas = {
44
47
"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):
62
65
"choiceHint" : "user" ,
63
66
"items" : [
64
67
{
65
- "id" : build_document_url ( document ) ,
68
+ "id" : canvas_media_path ,
66
69
"type" : get_iiif_type (document ),
67
70
"label" : "" ,
68
71
"format" : get_iiif_format (document )
@@ -76,34 +79,37 @@ def add_canvas_from_documents(in_mmif, iiif_json):
76
79
}
77
80
],
78
81
}
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
- # )
84
82
iiif_json ["sequences" ][0 ]["canvases" ].append (canvas )
85
83
break # todo currently only supports single document, needs more work to align canvas values
86
84
87
85
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
-
102
86
def add_structure_from_timeframe (in_mmif : Mmif , iiif_json : Dict ):
103
87
# # get all views with timeframe annotations from mmif obj
104
88
tf_views = in_mmif .get_views_contain (AnnotationTypes .TimeFrame )
105
89
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 )
107
113
iiif_json ["structures" ].append (view_range )
108
114
109
115
@@ -115,55 +121,6 @@ def save_manifest(iiif_json: Dict, viz_id) -> str:
115
121
return manifest .name
116
122
117
123
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
-
167
124
def get_iiif_format (document ):
168
125
if document .is_type (DocumentTypes .VideoDocument ):
169
126
return 'video/mp4'
0 commit comments