11#!/usr/bin/env python3
22import os
33import subprocess
4- import sys
4+
5+ import numpy as np
6+
7+ import torch
8+ from PIL import Image
59
610# Copyright (c) Meta Platforms, Inc. and affiliates.
711# All rights reserved.
1317# from source media already checked into the repo.
1418
1519
20+ def convert_image_to_tensor (image_path ):
21+ if not os .path .exists (image_path ):
22+ return
23+ # Get base filename without extension
24+ base_filename = os .path .splitext (image_path )[0 ]
25+ pil_image = Image .open (image_path )
26+ img_tensor = torch .from_numpy (np .asarray (pil_image ))
27+ print (img_tensor .shape )
28+ print (img_tensor .dtype )
29+ # Save tensor to disk
30+ torch .save (img_tensor , base_filename + ".pt" , _use_new_zipfile_serialization = True )
31+ os .remove (image_path )
32+
33+
1634def main ():
1735 SCRIPT_DIR = os .path .dirname (os .path .abspath (__file__ ))
18- TORCHCODEC_PATH = os .path .join (SCRIPT_DIR , ".." )
36+ TORCHCODEC_PATH = os .path .dirname (SCRIPT_DIR )
1937 RESOURCES_DIR = os .path .join (TORCHCODEC_PATH , "test" , "resources" )
2038 VIDEO_PATH = os .path .join (RESOURCES_DIR , "nasa_13013.mp4" )
2139
@@ -31,6 +49,7 @@ def main():
3149 # frames would result in 1-based index naming. We enforce 0-based index naming
3250 # so that the name of reference frames matches the index when accessing that
3351 # frame in the Python decoder.
52+ output_bmp = f"{ VIDEO_PATH } .stream{ stream } .frame{ frame :06d} .bmp"
3453 frame_name = f"{ frame :06d} "
3554 cmd = [
3655 "ffmpeg" ,
@@ -45,14 +64,16 @@ def main():
4564 "vfr" ,
4665 "-q:v" ,
4766 "2" ,
48- f" { VIDEO_PATH } .stream { stream } .frame { frame_name } .bmp" ,
67+ output_bmp ,
4968 ]
5069 subprocess .run (cmd , check = True )
70+ convert_image_to_tensor (output_bmp )
5171
5272 # Extract individual frames at specific timestamps, including the last frame of the video.
5373 seek_timestamp = [6.0 , 6.1 , 10.0 , 12.979633 ]
5474 timestamp_name = [f"{ seek_timestamp :06f} " for seek_timestamp in seek_timestamp ]
5575 for timestamp , name in zip (seek_timestamp , timestamp_name ):
76+ output_bmp = f"{ VIDEO_PATH } .time{ name } .bmp"
5677 cmd = [
5778 "ffmpeg" ,
5879 "-y" ,
@@ -65,6 +86,7 @@ def main():
6586 f"{ VIDEO_PATH } .time{ name } .bmp" ,
6687 ]
6788 subprocess .run (cmd , check = True )
89+ convert_image_to_tensor (output_bmp )
6890
6991 # This video was generated by running the following:
7092 # conda install -c conda-forge x265
@@ -75,6 +97,7 @@ def main():
7597 FRAMES = [5 ]
7698 for frame in FRAMES :
7799 frame_name = f"{ frame :06d} "
100+ output_bmp = f"{ VIDEO_PATH } .stream0.frame{ frame_name } .bmp"
78101 cmd = [
79102 "ffmpeg" ,
80103 "-y" ,
@@ -86,9 +109,10 @@ def main():
86109 "vfr" ,
87110 "-q:v" ,
88111 "2" ,
89- f" { VIDEO_PATH } .stream0.frame { frame_name } .bmp" ,
112+ output_bmp ,
90113 ]
91114 subprocess .run (cmd , check = True )
115+ convert_image_to_tensor (output_bmp )
92116
93117 # This video was generated by running the following:
94118 # ffmpeg -f lavfi -i testsrc=duration=5:size=640x360:rate=25,format=yuv420p -c:v libaom-av1 -crf 30 -colorspace bt709 -color_primaries bt709 -color_trc bt709 av1_video.mkv
@@ -98,6 +122,7 @@ def main():
98122
99123 for frame in FRAMES :
100124 frame_name = f"{ frame :06d} "
125+ output_bmp = f"{ VIDEO_PATH } .stream0.frame{ frame_name } .bmp"
101126 cmd = [
102127 "ffmpeg" ,
103128 "-y" ,
@@ -109,20 +134,10 @@ def main():
109134 "vfr" ,
110135 "-q:v" ,
111136 "2" ,
112- f" { VIDEO_PATH } .stream0.frame { frame_name } .bmp" ,
137+ output_bmp ,
113138 ]
114139 subprocess .run (cmd , check = True )
115-
116- for bmp in [f for f in os .listdir (RESOURCES_DIR ) if f .endswith (".bmp" )]:
117- bmp_path = os .path .join (RESOURCES_DIR , bmp )
118- subprocess .run (
119- [
120- sys .executable ,
121- os .path .join (TORCHCODEC_PATH , "test" , "convert_image_to_tensor.py" ),
122- bmp_path ,
123- ]
124- )
125- os .remove (bmp_path )
140+ convert_image_to_tensor (output_bmp )
126141
127142
128143if __name__ == "__main__" :
0 commit comments