将切分的图片筛选出有缺陷的
- 需求
- 代码
需求
由于之前切分的图像有一些存在没有缺陷,需要再次筛选
将可视化的图像更改后缀
更改为xml的
可视化代码
可视化后只有7000多个图像
原本的图像有1W多张
代码
# 按照xml文件删除对应的图片
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json#删除重名图象文件def read_json(file_name):with open(file_name, 'rb') as f:data = json.load(f)return data#读取源配置文件的路径
def delete1(image_root_path,suffix):delImg = []for root, dirs, files in os.walk(image_root_path):# 使用os模块获取文件夹中所有文件的路径all_files = os.listdir(root)filtered_files = [file for file in all_files if file.endswith(suffix)]if (len(filtered_files)):# print(root) #当前工作文件夹for i in range(len(filtered_files)):bmp_path = root + "\\" + filtered_files[i]delImg.append(filtered_files[i])return delImg#在目标文件中删除图象
def delete2(image_root_path,suffix,delImg):del_delImg = []for root, dirs, files in os.walk(image_root_path):# 使用os模块获取文件夹中所有文件的路径all_files = os.listdir(root)filtered_files = [file for file in all_files if file.endswith(suffix)]if (len(filtered_files)):print(root) #当前工作文件夹for i in range(len(filtered_files)):bmp_path = root + "\\" + filtered_files[i]for j in range(len(delImg)):if(delImg[j]==filtered_files[i]):print(bmp_path)del_delImg.append(bmp_path)return del_delImgdef myCopyImg(del_delImg,desPath):if(len(del_delImg)>0):for i in range(len(del_delImg)):bmp_path = del_delImg[i]#os.remove(bmp_path)shutil.copy(bmp_path, desPath) # shutil.copy函数放入原文件的路径文件全名 然后放入目标文件夹else:print("无文件")#替换列表中文件的后缀
def tihuanhouozhui(delImg):old_suffix1 = '.txt'old_suffix2 = '.xml'new_suffix = '.bmp'delImg = [file.replace(old_suffix1, new_suffix) for file in delImg]delImg = [file.replace(old_suffix2, new_suffix) for file in delImg]return delImgif __name__ == "__main__":#my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")delImg1 = delete1("G:\\datasets\\coco\\vis",".xml")#delImg2 = delete1("E:\\黄花标注\\glass\\testtt\\yesann", ".xml")#delImg = delImg1+delImg2#delImg = delImg1#new_delImg = tihuanhouozhui(delImg1)# for i in range(len(delImg)):# os.remove(delImg[i])del_delImg = delete2("G:\\datasets\\coco\\split\\xml",".xml",delImg1)desPath = "G:\\datasets\\coco\\final"myCopyImg(del_delImg,desPath)
将xml复制过来了
同理复制jpg
也是先改名
(将vis中的文件改为jpg)
总图像文件:
然后使用代码:
# 按照xml文件删除对应的图片
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json#删除重名图象文件def read_json(file_name):with open(file_name, 'rb') as f:data = json.load(f)return data#读取源配置文件的路径
def delete1(image_root_path,suffix):delImg = []for root, dirs, files in os.walk(image_root_path):# 使用os模块获取文件夹中所有文件的路径all_files = os.listdir(root)filtered_files = [file for file in all_files if file.endswith(suffix)]if (len(filtered_files)):# print(root) #当前工作文件夹for i in range(len(filtered_files)):bmp_path = root + "\\" + filtered_files[i]delImg.append(filtered_files[i])return delImg#在目标文件中删除图象
def delete2(image_root_path,suffix,delImg):del_delImg = []for root, dirs, files in os.walk(image_root_path):# 使用os模块获取文件夹中所有文件的路径all_files = os.listdir(root)filtered_files = [file for file in all_files if file.endswith(suffix)]if (len(filtered_files)):print(root) #当前工作文件夹for i in range(len(filtered_files)):bmp_path = root + "\\" + filtered_files[i]for j in range(len(delImg)):if(delImg[j]==filtered_files[i]):print(bmp_path)del_delImg.append(bmp_path)return del_delImgdef myCopyImg(del_delImg,desPath):if(len(del_delImg)>0):for i in range(len(del_delImg)):bmp_path = del_delImg[i]#os.remove(bmp_path)shutil.copy(bmp_path, desPath) # shutil.copy函数放入原文件的路径文件全名 然后放入目标文件夹else:print("无文件")#替换列表中文件的后缀
def tihuanhouozhui(delImg):old_suffix1 = '.txt'old_suffix2 = '.xml'new_suffix = '.bmp'delImg = [file.replace(old_suffix1, new_suffix) for file in delImg]delImg = [file.replace(old_suffix2, new_suffix) for file in delImg]return delImgif __name__ == "__main__":#my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")delImg1 = delete1("G:\\datasets\\coco\\vis",".jpg")#delImg2 = delete1("E:\\黄花标注\\glass\\testtt\\yesann", ".xml")#delImg = delImg1+delImg2#delImg = delImg1#new_delImg = tihuanhouozhui(delImg1)# for i in range(len(delImg)):# os.remove(delImg[i])del_delImg = delete2("G:\\datasets\\coco\\split\\yes",".jpg",delImg1)desPath = "G:\\datasets\\coco\\final2"myCopyImg(del_delImg,desPath)