Skip to content

Commit

Permalink
fix(toolbox): meaning of timestamp
Browse files Browse the repository at this point in the history
frame 1 's timestamp is the beginning (not the end now) of this frame
  • Loading branch information
williamfzc committed Mar 1, 2020
1 parent 746abf4 commit 22aef44
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions stagesepx/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ def video_capture(video_path: str):
def video_jump(video_cap: cv2.VideoCapture, frame_id: int):
# IMPORTANT:
# - frame is a range actually
# - frame 1 's timestamp is the end of this frame
# - frame 1 's timestamp is the beginning of this frame
#
# video_jump(cap, 1) means: moving the pointer to the start point of frame 1 => the end point of frame 0
# video_jump(cap, 2) means: moving the pointer to the start point of frame 2 => the end point of frame 1

video_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id - 1)
# -1 for moving to the beginning
# another -1 for re-read
video_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id - 1 - 1)
video_cap.read()
logger.debug(
f"current pointer: {get_current_frame_id(video_cap)}({get_current_frame_time(video_cap)})"
)
Expand Down Expand Up @@ -78,12 +81,12 @@ def get_frame_time(
video_cap: cv2.VideoCapture, frame_id: int, recover: bool = None
) -> float:
cur = get_current_frame_id(video_cap)
video_jump(video_cap, frame_id + 1)
video_jump(video_cap, frame_id)
result = get_current_frame_time(video_cap)
logger.debug(f"frame {frame_id} -> {result}")

if recover:
video_jump(video_cap, cur + 1)
video_jump(video_cap, cur)
return result


Expand All @@ -109,7 +112,7 @@ def get_frame(
assert ret, f"read frame failed, frame id: {frame_id}"

if recover:
video_jump(video_cap, cur + 1)
video_jump(video_cap, cur)
return frame


Expand Down

0 comments on commit 22aef44

Please sign in to comment.