功能描述: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)