@wanghuijiao
2021-01-28T14:18:27.000000Z
字数 3118
阅读 2869
实验报告
pip install imgaug
# 出现Shapely错误时,用这个
pip install imgaug — upgrade — no-deps
如何对图像+bbox一起做变化?代码实现?暂不做。
决定采用:
# pip 安装
pip install albumentations
# conda 安装,需先安装imgaug
conda install -c conda-forge imgaug
conda install albumentations -c albumentations
# 1-导入库
import albumentations as A
import cv2
# 2-定义增强Pipeline
# format='pascal_voc','albumantations','coco','yolo';
transform = A.Compose([
A.RandomCrop(width=450, height=450),
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.2),
],bbox_param=A.BboxParams(format='yolo'))
# 3-读图片和Bbox
image = cv2.imread("/path/to/image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
bboxes = [
[23, 74, 295, 388, 'dog', 'animal'],
[377, 294, 252, 161, 'cat', 'animal'],
[333, 421, 49, 49, 'sports ball', 'item'],
]
# 4-获取增强结果
transformed = transform(image=image, bboxes=bboxes)
transformed_image = transformed['image']
transformed_bboxes = transformed['bboxes']
# 展示一下高阶用法
# 2-
# min_area, 当增强后bbox面积小于min_area,该bbox会被丢弃,返回的bbox增强列表不包含该bbox;
# min_visibility,(0-1),当增强后bbox面积占增强前bbox面积比例小于该值,那么该bbox同样被丢弃。
transform = A.Compose([
A.RandomCrop(width=450, height=450),
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.2),
], bbox_params=A.BboxParams(format='coco', label_fields=['class_labels', 'class_categories']))
# 3-
image = cv2.imread("/path/to/image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
bboxes = [
[23, 74, 295, 388],
[377, 294, 252, 161],
[333, 421, 49, 49],
]
class_labels = ['cat', 'dog', 'parrot']
class_categories = ['animal', 'animal', 'item']
# 4-
transformed = transform(image=image, bboxes=bboxes, class_labels=class_labels, class_categories=class_categories)
transformed_image = transformed['image']
transformed_bboxes = transformed['bboxes']
transformed_class_labels = transformed['class_labels']
transformed_class_categories = transformed['class_categories']
[x_min, y_min, x_max, y_max]
[x_min/i_w, y_min/i_h, x_max/i_w, y_max/i_h]
[x_min, y_min, width, height]
[x_center/i_w, y_center/i_h, width/i_w, height/i_h]