Skip to content

Commit af83e13

Browse files
authored
Merge pull request #303 from clamsproject/302-vdh-destroy-frame-list
Fix the issue of extract_frames_as_images destory input frame
2 parents 2e17432 + c94106c commit af83e13

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

mmif/utils/video_document_helper.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,19 @@ def extract_frames_as_images(video_document: Document, framenums: List[int], as_
8484
video = capture(video_document)
8585
cur_f = 0
8686
tot_fcount = video_document.get_property(FRAMECOUNT_DOCPROP_KEY)
87+
framenums_copy = framenums.copy()
8788
while True:
88-
if not framenums or cur_f > tot_fcount:
89+
if not framenums_copy or cur_f > tot_fcount:
8990
break
9091
ret, frame = video.read()
91-
if cur_f == framenums[0]:
92+
if cur_f == framenums_copy[0]:
9293
if not ret:
9394
sec = convert(cur_f, 'f', 's', video_document.get_property(FPS_DOCPROP_KEY))
9495
warnings.warn(f'Frame #{cur_f} ({sec}s) could not be read from the video {video_document.id}.')
9596
cur_f += 1
9697
continue
9798
frames.append(Image.fromarray(frame[:, :, ::-1]) if as_PIL else frame)
98-
framenums.pop(0)
99+
framenums_copy.pop(0)
99100
cur_f += 1
100101
return frames
101102

tests/black-2997fps.mp4

63.1 KB
Binary file not shown.

tests/test_utils.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pathlib
12
import unittest
23

34
import pytest
@@ -31,7 +32,7 @@ def setUp(self):
3132
"properties": {
3233
"mime": "video",
3334
"id": "d1",
34-
"location": "file:///home/snewman/Documents/test_vid.mp4"
35+
"location": f"file://{pathlib.Path(__file__).parent}/black-2997fps.mp4"
3536
}
3637
})
3738
self.video_doc.add_property('fps', self.fps)
@@ -105,6 +106,23 @@ def test_convert_timeframe(self):
105106
for times in zip((3.337, 6.674), vdh.convert_timeframe(self.mmif_obj, timeframe_ann, 's')):
106107
self.assertAlmostEqual(*times, places=0)
107108

109+
def test_extract_frames_as_images(self):
110+
frame_list = [5, 10, 15]
111+
target_images = vdh.extract_frames_as_images(self.video_doc, frame_list, as_PIL=False)
112+
self.assertEqual(3, len(target_images))
113+
# check if the extract_frames_as_images destroy the input frame list
114+
self.assertEqual(3, len(frame_list))
115+
# check return empty list if the frame list is empty
116+
empty_flist = []
117+
empty_target_images = vdh.extract_frames_as_images(self.video_doc, empty_flist, as_PIL=False)
118+
self.assertEqual([], empty_target_images)
119+
# check there is an error if there is a frame in the list that does not exist
120+
tot_fcount = self.video_doc.get_property('frameCount')
121+
frame_list.append(tot_fcount + 1)
122+
new_target_images = vdh.extract_frames_as_images(self.video_doc, frame_list, as_PIL=False)
123+
self.assertEqual(4, len(frame_list))
124+
self.assertEqual(3, len(new_target_images))
125+
108126

109127
class TestSequenceHelper(unittest.TestCase):
110128

0 commit comments

Comments
 (0)