|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import base64 |
|
import logging |
|
import os |
|
from io import BytesIO |
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Tuple, Union |
|
|
|
import PIL |
|
import numpy as np |
|
import requests |
|
from packaging import version |
|
|
|
|
|
def _is_numpy(x): |
|
return isinstance(x, np.ndarray) |
|
|
|
|
|
def is_numpy_array(x): |
|
""" |
|
Tests if `x` is a numpy array or not. |
|
""" |
|
return _is_numpy(x) |
|
|
|
|
|
def is_pil_image(img): |
|
return isinstance(img, PIL.Image.Image) |
|
|
|
|
|
def is_valid_image(img): |
|
return is_pil_image(img) or is_numpy_array(img) |
|
|
|
|
|
def valid_images(imgs): |
|
|
|
if isinstance(imgs, (list, tuple)): |
|
for img in imgs: |
|
if not valid_images(img): |
|
return False |
|
|
|
elif not is_valid_image(imgs): |
|
return False |
|
return True |
|
|
|
|
|
def is_batched(img): |
|
if isinstance(img, (list, tuple)): |
|
return is_valid_image(img[0]) |
|
return False |
|
|
|
|
|
def is_scaled_image(image: np.ndarray) -> bool: |
|
""" |
|
Checks to see whether the pixel values have already been rescaled to [0, 1]. |
|
""" |
|
if image.dtype == np.uint8: |
|
return False |
|
|
|
|
|
return np.min(image) >= 0 and np.max(image) <= 1 |
|
|
|
|
|
def make_batched_images(images): |
|
""" |
|
Accepts images in list or nested list format, and makes a list of images for preprocessing. |
|
|
|
Args: |
|
images (`Union[List[List[ImageInput]], List[ImageInput], ImageInput]`): |
|
The input image. |
|
|
|
Returns: |
|
list: A list of images. |
|
""" |
|
if ( |
|
isinstance(images, (list, tuple)) |
|
and isinstance(images[0], (list, tuple)) |
|
and is_valid_image(images[0][0]) |
|
): |
|
return [img for img_list in images for img in img_list] |
|
|
|
elif isinstance(images, (list, tuple)) and is_valid_image(images[0]): |
|
return images |
|
|
|
elif is_valid_image(images): |
|
return [images] |
|
|
|
raise ValueError(f"Could not make batched video from {images}") |
|
|