设置图片大小
# coding=utf-8
import os # 打开文件时需要
from PIL import Image
import re
Start_path = 'F:/faster-rcnn-tensorflow-python3-master/data/VOCdevkit2007/VOC2007/JPEGImages/' # 你的图片目录
width_max = 375 # 图片最大宽度
depth_max = 500 # 图片最大高度
list = os.listdir(Start_path)
# print list
count = 0
for pic in list:
path = Start_path + pic
print(path)
im = Image.open(path)
w, h = im.size
print (w,h)
#如果图片分辨率超过这个值,进行图片的等比例压缩
if w > width_max:
print(pic)
print("图片名称为" + pic + "图片被修改")
h_new = width_max * h // w
w_new = width_max
count = count + 1
out = im.resize((w_new, h_new), Image.ANTIALIAS)
new_pic = re.sub(pic[:-4], pic[:-4] , pic)
# print new_pic
new_path = Start_path + new_pic
out.save(new_path)
if h > depth_max:
print(pic)
print("图片名称为" + pic + "图片被修改")
w_new = depth_max * w // h
h_new = depth_max
count = count + 1
out = im.resize((w_new, h_new), Image.ANTIALIAS)
new_pic = re.sub(pic[:-4], pic[:-4] , pic)
# print new_pic
new_path = Start_path + new_pic
out.save(new_path)
print('END')
count = str(count)
print("共有" + count + "张图片尺寸被修改")
设置图片名字
import os
import os
path ='F:/faster-rcnn-tensorflow-python3-master/data/VOCdevkit2007/VOC2007/JPEGImages/' # 你的图片目录
filelist = os.listdir(path) #该文件夹下所有的文件(包括文件夹)
count=0
for file in filelist:
Olddir = os.path.join(path, file) # 原来的文件路径
if os.path.isdir(Olddir): # 如果是文件夹则跳过
continue
filename = os.path.splitext(file)[0] # 文件名
filetype = os.path.splitext(file)[1] # 文件扩展名
print(filename)
Newdir = os.path.join(path, str(count).zfill(5) + filetype) # 用字符串函数zfill 以0补全所需位数
os.rename(Olddir, Newdir) # 重命名
count += 1
# os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
print ('END')
count=str(count)
print("共有"+count+"张图片尺寸被修改")
filelist = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
count = 0
for file in filelist:
Olddir = os.path.join(path, file) # 原来的文件路径
if os.path.isdir(Olddir): # 如果是文件夹则跳过
continue
filename = os.path.splitext(file)[0] # 文件名
filetype = os.path.splitext(file)[1] # 文件扩展名
print(filename)
Newdir = os.path.join(path, str(count).zfill(5) + filetype) # 用字符串函数zfill 以0补全所需位数
os.rename(Olddir, Newdir) # 重命名
count += 1
# os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
print('END')
count = str(count)
print("共有" + count + "张图片尺寸被修改")
生成训练和测试需要的txt文件索引
# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
import random
trainval_percent = 0.8 #trainval占比例多少
train_percent = 0.7 #test数据集占比例多少
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)
num=len(total_xml)
list=range(num)
tv=int(num*trainval_percent)
tr=int(tv*train_percent)
trainval= random.sample(list,tv)
train=random.sample(trainval,tr)
ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')
for i in list:
name=total_xml[i][:-4]+'\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest .close()