1
1
import datetime
2
- import pathlib
3
2
4
3
import cv2
5
- import tempfile
6
4
import json
7
5
import re
8
- import os , shutil
6
+ import os
7
+ import shutil
9
8
10
- from flask import render_template
11
9
from mmif .utils .video_document_helper import convert_timepoint , convert_timeframe
12
10
13
11
import cache
@@ -50,16 +48,18 @@ def update(self, anno, mmif):
50
48
51
49
elif anno .at_type .shortname == "Paragraph" :
52
50
view = mmif .get_view_by_id (anno .parent )
53
- text_anno = view .get_annotation_by_id (anno .properties .get ("document" ))
51
+ text_anno = view .get_annotation_by_id (
52
+ anno .properties .get ("document" ))
54
53
self .add_text_document (text_anno )
55
54
56
-
57
55
def add_bounding_box (self , anno , mmif ):
58
56
if "timePoint" in anno .properties :
59
- timepoint_anno = find_annotation (anno .properties ["timePoint" ], mmif )
57
+ timepoint_anno = find_annotation (
58
+ anno .properties ["timePoint" ], mmif )
60
59
61
60
if timepoint_anno :
62
- self .add_timepoint (timepoint_anno , mmif , skip_if_view_has_frames = False )
61
+ self .add_timepoint (timepoint_anno , mmif ,
62
+ skip_if_view_has_frames = False )
63
63
else :
64
64
self .frame_num = convert_timepoint (mmif , anno , "frames" )
65
65
self .secs = convert_timepoint (mmif , anno , "seconds" )
@@ -82,9 +82,11 @@ def add_bounding_box(self, anno, mmif):
82
82
def add_timeframe (self , anno , mmif ):
83
83
# If annotation has multiple targets, pick the first and last as start and end
84
84
if "targets" in anno .properties :
85
- start_id , end_id = anno .properties .get ("targets" )[0 ], anno .properties .get ("targets" )[- 1 ]
85
+ start_id , end_id = anno .properties .get (
86
+ "targets" )[0 ], anno .properties .get ("targets" )[- 1 ]
86
87
anno_parent = mmif .get_view_by_id (anno .parent )
87
- start_anno , end_anno = anno_parent .get_annotation_by_id (start_id ), anno_parent .get_annotation_by_id (end_id )
88
+ start_anno , end_anno = anno_parent .get_annotation_by_id (
89
+ start_id ), anno_parent .get_annotation_by_id (end_id )
88
90
start = convert_timepoint (mmif , start_anno , "frames" )
89
91
end = convert_timepoint (mmif , end_anno , "frames" )
90
92
start_secs = convert_timepoint (mmif , start_anno , "seconds" )
@@ -93,32 +95,37 @@ def add_timeframe(self, anno, mmif):
93
95
start , end = convert_timeframe (mmif , anno , "frames" )
94
96
start_secs , end_secs = convert_timeframe (mmif , anno , "seconds" )
95
97
self .range = (start , end )
96
- self .timestamp_range = (str (datetime .timedelta (seconds = start_secs )), str (datetime .timedelta (seconds = end_secs )))
98
+ self .timestamp_range = (str (datetime .timedelta (seconds = start_secs )), str (
99
+ datetime .timedelta (seconds = end_secs )))
97
100
self .sec_range = (start_secs , end_secs )
98
101
if anno .properties .get ("frameType" ):
99
102
self .frametype = str (anno .properties .get ("frameType" ))
100
103
elif anno .properties .get ("label" ):
101
104
self .frametype = str (anno .properties .get ("label" ))
102
105
103
106
def add_timepoint (self , anno , mmif , skip_if_view_has_frames = True ):
104
- parent = mmif .get_view_by_id (anno .parent )
105
- other_annotations = [k for k in parent .metadata .contains .keys () if k != anno .id ]
106
- # If there are TimeFrames in the same view, they most likely represent
107
- # condensed information about representative frames (e.g. SWT). In this
108
- # case, only render the TimeFrames and ignore the TimePoints.
109
- if any ([anno .shortname == "TimeFrame" for anno in other_annotations ]) and skip_if_view_has_frames :
110
- return
111
- self .frame_num = convert_timepoint (mmif , anno , "frames" )
112
- self .secs = convert_timepoint (mmif , anno , "seconds" )
113
- self .timestamp = str (datetime .timedelta (seconds = self .secs ))
114
- if anno .properties .get ("label" ):
115
- self .frametype = anno .properties .get ("label" )
107
+ parent = mmif .get_view_by_id (anno .parent )
108
+ other_annotations = [
109
+ k for k in parent .metadata .contains .keys () if k != anno .id ]
110
+ # If there are TimeFrames in the same view, they most likely represent
111
+ # condensed information about representative frames (e.g. SWT). In this
112
+ # case, only render the TimeFrames and ignore the TimePoints.
113
+ if any ([anno .shortname == "TimeFrame" for anno in other_annotations ]) and skip_if_view_has_frames :
114
+ return
115
+ self .frame_num = convert_timepoint (mmif , anno , "frames" )
116
+ self .secs = convert_timepoint (mmif , anno , "seconds" )
117
+ self .timestamp = str (datetime .timedelta (seconds = self .secs ))
118
+ if anno .properties .get ("label" ):
119
+ self .frametype = anno .properties .get ("label" )
116
120
117
121
def add_text_document (self , anno ):
118
- t = anno .properties .get ("text_value" ) or anno .properties .get ("text" ).value
122
+ t = anno .properties .get (
123
+ "text_value" ) or anno .properties .get ("text" ).value
119
124
if t :
120
125
text_val = re .sub (r'([\\\/\|\"\'])' , r'\1 ' , t )
121
- self .text = self .text + [text_val ] if text_val not in self .text else self .text
126
+ self .text = self .text + \
127
+ [text_val ] if text_val not in self .text else self .text
128
+
122
129
123
130
def find_annotation (anno_id , mmif ):
124
131
if mmif .id_delimiter in anno_id :
@@ -153,7 +160,7 @@ def get_ocr_frames(view, mmif):
153
160
frames [i ].update (target , mmif )
154
161
else :
155
162
frames [i ] = frame
156
-
163
+
157
164
else :
158
165
for annotation in view .get_annotations ():
159
166
frame = OCRFrame (annotation , mmif )
@@ -185,6 +192,7 @@ def paginate(frames_list):
185
192
186
193
return {i : page for (i , page ) in enumerate (pages )}
187
194
195
+
188
196
def make_image_directory (mmif_id ):
189
197
# Make path for temp OCR image files or clear image files if it exists
190
198
path = cache .get_cache_root () / mmif_id / "img"
@@ -232,10 +240,14 @@ def is_duplicate_image(prev_frame, frame, cv2_vid):
232
240
img2_hsv = cv2 .cvtColor (frame , cv2 .COLOR_BGR2HSV )
233
241
234
242
# Calculate the histogram and normalize it
235
- hist_img1 = cv2 .calcHist ([img1_hsv ], [0 , 1 ], None , [180 , 256 ], [0 , 180 , 0 , 256 ])
236
- cv2 .normalize (hist_img1 , hist_img1 , alpha = 0 , beta = 1 , norm_type = cv2 .NORM_MINMAX );
237
- hist_img2 = cv2 .calcHist ([img2_hsv ], [0 , 1 ], None , [180 , 256 ], [0 , 180 , 0 , 256 ])
238
- cv2 .normalize (hist_img2 , hist_img2 , alpha = 0 , beta = 1 , norm_type = cv2 .NORM_MINMAX );
243
+ hist_img1 = cv2 .calcHist ([img1_hsv ], [0 , 1 ], None , [
244
+ 180 , 256 ], [0 , 180 , 0 , 256 ])
245
+ cv2 .normalize (hist_img1 , hist_img1 , alpha = 0 ,
246
+ beta = 1 , norm_type = cv2 .NORM_MINMAX )
247
+ hist_img2 = cv2 .calcHist ([img2_hsv ], [0 , 1 ], None , [
248
+ 180 , 256 ], [0 , 180 , 0 , 256 ])
249
+ cv2 .normalize (hist_img2 , hist_img2 , alpha = 0 ,
250
+ beta = 1 , norm_type = cv2 .NORM_MINMAX )
239
251
240
252
# Find the metric value
241
253
metric_val = cv2 .compareHist (hist_img1 , hist_img2 , cv2 .HISTCMP_CHISQR )
0 commit comments