Skip to content

Conversation

joaoascenso02
Copy link

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.

In frames_from_video_file, cv2.VideoCapture().get(cv2.CAP_PROP_FRAME_COUNT)
returns a float. This causes random.randint to fail with:

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

Fix: cast (video_length - need_length) to int before passing to randint.
@joaoascenso02 joaoascenso02 requested a review from a team as a code owner September 23, 2025 14:32
Copy link

google-cla bot commented Sep 23, 2025

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.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant