生成Yolox检测负样本-对应空文件夹txt、批量文件重命名、批量转化三通道去除小图

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" #生成空的txt文件夹
files = os.listdir(path)
print(files)
for pic in tqdm(files):
    # # basename = os.path.basename(image_name)
    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 glob
import os.path


path = "G:\\pachong\\fuyangben\\"
files = os.listdir(path)          #会按顺序排列1,2,3,4,.jpg格式
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        #批量修改尺寸2048*1024(此时也可能32位)
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))
    # img.close()
    # print(pic)
    # print(img.getbands())  # ('P',) 这种是有彩色的,而L是没有彩色的
    # print(img.size)
    i +=1
    # Img = np.array(img)
    # a = np.unique(Img)
    # print(a)    #看像素值
    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))  #去除小图,爬虫下在下来的小图会有问题,必须img.close() 否则会报错,这张图正在使用进程
    # if w == 640:
    #     if h == 480:
    #         c += 1

print('图像image的总数量: ', i)
print('总图像不是RGB的数量:', a)
print('总图像是640*480的数量:', c)
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值