Skip to content

Commit 4c5a701

Browse files
committed
Alignments from #2
1 parent 53d301a commit 4c5a701

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

app.py

+18-30
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,37 @@ def _annotate(self, mmif: Union[str, dict, Mmif], **parameters) -> Mmif:
3030
video_doc: Document = mmif.get_documents_by_type(DocumentTypes.VideoDocument)[0]
3131
input_view: View = mmif.get_views_for_document(video_doc.properties.id)[0]
3232

33-
config = self.get_configuration(**parameters)
3433
new_view: View = mmif.new_view()
3534
self.sign_view(new_view, parameters)
3635

3736
for timeframe in input_view.get_annotations(AnnotationTypes.TimeFrame):
3837
self.logger.debug(timeframe.properties)
39-
# get images from time frame
40-
if config["sampleFrames"] == 1:
41-
self.logger.debug("Sampling 1 frame")
42-
image: np.ndarray = vdh.extract_mid_frame(mmif, timeframe, as_PIL=False)
43-
self.logger.debug("Extracted image")
44-
self.logger.debug("Running OCR")
45-
ocrs = [self.reader.readtext(image, width_ths=0.25)]
46-
self.logger.debug(ocrs)
47-
else:
48-
self.logger.debug(f"Sampling {config['sampleFrames']} frames")
49-
timeframe_length = int(timeframe.properties["end"] - timeframe.properties["start"])
50-
sample_frames = config["sampleFrames"]
51-
if timeframe_length < sample_frames:
52-
sample_frames = int(timeframe_length)
53-
sample_ratio = int(timeframe.get("end") - timeframe.get("start")) // sample_frames
54-
tf_sample = vdh.sample_frames(timeframe.get("start"), timeframe.get("end"), sample_ratio)
55-
images = vdh.extract_frames_as_images(video_doc, tf_sample)
56-
ocrs = []
57-
for image in images:
58-
ocrs.append(self.reader.readtext(image, width_ths=0.25))
59-
38+
self.logger.debug("Sampling 1 frame")
39+
# Samples midframe of timeframe until SWT outputs TimePoints
40+
image: np.ndarray = vdh.extract_mid_frame(mmif, timeframe, as_PIL=False)
41+
self.logger.debug("Extracted image")
42+
self.logger.debug("Running OCR")
43+
ocrs = [self.reader.readtext(image, width_ths=0.25)]
44+
self.logger.debug(ocrs)
45+
timepoint = new_view.new_annotation(AnnotationTypes.TimePoint)
46+
timepoint.add_property("timePoint", vdh.get_mid_framenum(mmif, timeframe))
47+
point_frame = new_view.new_annotation(AnnotationTypes.Alignment)
48+
point_frame.add_property("source", timeframe.id)
49+
point_frame.add_property("target", timepoint.id)
6050
for ocr in ocrs:
6151
for coord, text, score in ocr:
6252
if score > 0.4:
6353
self.logger.debug("Confident OCR: " + text)
6454
text_document = new_view.new_textdocument(text)
6555
bbox_annotation = new_view.new_annotation(AnnotationTypes.BoundingBox)
66-
timepoint = new_view.new_annotation(AnnotationTypes.TimePoint)
67-
timepoint.add_property("timePoint", timeframe.properties["start"])
6856
bbox_annotation.add_property("coordinates", coord)
6957
bbox_annotation.add_property("boxType", "text")
70-
text_align = new_view.new_annotation(AnnotationTypes.Alignment)
71-
text_align.add_property("source", bbox_annotation.id)
72-
text_align.add_property("target", text_document.id)
73-
time_align = new_view.new_annotation(AnnotationTypes.Alignment)
74-
time_align.add_property("source", bbox_annotation.id)
75-
time_align.add_property("target", timepoint.id)
58+
time_bbox = new_view.new_annotation(AnnotationTypes.Alignment)
59+
time_bbox.add_property("source", timepoint.id)
60+
time_bbox.add_property("target", bbox_annotation.id)
61+
bbox_text = new_view.new_annotation(AnnotationTypes.Alignment)
62+
bbox_text.add_property("source", bbox_annotation.id)
63+
bbox_text.add_property("target", text_document.id)
7664

7765
return mmif
7866

0 commit comments

Comments
 (0)