Numpy array to tensor. Args: imgs (list[ndarray] | ndarray): Input images. bgr2rgb (bool): Whether to change bgr to rgb. float32 (bool): Whether to change to float32. Returns: list[tensor] | tensor: Tensor images. If returned results only have on
(imgs, bgr2rgb=True, float32=True)
| 82 | |
| 83 | # from basicsr |
| 84 | def img2tensor(imgs, bgr2rgb=True, float32=True): |
| 85 | """Numpy array to tensor. |
| 86 | |
| 87 | Args: |
| 88 | imgs (list[ndarray] | ndarray): Input images. |
| 89 | bgr2rgb (bool): Whether to change bgr to rgb. |
| 90 | float32 (bool): Whether to change to float32. |
| 91 | |
| 92 | Returns: |
| 93 | list[tensor] | tensor: Tensor images. If returned results only have |
| 94 | one element, just return tensor. |
| 95 | """ |
| 96 | |
| 97 | def _totensor(img, bgr2rgb, float32): |
| 98 | if img.shape[2] == 3 and bgr2rgb: |
| 99 | if img.dtype == 'float64': |
| 100 | img = img.astype('float32') |
| 101 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 102 | img = torch.from_numpy(img.transpose(2, 0, 1)) |
| 103 | if float32: |
| 104 | img = img.float() |
| 105 | return img |
| 106 | |
| 107 | if isinstance(imgs, list): |
| 108 | return [_totensor(img, bgr2rgb, float32) for img in imgs] |
| 109 | return _totensor(imgs, bgr2rgb, float32) |
| 110 | |
| 111 | |
| 112 | def tensor2img(tensor, rgb2bgr=True, out_type=np.uint8, min_max=(0, 1)): |
no test coverage detected