fvcore/transforms/transform.py
detectron2/data/transforms/transform.py + PIL
提供底層實現。
__all__ = [
"RandomBrightness",
"RandomContrast",
"RandomCrop",
"RandomExtent",
"RandomFlip",
"RandomSaturation",
"RandomLighting",
"Resize",
"ResizeShortestEdge",
"TransformGen",
"apply_transform_gens",
]
from detectron2.config import get_cfg
from detectron2.data import detection_utils
from detectron2.data import transforms as T
from PIL import Image
# ****************************
# Pillow 基本圖像轉換用法
>>> img = Image.open("image.jpg")
>>> img.show()
>>> img.size
(500, 363)
# 截取圖像data座標之間的區域,並輸出爲size尺寸
>>> ret = img.transform(size=(50,50), method=Image.EXTENT, data=(0,0,200,200), fill=0)
>>> ret.size
(50, 50)
# ****************************
# ****************************
cfg = get_cfg()
is_train = True
build_transform_gen
# build_transform_gen 會預先指定需要進行的變換,加入的是T裏面的變換
transform_gen = detection_utils.build_transform_gen(cfg, is_train)
image = np.random.rand(200, 300)
# apply_transform_gens 會根據需要的變換,調用get_transform獲取相應轉換類,apply_image進行變換;同時轉換也要傳給bbox使用
image, transforms = T.apply_transform_gens(transform_gen, image)
image_shape = image.shape[:2] # h, w
assert image_shape == (800, 1200)
annotation = {"bbox": [179, 97, 62, 40, -56]}
# boxes.shape = (1, 5) x_center, y_center, width, height, angle
boxes = np.array([annotation["bbox"]], dtype=np.float64)
# 轉換也要傳給bbox使用
transformed_bbox = transforms.apply_rotated_box(boxes)[0]
# ****************************