File size: 3,247 Bytes
e8f2571 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
from typing import Optional, Tuple, Union
import matplotlib.pyplot as plt
import tifffile
import torch
import numpy as np
import mmcv
from mmcv.transforms import to_tensor
from mmcv.transforms.base import BaseTransform
from mmengine.structures import InstanceData, PixelData
from mmdet.registry import TRANSFORMS
from mmdet.structures import DetDataSample
from mmdet.structures.bbox import BaseBoxes
import mmengine.fileio as fileio
def irflyfromfile(img_path, img_num=1,repeat =1) -> np.ndarray:
"""Read an image from bytes.
Args:
backend (str | None): The image decoding backend type.
Returns:
ndarray: Loaded image array.
Examples:
"""
with tifffile.TiffFile(img_path) as tif:
img = tif.asarray()
img = np.expand_dims(img, axis=-1)
img_name = img_path.split('/')[-1]
try:
img_id = int(img_name.split('.')[0])
except:
img_id = 1
for i in range(1, img_num):
img_id_i = img_id - 1
img_name_i = str(img_id_i).zfill(6)+'.tiff'
img_path_i = img_path.replace(img_name, img_name_i)
with tifffile.TiffFile(img_path_i) as tif:
img_i = tif.asarray()
img_i = np.expand_dims(img_i, axis=-1)
img = np.concatenate((img_i, img), axis=-1)
img = np.repeat(img,repeat,axis=-1)
return img
@TRANSFORMS.register_module()
class LoadIRFlyImageFromFiles(BaseTransform):
"""Load multi-channel images from a list of separate channel files.
Required Keys:
- img_path
Modified Keys:
- img
- img_shape
- ori_shape
Args:
to_float32 (bool): Whether to convert the loaded image to a float32
numpy array. If set to False, the loaded image is an uint8 array.
Defaults to False.
"""
def __init__(
self,
to_float32: bool = False,
image_num: int = 1,
repeat: int =1,
normalized_basis = None,
) -> None:
self.to_float32 = to_float32
self.image_num = image_num
self.repeat = repeat
self.normalized_basis = normalized_basis
def transform(self, results: dict) -> dict:
"""Transform functions to load multiple images and get images meta
information.
Args:
results (dict): Result dict from :obj:`mmdet.CustomDataset`.
Returns:
dict: The dict contains loaded images and meta information.
"""
img = irflyfromfile(results['img_path'],img_num = self.image_num, repeat=self.repeat)
# up_limit = 3500
# low_limit = 600
# new_img = (img - low_limit) / up_limit
# new_img[new_img > 1] = 1
# new_img[new_img < 0] = 0
# img = new_img * 255
if self.normalized_basis == None:
img = img/1500
else:
img = img/np.array(self.normalized_basis)
if self.to_float32:
img = img.astype(np.float32)
results['img'] = img
results['img_shape'] = img.shape[:2]
results['ori_shape'] = img.shape[:2]
return results
def __repr__(self):
repr_str = (f'{self.__class__.__name__}('
f'to_float32={self.to_float32}, ')
return repr_str
|