# -*- coding: utf-8 -*-
"""
@Time : 2023/11/2 10:08
@Auth : xyzzy
@File :writeTXT.py
@IDE :PyCharm
"""
import json
import os
# 指定包含 JSON 文件的文件夹路径
folder_path = 'images'
# 提取标签的集合
labels = set()
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith(".json"):
file_path = os.path.join(folder_path, filename)
# 读取 JSON 数据
with open(file_path, 'r') as file:
json_data = json.load(file)
# 提取标签
for shape in json_data["shapes"]:
labels.add(shape["label"])
# 将标签写入 labels.txt 文件
with open("labels.txt", "w") as file:
for label in labels:
file.write(label + "\n")
print("Labels have been written to labels.txt.")
json2label
于 2023-11-03 14:17:55 首次发布