docs icon indicating copy to clipboard operation
docs copied to clipboard

Fix randint TypeError in video tutorial by casting frame count to int

Open joaoascenso02 opened this issue 3 months ago • 2 comments

Problem

In frames_from_video_file (inside site/en/tutorials/load_data/video.ipynb), the code fails when running:

sample_video = frames_from_video_file(video_path, n_frames=10)
sample_video.shape

with the following error:

TypeError: 'float' object cannot be interpreted as an integer

This happens because:

  video_length = src.get(cv2.CAP_PROP_FRAME_COUNT)

  need_length = 1 + (n_frames - 1) * frame_step

  if need_length > video_length:
    start = 0
  else:
    max_start = video_length - need_length
    start = random.randint(0, max_start + 1)

cv2.VideoCapture().get(cv2.CAP_PROP_FRAME_COUNT) returns a float. As a result, max_start is a float, and random.randint requires integers.

Fix

Cast max_start to int:

max_start = int(video_length - need_length)

Result

The function now works correctly, and the tutorial executes without raising TypeError.

joaoascenso02 avatar Sep 23 '25 14:09 joaoascenso02

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Sep 23 '25 14:09 google-cla[bot]

Preview

Preview and run these notebook edits with Google Colab: Rendered notebook diffs available on ReviewNB.com.

Format and style

Use the TensorFlow docs notebook tools to format for consistent source diffs and lint for style:
$ python3 -m pip install -U --user git+https://github.com/tensorflow/docs

$ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb
$ python3 -m tensorflow_docs.tools.nblint --arg=repo:tensorflow/docs notebook.ipynb
If commits are added to the pull request, synchronize your local branch: git pull origin fix/video-tutorial-randint

github-actions[bot] avatar Sep 23 '25 14:09 github-actions[bot]