通过labelme的json文件实现对图片的批量裁剪

通过labelme的json文件实现对图片的批量裁剪

工具安装

首先我们需要在conda的终端下载labelme,然后打开labelme对图片进行抠图,将一张图需要抠的指定元素打好标签。具体操作很简单需要进一步了解的可以点击这里

抠图程序编写

首先我们将每张图对应的json文件命名与原图一样,以便于后续操作,并将全部json文件统一放到统一的文件夹下面,我这里的文件夹是picturejson
在这里插入图片描述
json文件的具体样式为:

{
  "version": "5.0.1",
  "flags": {},
  "shapes": [
    {
      "label": "yang",
      "points": [
        [
          34.09803921568627,
          200.0
        ],
        [
          79.19607843137254,
          162.7450980392157
        ],
        [
          200.76470588235293,
          166.66666666666666
        ],
        [
          418.4117647058823,
          162.7450980392157
        ],
        [
          447.82352941176475,
          245.09803921568627
        ],
        [
          426.2549019607843,
          327.45098039215685
        ],
        [
          439.98039215686276,
          411.7647058823529
        ],
        [
          706.6470588235294,
          325.4901960784314
        ],
        [
          718.4117647058823,
          1788.235294117647
        ],
        [
          32.13725490196077,
          1796.078431372549
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "yang",
      "points": [
        [
          1043.9019607843138,
          168.62745098039215
        ],
        [
          1392.921568627451,
          164.70588235294116
        ],
        [
          1432.1372549019607,
          254.90196078431373
        ],
        [
          1406.6470588235293,
          350.98039215686276
        ],
        [
          1438.0196078431372,
          1780.392156862745
        ],
        [
          841.9411764705882,
          1813.7254901960785
        ],
        [
          743.9019607843137,
          1156.862745098039
        ],
        [
          745.8627450980392,
          890.1960784313725
        ],
        [
          857.6274509803922,
          774.5098039215686
        ],
        [
          994.8823529411764,
          760.7843137254902
        ],
        [
          983.1176470588234,
          200.0
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "yang",
      "points": [
        [
          2416.450980392157,
          164.70588235294116
        ],
        [
          2851.7450980392155,
          168.62745098039215
        ],
        [
          2857.627450980392,
          1782.3529411764705
        ],
        [
          2189.0,
          1792.156862745098
        ],
        [
          2153.705882352941,
          905.8823529411765
        ],
        [
          2120.372549019608,
          190.19607843137254
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "imagePath": "00001P.jpg",
  "imageData": null,
  "imageHeight": 1901,
  "imageWidth": 2874
}

注意

我们在抠图的时候不要把图片的imagedata给保存,这个对勾给取消,不然数据乱码,json文件会识别不出来。
通过labelme的json文件实现对图片的批量裁剪_第1张图片

然后我们还需要对应的图片的数据集以便于我们可以通过json文件找到指定的图片并对指定的图片进行操作,这里要保证图片可以找到
通过labelme的json文件实现对图片的批量裁剪_第2张图片

具体程序编码



import os
import cv2
import json
import numpy as np


jsonPath = "./picturejson/"
jpgDirPath = "./dataset/"


jsonName = os.listdir(jsonPath)

for json1 in jsonName:

    img = cv2.imread(jpgDirPath + json1.split(".")[0] + ".jpg")

    path = jsonPath +json1
    print(path)

    j = 0
   
    with open(path, 'r', encoding='utf-8') as f:
        load_dict = json.load(f)
        dic_data = load_dict["shapes"]

        for i in dic_data:
            pts = np.array(i["points"])

            
            pts = pts.astype(np.int64)

           
            rect = cv2.boundingRect(pts)
            x, y, w, h = rect
            croped = img[y:y + h, x:x + w].copy()
           
            pts = pts - pts.min(axis=0)
            mask = np.zeros(croped.shape[:2], np.uint8)
            cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)

           
            dst = cv2.bitwise_and(croped, croped, mask=mask)
            
            bg = np.ones_like(croped, np.uint8) * 255
            cv2.bitwise_not(bg, bg, mask=mask)
            dst2 = bg + dst



            Save_File = "./cutout/"
            Name_File = i['label']

            category = load_dict['imagePath']

            if "P" in category.split('.')[0] or "p" in category.split('.')[0]:
                save_path = os.path.join(Save_File,Name_File+'/psave', load_dict['imagePath'].split(".")[0]+"_"+str(j) + '.jpg')
            else:
                save_path = os.path.join(Save_File, Name_File + '/',load_dict['imagePath'].split(".")[0] + "_" + str(j) + '.jpg')


            if os.path.exists(Save_File + Name_File):
                cv2.imwrite(save_path, dst2)
            else:
                os.makedirs(Save_File + Name_File + "/psave")
            j += 1

结果

程序会在当前文件夹下进行创建一个cutout文件夹并且同时会在cutout文件夹下对数据的类别进行分类存储。
在这里插入图片描述

你可能感兴趣的:(Python,json,python,开发语言)