-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdel.py
More file actions
81 lines (67 loc) · 3.34 KB
/
del.py
File metadata and controls
81 lines (67 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import os
def remove_extra_points_from_file(input_file, output_file):
"""删除单个JSON文件中points超过4个点的标签项"""
try:
# 读取JSON文件
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# 如果数据是字典,并且包含"shapes"键(且其值为列表)
if isinstance(data, dict) and "shapes" in data and isinstance(data["shapes"], list):
# 获取原始标签数量
original_count = len(data["shapes"])
# 过滤掉points数量超过4的标签
data["shapes"] = [shape for shape in data["shapes"]
if isinstance(shape.get("points", []), list)
and len(shape["points"]) <= 4]
new_count = len(data["shapes"])
else:
# 如果不是上述结构,尝试其他常见键名或直接处理列表
# 这里我们假设整个JSON是标签列表(如之前的情况)
if isinstance(data, list):
original_count = len(data)
data = [shape for shape in data
if isinstance(shape.get("points", []), list)
and len(shape["points"]) <= 4]
new_count = len(data)
else:
raise ValueError("JSON结构未知,无法处理。")
# 保存处理后的数据
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print(f"处理完成: {os.path.basename(input_file)}")
print(f"原始标签数: {original_count},处理后标签数: {new_count}")
print(f"已删除 {original_count - new_count} 个非法标签\n")
except Exception as e:
print(f"处理失败({os.path.basename(input_file)}): {str(e)}\n")
def process_folder(input_folder, output_folder=None):
"""处理文件夹中的所有JSON文件"""
# 如果没有指定输出文件夹,则在输入文件夹内创建"cleaned"子文件夹
if output_folder is None:
output_folder = os.path.join(input_folder, "cleaned")
# 创建输出文件夹(如果不存在)
os.makedirs(output_folder, exist_ok=True)
# 遍历文件夹中的所有文件
for filename in os.listdir(input_folder):
if filename.lower().endswith('.json'):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
# 处理每个JSON文件
remove_extra_points_from_file(input_path, output_path)
# 使用示例
if __name__ == "__main__":
# 可以指定单个文件或整个文件夹
input_path = r"C:\Users\follo\Pictures\数据集\太阳能板\labeling" # 文件夹路径
# input_path = r"D:\python\alabel2\DJI_20250709103057_0001_V.json" # 单个文件路径
# 如果是文件,调用单个文件处理函数
if os.path.isfile(input_path):
output_path = os.path.join(
os.path.dirname(input_path),
"cleaned_" + os.path.basename(input_path)
)
remove_extra_points_from_file(input_path, output_path)
# 如果是文件夹,调用文件夹处理函数
elif os.path.isdir(input_path):
process_folder(input_path)
else:
print("指定的路径既不是文件也不是文件夹")