labelme json文件提取特定区域成新json

# 读取json文件内容,返回字典格式
from collections import defaultdict, OrderedDict
import json
import cv2
from labelme import utils
def new_json(jsonname,xgpoints,txtpoints,imagepath,imageH,imageW,imagedata,xg_shapetype='rectangle',txt_shapetype="polygon"):
    # xg_dist={
    #     "label":"xg",
    #     "points": xgpoints,
    #     "group_id": None,
    #     "shape_type": xg_shapetype,
    #     "flags": {}
    # }
    txt_dist={
        "label":"txt",
        "points": txtpoints,
        "group_id": None,
        "shape_type": txt_shapetype,
        "flags": {}
    }

    shape = []
    # shape.append(xg_dist)
    shape.append(txt_dist)
    test_dict = {
        'version': "4.5.9",
        "flags": {},
        "shapes":shape,
        "imagePath": imagepath,
        "imageData":imagedata,
        "imageHeight": imageH,
        "imageWidth": imageW

    }

    json_str = json.dumps(test_dict, indent=4)
    with open(jsonname, 'w') as json_file:
        json_file.write(json_str)

def savefile(bs,path):
    with open(path,'wb') as ds:
        ds.write(bytes(bs))
        ds.close()


path ='0926305001_10005.json'
with open(path,'r',encoding='utf8')as fp:            
    newimage_dir ='newimage/'
    newjson_dir = 'newjson/'
    json_data = json.load(fp)
    labels = json_data.get('shapes')
    org_imagepath = json_data.get("imagePath")
    org_image =  cv2.imread(org_imagepath)
    
    print(org_image.shape)
    org_point=[]
    txt_point =[]
    for i in range(len(labels)):
        if labels[i]['label'] =='xg':
            org_point = labels[i]['points']
            st_x = int(org_point[0][0])
            st_y = int(org_point[0][1])
            ed_x = int(org_point[1][0])
            ed_y = int(org_point[1][1])
            cut_image =org_image[st_y:ed_y,st_x:ed_x,:]
            cut_image_temp =  cv2.cvtColor(cut_image,cv2.COLOR_BGR2RGB)
            imagedata=utils.img_arr_to_b64(cut_image_temp).decode('utf-8')
            cut_image_data =cv2.imencode(".jpg",cut_image)[1].tobytes()
            newimage_name = path.split('.')[0]
            newimage_path = newimage_dir + newimage_name +'cut.jpg'
            json_name = newjson_dir+newimage_name +'cut.json'
            savefile(cut_image_data,newimage_path)
        elif labels[i]['label'] =='txt':
            txt_point = labels[i]['points']
        else:
            pass
        if len(org_point)>0 and len(txt_point)>0:
            for i in range(len(txt_point)):
                txt_point[i][0]=txt_point[i][0]-org_point[0][0]
                txt_point[i][1]=txt_point[i][1]-org_point[0][1]
            org_point[1][0] = org_point[1][0]-org_point[0][0]
            org_point[1][1] = org_point[1][1]-org_point[0][1]
            org_point[0][0] = 0
            org_point[0][1] = 0
            new_json(json_name,org_point,txt_point,newimage_path,int(org_point[1][0]),int(org_point[1][1]),imagedata)
            break

        

上一篇:Final Cut Pro中文新手教程 (二六) 裁剪工具的使用


下一篇:2021-10-02 WPF上位机 53-MVVM模式中的命令