去除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 @   阳光天气  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
点击右上角即可分享
微信分享提示