Skip to content

Commit

Permalink
workaround when reading broken video frames
Browse files Browse the repository at this point in the history
Some videos are being read with additional fake/broken frames that may
cause an unexpected early exit. This attempts to skip over these broken
frames if more were expected, based on the initally acquired frame
count.

According to other reports upstream, the number of broken frames read
varies. To avoid getting stuck in an endless loop, e.g. when the video
is actually broken, this gives up after 1000 reads.

see ultralytics#2064
see opencv/opencv#15352
  • Loading branch information
breunigs committed Dec 23, 2021
1 parent abbdd48 commit 7cafb37
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ def __next__(self):
# Read video
self.mode = 'video'
ret_val, img0 = self.cap.read()

if not ret_val and self.frame < self.frames - 1:
fake_frame_skip = 1000
while not ret_val and fake_frame_skip > 0:
fake_frame_skip -= 1
ret_val, img0 = self.cap.read()
if not ret_val:
raise Exception(
f'Video stream ended unexpectedly. See this issue for details: https://github.com/ultralytics/yolov5/issues/2064')

while not ret_val:
self.count += 1
self.cap.release()
Expand Down

0 comments on commit 7cafb37

Please sign in to comment.