MCPcopy Create free account
hub / github.com/apple/ml-streambridge / frame_transform

Function frame_transform

streambridge/utils.py:209–246  ·  view source on GitHub ↗
(
        image_size: Union[int, Tuple[int, int]],
        rescale_factor: float = 1.0,
        mean: Optional[Tuple[float, ...]] = None,
        std: Optional[Tuple[float, ...]] = None,
        random_flip = False,
)

Source from the content-addressed store, hash-verified

207 return Compose(transforms)
208
209def frame_transform(
210 image_size: Union[int, Tuple[int, int]],
211 rescale_factor: float = 1.0,
212 mean: Optional[Tuple[float, ...]] = None,
213 std: Optional[Tuple[float, ...]] = None,
214 random_flip = False,
215):
216 mean = mean or OPENAI_DATASET_MEAN
217 if not isinstance(mean, (list, tuple)):
218 mean = (mean,) * 3
219
220 std = std or OPENAI_DATASET_STD
221 if not isinstance(std, (list, tuple)):
222 std = (std,) * 3
223
224 if isinstance(image_size, int):
225 resize_size = (image_size, image_size)
226 crop_size = (image_size, image_size)
227 elif isinstance(image_size, (list, tuple)) and len(image_size) == 2:
228 resize_size = (image_size[0], image_size[1])
229 crop_size = (image_size[0], image_size[1])
230 else:
231 raise ValueError("image_size must be an int or a tuple of two ints.")
232
233 normalize = Normalize(mean=mean, std=std)
234
235 transforms = [
236 ToPILImage(),
237 Resize(resize_size, interpolation=InterpolationMode.BICUBIC),
238 CenterCrop(resize_size),
239 RandomHorizontalFlip() if random_flip else Lambda(lambda x: x),
240 ]
241 transforms.extend([
242 _convert_to_rgb,
243 ToTensor(),
244 normalize,
245 ])
246 return Compose(transforms)
247
248def expand2square(pil_img, background_color=tuple(int(x*255) for x in OPENAI_DATASET_MEAN)):
249 width, height = pil_img.size

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected