labelme数据增强,原数据集图片使用方框标注(非圆点),纯python代码实现。

该代码示例展示了如何通过Python读取JSON标注文件,对图像进行随机裁剪并调整大小以实现数据集的增强。每个图像被扩充为五张新图像,同时更新形状的坐标以匹配裁剪后的图像。最后,新的JSON文件被保存,包含裁剪和增强后的图像数据。
摘要由CSDN通过智能技术生成

先看结果吧

然后进入正题,展示一下数据集格式

再展示一下修改后的数据集格式(每一张图片扩充了五张)

这是数据增强的代码,记得按自己文件的路径和命名进行少量修改

import cv2
import json
import numpy as np
import base64
import io
from PIL import Image
import random

# 读取JSON标注文件
def data_build():
    #共有45张图片
    for i in range(1, 46):
        #每张图片随机裁剪五下
        for j in range(1, 6):
            json_file_path = f'dataset/{str(i).zfill(2)}.json'
            # 读取JSON文件
            with open(json_file_path, 'r') as f:
                data = json.load(f)

            image_data = data['imageData']
            image_data = base64.b64decode(image_data)
            # 创建新的JSON数据对象
            new_data = {
                "version": data["version"],
                "flags": data["flags"],
                "shapes": [],
                "imagePath": data["imagePath"],
                "imageData": [],
                "imageHeight": 416,
                "imageWidth": 416
            }


            # 将图像数据转换为NumPy数组
            image_array = np.frombuffer(image_data, dtype=np.uint8)

            # 解码图像数组为OpenCV图像
            image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)

            # 获取原始图像尺寸
            height, width, _ = image.shape

            # 设置目标裁剪尺寸
            target_size = 416

            # 随机选择裁剪位置
            x = random.randint(0, width - target_size)
            y = random.randint(0, height - target_size)
            print('x的范围为',x,'--',x+416)
            print('y的范围为',y,'--',y+416)

            # 裁剪图像
            cropped_image = image[y:y+target_size, x:x+target_size]

            # 调整裁剪后的图像大小为416x416
            resized_image = cv2.resize(cropped_image, (target_size, target_size))

            # 将图像转换为Base64编码
            _, buffer = cv2.imencode('.jpg', resized_image)
            base64_cropped_image = base64.b64encode(buffer).decode()

            new_data['imageData'] = base64_cropped_image


            # 遍历每个形状
            for shape in data['shapes']:
                points = shape['points']
                print("points:",points)
                # 检查每个点的坐标

                x1, y1 = points[0]
                x2, y2 = points[1]
                x_ = (x1+x2)/2
                y_ = (y1+y2)/2

                if x_>x and x_<x+416 and y_>y and y_<y+416:
                    print("找到了一个点,x为:",x1,',y为',y1)
                    x1 = x1-x
                    x2 = x2-x
                    y1 = y1-y
                    y2 = y2-y
                    points = [[x1,y1],[x2,y2]]
                    shape['points'] = points
                    new_data['shapes'].append(shape)



            # 将新的JSON数据保存到新的文件中
            with open(f'{i}_{j}.json', 'w') as f:
                json.dump(new_data, f, indent=4)

data_build()

 裁剪完毕,读取看看

import json
import cv2
import base64
import numpy as np

# 读取JSON文件
with open('dataset_build/26_3.json', 'r') as f:
    data = json.load(f)

# 读取图像路径和图像数据
image_path = data["imagePath"]
image_data = data["imageData"]

# 解码图像数据
image_data = base64.b64decode(image_data)

# 将图像数据转换为NumPy数组
image_array = np.frombuffer(image_data, dtype=np.uint8)

# 解码图像数组为OpenCV图像
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)

# 遍历每个形状
for shape in data['shapes']:
    label = shape['label']
    points = shape['points']

    # 绘制方框
    x1, y1 = points[0]
    x2, y2 = points[1]
    cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)

# 显示图像
cv2.imshow('Image with Annotations', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

 结果:

Python中,将LabelMe标注的数据转换为YOLO所需的格式,通常需要几个步骤。LabelMe是一个图像标注工具,而YOLO是一种目标检测算法,它们的数据结构有所不同。这里提供一个简化版的示例,假设你已经有一个包含LabelMe JSON文件的目录: ```python import os import json from PIL import Image from pascal_voc_writer import Writer def labelme_to_yolo(labelme_json_dir, output_yolo_dir): # 创建YOLO所需的文件结构 if not os.path.exists(output_yolo_dir): os.makedirs(output_yolo_dir) for filename in os.listdir(labelme_json_dir): if filename.endswith(".json"): with open(os.path.join(labelme_json_dir, filename), "r") as f: data = json.load(f) img_path = os.path.join(labelme_json_dir, data["imagePath"]) im = Image.open(img_path) width, height = im.size annotations = [] for shape in data["shapes"]: label = shape["label"] points = [(x, y) for x, y, w, h in shape["points"]] x_min, y_min = min(points, key=lambda xy: xy[0])[0], min(points, key=lambda xy: xy[1])[1] x_max, y_max = max(points, key=lambda xy: xy[0])[0] + w, max(points, key=lambda xy: xy[1])[1] + h box_w, box_h = x_max - x_min, y_max - y_min object_name = f"{label}_box" annotations.append({ "class": object_name, "xmin": x_min, "ymin": y_min, "xmax": x_max, "ymax": y_max, "obj": 1 }) # 写入YOLO所需的txt文件 txt_file = os.path.join(output_yolo_dir, filename[:-5] + ".txt") with open(txt_file, "w") as txt_f: Writer(txt_file).write(annotations, names=[f"{i}_box" for i in range(len(annotations))], image_size=(width, height)) # 使用函数转换数据 labelme_to_yolo("path/to/labelme/json", "path/to/output/yolo") ``` 这个脚本读取每个LabelMe JSON,提取出边界框信息并转换为YOLO所需的格式(每行一个对象,包含类别名、坐标)。注意实际使用时可能需要根据LabelMe JSON的具体结构进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值