labelme去除空图片

Labelme是一个用于图像标注的开源工具。在使用Labelme进行数据标注后,可能会生成一些空的图像文件(即没有进行标注的图片),这些空图片通常不应该被使用。以下是一个简单的Python脚本,用于检测和删除这些空的标注文件:

点击查看代码
import os
import json


def is_image_empty(image_path):
    # 加载JSON文件
    json_file = image_path.replace('.jpg', '.json')
    if not os.path.isfile(json_file):
        return True
    with open(json_file, 'r') as f:
        data = json.load(f)
    # 检查是否有标注
    return len(data['shapes']) == 0


def remove_empty_images(image_dir):
    for filename in os.listdir(image_dir):
        if filename.endswith('.jpg'):
            image_path = os.path.join(image_dir, filename)
            if is_image_empty(image_path):
                # 删除图片和对应的JSON文件
                os.remove(image_path)
                json_file = image_path.replace('.jpg', '.json')
                if os.path.isfile(json_file):
                    os.remove(json_file)


# 使用方式:将image_dir替换为你的图像文件夹路径
remove_empty_images(r'D:\pic\zhongwei\label_pic_wfdkl_wfxkl_reworded\target')
posted @ 2024-05-17 10:15  阳光天气  阅读(1)  评论(0编辑  收藏  举报