对labelme生成的json文件及图片同时进行resize

在目标检测中,有时需要对数据集进行resize处理,该python脚本可实现对分割数据集图片及标注文件的resize

import cv2
import os
import numpy as np
import base64
import json
import shutil
in_dir = '' #标注数据集地址
output_dir = " " #输出地址
img_list = [f for f in os.listdir(in_dir) if f.endswith(".jpg")]
if not os.path.exists(output_dir):
    os.makedirs(output_dir, exist_ok=True)
for img_name in img_list:
    json_name = img_name.replace(".jpg", ".json")
    img = cv2.imread(os.path.join(in_dir, img_name))
    h, w = img.shape[:2]
    # resize尺寸根据自己需求调整
    if h > w:
        re_size = (512, 1024)
    elif w > h:
        re_size = (1024, 512)
    else:
        re_size = (512, 512)
    img_re = cv2.resize(img, re_size)
    img_path = os.path.join(output_dir, img_name)
    cv2.imwrite(img_path, img_re)
    # resize json
    re_x = re_size[0] / w
    re_y = re_size[1] / h
    with open(os.path.join(in_dir, json_name), "r", encoding="utf-8") as f:
        data = json.load(f)
    shapes = data["shapes"]
    for shape in shapes:
        points = shape["points"]
        for i, pt in enumerate(points):
            pt = [pt[0] * re_x, pt[1] * re_y]
            points[i] = pt
        shape["points"] = points
    data["shapes"] = shapes
    data["imagePath"] = img_name
    with open(img_path, "rb") as h:
        data["imageData"] = base64.b64encode(h.read()).decode('utf-8')
    img = cv2.imread(img_path)
    data["imageHeight"], data["imageWidth"] = re_size[1], re_size[0]
     # save the new json file
    json_out = os.path.join(output_dir, json_name)
    with open(json_out, "w", encoding="UTF-8") as f1:
        json.dump(data, f1, ensure_ascii=False, indent=2)
    print(f"json_path edited!:{json_out}")  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用labelme的官方工具labelme2voc将json文件转化为VOC格式的标注文件,然后使用VOC格式的标注文件生成灰度的png图像。 具体步骤如下: 1. 安装labelme2voc 可以使用pip安装: ``` pip install labelme2voc ``` 2. 将json文件转化为VOC格式的标注文件 使用labelme2voc命令将json文件转化为VOC格式的标注文件: ``` labelme2voc input_json_dir output_voc_dir ``` 其中,input_json_dir是存放json文件的目录路径,output_voc_dir是输出VOC格式标注文件的目录路径。 例如,有一个json文件存放在/home/user/data/json目录下,要将其转化为VOC格式标注文件并存放在/home/user/data/voc目录下,可以执行以下命令: ``` labelme2voc /home/user/data/json /home/user/data/voc ``` 执行完毕后,会在输出目录下生成json文件对应的VOC格式标注文件。 3. 将VOC格式标注文件生成灰度的png图像 可以使用VOC格式标注文件生成灰度的png图像的工具有很多,例如opencv、PIL等。 以opencv为例,可以使用以下代码将VOC格式标注文件生成灰度的png图像: ``` import cv2 import os input_voc_dir = "/home/user/data/voc" output_gray_dir = "/home/user/data/gray" if not os.path.exists(output_gray_dir): os.makedirs(output_gray_dir) for filename in os.listdir(input_voc_dir): if filename.endswith(".xml"): xml_file = os.path.join(input_voc_dir, filename) img_file = xml_file.replace(".xml", ".jpg") img = cv2.imread(img_file) h, w = img.shape[:2] gray = np.zeros((h, w), np.uint8) tree = ET.parse(xml_file) for obj in tree.findall("object"): name = obj.find("name").text bndbox = obj.find("bndbox") xmin = int(bndbox.find("xmin").text) ymin = int(bndbox.find("ymin").text) xmax = int(bndbox.find("xmax").text) ymax = int(bndbox.find("ymax").text) gray[ymin:ymax, xmin:xmax] = 255 if name == "target" else 128 gray_file = os.path.join(output_gray_dir, filename.replace(".xml", ".png")) cv2.imwrite(gray_file, gray) ``` 其中,input_voc_dir是存放VOC格式标注文件的目录路径,output_gray_dir是输出灰度png图像的目录路径。 该代码会遍历目录下所有的xml文件(即VOC格式标注文件),根据标注信息生成相应的灰度的png图像,并存放在输出目录下。其中,目标区域的像素值为255,非目标区域的像素值为128。 执行完毕后,会在输出目录下生成与VOC格式标注文件对应的灰度png图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值