labelme标注后的数据根据面积过滤,把小面积的多边形改成另一种标签

修改前

代码:

点击查看代码
import cv2
import numpy as np
import json
def remove_specific_labels(json_file):
    # 读取JSON文件
    with open(json_file, 'r') as f:
        data = json.load(f)
        data_small=data
    if"shapes" in data:
        new_shapes=[]
        new_shapes_small=[]
        # 提取闭合区域的轮廓
        contours = []
        for shape in data['shapes']:
            points = np.array(shape['points'], np.int32)
            area=cv2.contourArea(points)
            if(area>600):
                new_shapes.append(shape)
            else:
                shape["label"]="small_ban"
                new_shapes.append(shape)
                print("")
            # print("闭合区域的面积为", cv2.contourArea(points))
            # contours.append(points)
        data["shapes"]=new_shapes
        # data_small["shapes"]=new_shapes_small
    with open(json_file,'w',encoding='utf-8')as f:
        json.dump(data,f,ensure_ascii=False,indent=4)
    # with open(r"D:\gzj\pic\SEE\0827\backup\small.json",'w',encoding='utf-8')as f:
    #     json.dump(data_small,f,ensure_ascii=False,indent=4)
if __name__ == '__main__':
    remove_specific_labels(r'D:\gzj\pic\SEE\0827\Z003.json')
    #E:\SVN\08代码交接\granule_server_cngr_2\granule_server_cngr_2
修改后:

posted @ 2024-08-27 16:57  阳光天气  阅读(1)  评论(0编辑  收藏  举报