from torch.utils.tensorboard import SummaryWriter import cv2 writer = SummaryWriter() # Read video file cap = cv2.VideoCapture("example_video.mp4") # Loop through frames and add to TensorBoard while cap.isOpened(): ret, frame = cap.read() if not ret: # End of video file break writer.add_video("example_video", frame, fps=30) # Release video capture and close writer cap.release() writer.close()
from torch.utils.tensorboard import SummaryWriter import numpy as np writer = SummaryWriter() # Generate random video frames video = np.random.rand(10, 3, 64, 64) * 255 # Add video to TensorBoard writer.add_video("random_video", video, fps=5) # Close writer writer.close()In this example, we create a `SummaryWriter` object as before, but instead of reading from a file, we generate random video frames using NumPy. We then add the video frames to the TensorBoard `SummaryWriter` using the `add_video` method. Finally, we close the writer.