我正在尝试从图像创建视频,但该视频未在 google colab 中显示。但是同一个视频,当我下载并在本地计算机上播放时,它工作正常。我提供了重现上述内容的代码,
import numpy as np
import cv2
from IPython.display import Video, display
def create_random_image(width=256, height=256):
return np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
def modify_image_randomly(image):
# Select random points and change pixel values
for _ in range(100): # Modify 100 random pixels
x = np.random.randint(0, image.shape[1])
y = np.random.randint(0, image.shape[0])
image[y, x] = [np.random.randint(0, 255) for _ in range(3)]
return image
initial_image = create_random_image()
frame_width = initial_image.shape[1]
frame_height = initial_image.shape[0]
fps = 10
num_frames = 50 # Number of frames in the video
# Create a VideoWriter object to save as MP4
out = cv2.VideoWriter('random_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
# Generate frames and write to video
for i in range(num_frames):
modified_image = modify_image_randomly(initial_image.copy()) # Modify a copy of the initial image
out.write(modified_image)
# Release the video writer
out.release()
# Display the video in Colab
display(Video("random_video.mp4", embed=True))
我完全按照原样运行了您的代码,没有在 colab 中进行任何更改。它可以很好地显示随机像素视频,没有任何错误。我建议您检查colab中是否使用了所有软件包的更新版本或检查本地计算机中所有软件包的版本并在您的Google colab笔记本中进行相应匹配。