labelme标注的文件去按面积除大颗粒或者小颗粒
去除大颗粒脚本
点击查看代码
import cv2
import numpy as np
import json
import os
def list_jsons(folder_path):
for filename in os.listdir(folder_path):
if filename.endswith(('.json',)):
yield os.path.join(folder_path, filename)
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 > 1600): # 过滤的颗粒面积
new_shapes.append(shape)
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)
if __name__ == '__main__':
# 使用例子
folder_path = '/home/administrator/gzj/pic/primary_ban/big_particle/src_1600/src' # 替换为你的文件夹路径
for json_path in list_jsons(folder_path):
remove_specific_labels(json_path)
点击查看代码
import cv2
import numpy as np
import json
import os
def list_jsons(folder_path):
for filename in os.listdir(folder_path):
if filename.endswith(('.json',)):
yield os.path.join(folder_path, filename)
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<1600): #按面积过滤
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__':
# 使用例子
folder_path = '/home/administrator/gzj/pic/primary_ban/src/' # 替换为你的文件夹路径
for json_path in list_jsons(folder_path):
remove_specific_labels(json_path)
#remove_specific_labels('/home/administrator/gzj/pic/primary_ban/src_copy')
#E:\SVN\08代码交接\granule_server_cngr_2\granule_server_cngr_2
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期