Bootstrap

python 批量修剪图片数据集

应用SSD进行物体检测,做了一个数据集,命名规则如下:

 

发现每张图片的大小不一样,需要对图形进行修剪

1.修剪代码如下所示:

import cv2
import os
dir_name="/home/kd/PandaDetect/datasets/JPEGImages/"
pic_type=".jpeg"
dir_name_re="/home/kd/PandaDetect/datasets/JPEGImagesR/"
def visitDir(dir_name):
    if not os.path.isdir(dir_name):
        print('Error:"',dir_name,'"is not a directory or does nort exist.')
        return
    else:
        global x
        try:
            for lists in os.listdir(dir_name):
                sub_path=os.path.join(dir_name,lists)
                x+=1
                print('No.',x,' ',sub_path)
                if os.path.isdir(sub_path):
                    visitDir(sub_path)
        except:
            pass
#调用函数,设置相关的参数
x=0
visitDir(dir_name)
len=x
i=1

#对120张图片进行修剪,利用循环的方式自动进行
#str()将int转换为str,字符可以直接相加
while i<=len:
    img = cv2.imread(dir_name + str(i) + pic_type)
    img_resize=cv2.resize(img, (128, 128), interpolation=cv2.INTER_CUBIC)
    cv2.waitKey(1000)
    cv2.imshow("picture",img_resize)
    cv2.imwrite(dir_name_re + str(i)+"r" + pic_type, img_resize)
    i+=1


 

;