python修改json参数

对labelme生成的文件进行指定的修改

import json
import os
import re
import operator


def file_updata(file):
    try:
        with open(file,'r') as f:
            file_data=f
            #使用json.load()函数将对象转化为集合
            #获取集合中的部分参数的数值
            str_json_book=json.load(file_data)
            str_json_book['shapes'][0]['label']='freespace'
            str_json_book['shapes'][0]['shape_type']='polygon'
            points_0_len = len(str_json_book['shapes'][0]['points'])
            points_0_start_x=str_json_book['shapes'][0]['points'][0][0]
            points_0_end_x=str_json_book['shapes'][0]['points'][points_0_len-1][0]
            height = str_json_book['imageHeight']
            width = str_json_book['imageWidth']
            # for json_line1 in range(0,points_0_len):
            #     print(str_json_book['shapes'][0]['points'][json_line1])

            # 多区间合并
            #当存在多个对象是进行合并修改
            if len(str_json_book['shapes'])>1:
                print("2")
                points_1_len = len(str_json_book['shapes'][1]['points'])
                points_1_start_x=str_json_book['shapes'][1]['points'][0][0]
                points_1_end_x=str_json_book['shapes'][1]['points'][points_1_len-1][0]
                if abs(points_1_start_x-points_0_end_x) < abs(points_1_end_x-points_0_end_x) :
                    for json_line2 in range(0,points_1_len):
                        list_temp=str_json_book['shapes'][1]['points'][json_line2]
                        str_json_book['shapes'][0]['points'].append(list_temp)
                else:
                    for json_line2 in range(0,points_1_len):
                        list_temp=str_json_book['shapes'][1]['points'][json_line2]
                        str_json_book['shapes'][0]['points'].insert(0+json_line2,list_temp)
                str_json_book['shapes'].pop(1)

            list1=[width-1,height-1]
            list2=[0,height-1]
            list3=[str_json_book['shapes'][0]['points'][points_0_len-1][0],str_json_book['shapes'][0]['points'][points_0_len-1][1]]
            #operator.eq()函数可以用来比较两组列表是否相等
            if str_json_book['shapes'][0]['points'][points_0_len-1][0] > 600 and operator.eq(list1,list3)==False and operator.eq(list2,list3)==False:
                str_json_book['shapes'][0]['points'].append(list1)
                str_json_book['shapes'][0]['points'].append(list2)
            elif operator.eq(list2,list3)==False and operator.eq(list1,list3)==False:
                str_json_book['shapes'][0]['points'].append(list2)
                str_json_book['shapes'][0]['points'].append(list1)
    except:
        print("error main","第:",file)
    finally:
        # print(str_json_book)
        file_data = json.dumps(str_json_book,sort_keys=False,indent=3,separators=(',',': '))
        # file_data = json.dump(file_data)
        f.close()
    with open(file,'w') as f:
        try:
            f.write(file_data)
            # print(file_data)
            print(file)
        except:
            print("write error")
        finally:
            f.close()
if __name__ == "__main__":
    file_num = 1
    #os.listdir()函数查看并返回文件夹下面的文件
    #json.dumps()函数将os.listdir()的返回转化为string类型并且indent=1参数添加缩进
    file_dir=json.dumps(os.listdir('./'),indent=1)
    #re.compile()可以写入正则表达式
    res=re.compile(r'"([0-9].*\.json)"')
    # print(file_dir)
    # for line in re.findall(res,file_dir):
    #     print(line)
    for line in re.findall(res,file_dir):
    #使用os.path.exists()函数查看文件是否存在
        if(os.path.exists(line)):
            file_updata(line)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值