Hi , I have a network that extract a tensor with shape:(batch,channels,time,x,y)=>(batch, 3,16,56,56), and I want to save it as a video, so that the output will be a video.
I tried the naive way using:
You can guess what happened… I’ll be happy if someone has an Idea, really need your help!
I haven’t done it myself but openCV has something called a VideoWriter. That should do the trick. There is also the ffmpeg module outside of Python.
The way I have done it in the past saving all the images in a folder and via a bash script and ffmpeg make a movie out of them. Not as clean, but yeah.
Here is a pyimagesearch tutorial of something similar
Edit: Well you’d have to convert your tensor to images beforehand. If you need help with that let us know
I’ll try the opencv option tomorrow (It’s night time at my place).
And yes I’ll be happy with some direction about turning tensor to images.
I wrote this example that should be able to convert tensors to images. It might not work 100% for you but it should help
from torchvision import transforms
tensor_2_image = transforms.ToPILImage() # Converts to Image
output = torch.randn(2, 3, 16, 56, 56) # Tensor
for batch in output:
batch = batch.permute(1, 0, 2, 3) # Change axises -> 16,3,56,56
images = [tensor_2_image(im) for im in batch] # Convert images
for image in images:
# Do what you want with your image here
Edit: Perhaps OpenCV wants a numpy array when I think about it. You can convert the image to a numpy array with
np_image = np.array(image)
Hi, I think imageio library (which is very well optimized, clean API) can write video just by giving a list of frames format and frame rate
Thanks , I will try both of them.
Have you tried or heard trying using pyffmpeg?
Cool, no but that sounds like a great option
I was looking for it now and found that torchvision allows you to write a tensor as a video. I suppose it wasn’t available a year ago, otherwise it would have been mentioned here for sure. Here’s the link https://pytorch.org/docs/stable/torchvision/io.html#torchvision.io.write_video