MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / to_np

Method to_np

diffusers/tests/others/test_video_processor.py:76–129  ·  view source on GitHub ↗
(self, video)

Source from the content-addressed store, hash-verified

74 return sample
75
76 def to_np(self, video):
77 # List of images.
78 if isinstance(video[0], PIL.Image.Image):
79 video = np.stack([np.array(i) for i in video], axis=0)
80
81 # List of list of images.
82 elif isinstance(video, list) and isinstance(video[0][0], PIL.Image.Image):
83 frames = []
84 for vid in video:
85 all_current_frames = np.stack([np.array(i) for i in vid], axis=0)
86 frames.append(all_current_frames)
87 video = np.stack([np.array(frame) for frame in frames], axis=0)
88
89 # List of 4d/5d {ndarrays, torch tensors}.
90 elif isinstance(video, list) and isinstance(video[0], (torch.Tensor, np.ndarray)):
91 if isinstance(video[0], np.ndarray):
92 video = np.stack(video, axis=0) if video[0].ndim == 4 else np.concatenate(video, axis=0)
93 else:
94 if video[0].ndim == 4:
95 video = np.stack([i.cpu().numpy().transpose(0, 2, 3, 1) for i in video], axis=0)
96 elif video[0].ndim == 5:
97 video = np.concatenate([i.cpu().numpy().transpose(0, 1, 3, 4, 2) for i in video], axis=0)
98
99 # List of list of 4d/5d {ndarrays, torch tensors}.
100 elif (
101 isinstance(video, list)
102 and isinstance(video[0], list)
103 and isinstance(video[0][0], (torch.Tensor, np.ndarray))
104 ):
105 all_frames = []
106 for list_of_videos in video:
107 temp_frames = []
108 for vid in list_of_videos:
109 if vid.ndim == 4:
110 current_vid_frames = np.stack(
111 [i if isinstance(i, np.ndarray) else i.cpu().numpy().transpose(1, 2, 0) for i in vid],
112 axis=0,
113 )
114 elif vid.ndim == 5:
115 current_vid_frames = np.concatenate(
116 [i if isinstance(i, np.ndarray) else i.cpu().numpy().transpose(0, 2, 3, 1) for i in vid],
117 axis=0,
118 )
119 temp_frames.append(current_vid_frames)
120 temp_frames = np.stack(temp_frames, axis=0)
121 all_frames.append(temp_frames)
122
123 video = np.concatenate(all_frames, axis=0)
124
125 # Just 5d {ndarrays, torch tensors}.
126 elif isinstance(video, (torch.Tensor, np.ndarray)) and video.ndim == 5:
127 video = video if isinstance(video, np.ndarray) else video.cpu().numpy().transpose(0, 1, 3, 4, 2)
128
129 return video
130
131 @parameterized.expand(["list_images", "list_list_images"])
132 def test_video_processor_pil(self, input_type):

Callers 3

Calls 1

transposeMethod · 0.80

Tested by

no test coverage detected