栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号