去除labelme空的标注文件和对应的图片

去除labelme空的标注文件和对应的图片

点击查看代码
import os
import json
 
def is_empty_labelme_file(file_path):
    with open(file_path, 'r') as f:
        data = json.load(f)
        # 检查图像数据是否存在
        # if 'imageData' not in data:
        #     return True
        # 检查标注列表是否为空
        if len(data['shapes']) == 0:
            return True
    return False
 
def remove_empty_labelme_files(dir_path):
    for filename in os.listdir(dir_path):
        file_path = os.path.join(dir_path, filename)
        if filename.endswith('.json') and is_empty_labelme_file(file_path):
            
            file_name,old_suffix=os.path.splitext(filename)
            jpg_filename=file_name+".jpg"
            jpg_file_path=os.path.join(dir_path,jpg_filename)
            json_file_path=file_path
            print(f"Removed empty file: {jpg_file_path}")
            print(f"Removed empty file: {json_file_path}")
            print("")
            os.remove(jpg_file_path)
            os.remove(json_file_path)

 
# 使用示例
labelme_dir = '/home/administrator/gzj/pic/primary_ban/src_copy_coco/cutted_generated'  # 替换为你的labelme文件夹路径
remove_empty_labelme_files(labelme_dir)
posted @ 2024-12-06 11:10  阳光天气  阅读(113)  评论(0)    收藏  举报