1.生成Yolox检测负样本-对应空文件夹txt
import os.path
import cv2
from tqdm import tqdm
path = r"G:\pachong\fuyangben_img"
save_path=r"G:\pachong\fuyangben_txt"
files = os.listdir(path)
print(files)
for pic in tqdm(files):
before_name = os.path.splitext(pic)[0]
txt_name = os.path.splitext(before_name)[0] + ".txt"
txt_name = os.path.join(save_path,txt_name)
f = open(txt_name, "w")
2.批量文件重命名
import os
import os.path
path = "G:\\pachong\\fuyangben\\"
files = os.listdir(path)
i=0
for file in files:
original = path + os.sep+ files[i]
new = path + os.sep +'fuyangben'+ str(i+1) + ".jpg"
os.rename(original,new)
i +=1
3.批量转化三通道去除小图
import os.path
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import cv2
from tqdm import tqdm
path = r"G:\pachong\fuyangben_img"
files = os.listdir(path)
print(files)
i = 0
a = 0
c = 0
for pic in tqdm(files):
img = Image.open(os.path.join(path, pic))
i +=1
if len(img.getbands()) != 3:
img = img.convert("RGB")
pic_new = os.path.join(path, pic)
img.save(pic_new)
a +=1
size = img.size
w = size[0]
h = size[1]
if w<100 or h<100:
os.remove(os.path.join(path, pic))
print('图像image的总数量: ', i)
print('总图像不是RGB的数量:', a)
print('总图像是640*480的数量:', c)