处理标签和图片对应关系

功能描述:findNullAnnotation(annotation_path,images_path,target_path)

参数解释:annotation_path为包含所有标注文件的文件夹路径,images_path为包含所有图片文件的文件夹路径;target_path为所要移动的图片保存的路径。

如果一张图有空标签或者没有标签,就把这张图移动到目标文件夹,等待后续对这些没有标注的图片进行处理;

deleteNoMeaningAnnotation(annotation_path,images_path)

如果在标签列表里存在标签但图片列表里找不到的标签删除。

import os
import shutil
def deleteNoMeaningAnnotation(annotation_path,images_path):
    # 清除那些有标注但图片不存在的标注文件
    anno_names = os.listdir(annotation_path)
    images_name = os.listdir(images_path)
    i=0
    for anno in anno_names:
        name = anno.split('.')[0]
        if name+'.jpg' in images_name or name+'.JPG' in images_name:
            continue
        else:
            if os.path.exists(os.path.join(annotation_path,anno)):
                os.remove(os.path.join(annotation_path,anno))
                print("成功删除{}".format(anno))
                i += 1
    print("共删除%d"%i)

def findNullAnnotation(annotation_path,images_path,target_path):
    # 从标注文件夹中找那些标注为空或者没有标注的图片,把这些图片从原来的路径移动到目标路径
    anno_names = os.listdir(annotation_path)
    images_name = os.listdir(images_path)
    for image in images_name:
        name = image.split('.')[0]
        if name+'.txt' not in anno_names:
            shutil.move(os.path.join(images_path,image),os.path.join(target_path,image))
            print("%s在标注中不存在,对应图片被移动到目标文件夹"%name)
        else:
            if os.path.getsize(os.path.join(annotation_path,name+'.txt'))==0:
                shutil.move(os.path.join(images_path,image),os.path.join(target_path,image))
                print("%s标注存在但为空,对应图片被移动到目标文件夹"%name)

if __name__ == '__main__':
    annotation_path = r""
    images_path = r""
    target_path = r""
    deleteNoMeaningAnnotation(annotation_path,images_path)
    # findNullAnnotation(annotation_path,images_path,target_path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值