labelme json 增强
时间: 2025-01-03 11:36:04 浏览: 9
### 对LabelMe生成的JSON文件进行数据增强
为了对由LabelMe标注工具生成的JSON文件实施有效的数据增强操作,可以采用多种方法和技术来扩展训练集的质量和数量。以下是几种常见的策略:
#### 使用图像变换库配合自定义脚本
一种常见的方式是利用Python中的`imgaug`或`albumentations`这样的图像增广库,在读取原始图片及其对应的标签信息之后,同步地对二者应用相同的几何变换或其他类型的扰动[^1]。
对于具体的实现过程而言,当加载了一个通过LabelMe创建好的JSON配置文档后,解析其中存储的对象边界框坐标以及类别名称等元数据;接着按照预设的概率分布随机选取若干种可能的变化模式——比如平移、缩放、旋转甚至是颜色抖动等等作用于关联的一组像素阵列之上;最后再把这些经过调整后的特征重新编码回符合预期格式的新版JSON结构里保存起来供后续调用[^2]。
```python
import json
from imgaug import augmenters as iaa
import cv2
import os
import numpy as np
def load_labelme_json(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return data
def save_augmented_data(image, shapes, output_image_path, output_json_path):
augmented_data = {
"version": "4.5.6",
"flags": {},
"shapes": shapes,
"imagePath": os.path.basename(output_image_path),
"imageData": None,
"imageHeight": image.shape[0],
"imageWidth": image.shape[1]
}
with open(output_json_path, 'w', encoding='utf-8') as f:
json.dump(augmented_data, f, ensure_ascii=False)
seq = iaa.Sequential([
iaa.Fliplr(0.5), # horizontal flips
iaa.Crop(percent=(0, 0.1)), # random crops
iaa.Sometimes(
0.5,
iaa.GaussianBlur(sigma=(0, 0.5))
),
iaa.LinearContrast((0.75, 1.5)),
], random_order=True)
input_dir = './data'
output_dir = './augmented'
for filename in os.listdir(input_dir):
if not filename.endswith('.json'):
continue
file_path = os.path.join(input_dir, filename)
labelme_data = load_labelme_json(file_path)
image_file_name = labelme_data['imagePath']
image = cv2.imread(os.path.join(input_dir, image_file_name))
points_list = []
for shape in labelme_data["shapes"]:
polygon_points = [(point_x, point_y) for (point_x, point_y) in shape["points"]]
points_list.append(np.array(polygon_points).reshape((-1, 2)))
seq_det = seq.to_deterministic()
aug_img = seq_det.augment_images([image])[0]
keypoints_on_image = [ia.KeypointsOnImage.from_xy_array(points.reshape(-1, 2), shape=image.shape[:2]) for points in points_list]
aug_keypoints = seq_det.augment_keypoints(keypoints_on_image)
new_shapes = []
for idx, kpsoi in enumerate(aug_keypoints):
updated_shape = dict(labelme_data["shapes"][idx])
updated_shape["points"] = [[int(point.x), int(point.y)] for point in kpsoi.keypoints]
new_shapes.append(updated_shape)
out_filename_base = os.path.splitext(filename)[0] + "_aug"
out_image_path = os.path.join(output_dir, "{}.jpg".format(out_filename_base))
out_json_path = os.path.join(output_dir, "{}.json".format(out_filename_base))
cv2.imwrite(out_image_path, aug_img)
save_augmented_data(aug_img, new_shapes, out_image_path, out_json_path)
```
这段代码展示了如何使用`imgaug`来进行基本的数据增强,并保持原有的LabelMe JSON格式不变。它会遍历指定目录下的所有`.json`文件,执行一系列预定的操作并将结果存入另一个位置[^3]。
阅读全文