对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}")  

猜你喜欢

转载自blog.csdn.net/qq_41980080/article/details/132557343