1、VOC格式篇
这个代码的用途是将annotations下的xml文件转换成txt形式的labels。
# 第二步运行
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')] #替换为自己的数据集
classes = ["person","bird","cat","cow","dog","horse",
"sheep","aeroplane","bicycle","boat","bus","car","motorbike","train","bottle","chair","diningtable","pottedplant","sofa","tvmonitor"] #修改为自己的类别
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def convert_annotation(year, image_id):
in_file = open('Annotations/%s.xml'%(image_id)) #将数据集放于当前目录下
out_file = open('label/%s.txt'%(image_id), 'w')
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
for year, image_set in sets:
if not os.path.exists('labels/'):
os.makedirs('labels/')
image_ids = open('ImageSets/Main/%s.txt'%(image_set)).read().strip().split()
list_file = open('%s_%s.txt'%(year, image_set), 'w')
for image_id in image_ids:
# list_file.write('JPEGImages/%s.jpg\n'%(image_id))
list_file.write('%s.jpg\n'%(image_id))
convert_annotation(year, image_id)
list_file.close()
# os.system("cat 2007_train.txt 2007_val.txt > train.txt") #修改为自己的数据集用作训练
2、COCO格式篇
3、图像尺寸变换
import datetime
import io, os
from PIL import Image
import shutil
import cv2
import numpy as np
import matplotlib.pyplot as plt
def crop_image(image, size): # image_dir 批量处理图像文件夹 size 裁剪后的尺寸
h, w = image.shape[0:2]
h_no = h // size
w_no = w // size
for row in range(0, h_no):
for col in range(0, w_no):
cropped_img = image[size*row : size*(row+1), size*col : size*(col+1), : ]
return cropped_img
inputPathRoot = 'D:\\Users\\my_SAR\\'
outputPathRoot = 'D:\\Users\\my_SAR_jpg\\'
flag_change_format = 1
# 读取图像, 转换格式, 裁剪, 加黑边(保持分辨率)
if flag_change_format:
print(str(datetime.datetime.now()) + " 图片格式转换功能激活 ")
counter = 0
for roots, dirs, files in os.walk(inputPathRoot):
for index in range(len(files)):
formatFile = files[index].split('.')[-1]
name = files[index].split('.')[-2]
# print(name)
if formatFile == 'JPG':
oldFilePath = os.path.join(roots, files[index])
# print(oldFilePath)
img = cv2.imread(oldFilePath)
newroots = roots.replace(inputPathRoot,outputPathRoot)
if not os.path.exists(newroots):
os.makedirs(newroots)
newFilePath = os.path.join(newroots, name)
newFilePath = str(newFilePath) + '.jpg'
# print(newFilePath)
h, w, c = img.shape
if h<=128 and w<=128:
#cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_CONSTANT, value=0)
img = cv2.copyMakeBorder(img,64-h//2, 64-h//2, 64-w//2, 64-w//2,cv2.BORDER_CONSTANT, value=0)
img2 = cv2.resize(img, (128, 128), interpolation=cv2.INTER_CUBIC)
elif h>128 and w<=128:
img = cv2.copyMakeBorder(img,h,128,0,0,cv2.BORDER_CONSTANT,value=0)
img2 = crop_image(img, 128)
elif h<=128 and w>128:
img = cv2.copyMakeBorder(img, 128, w,0,0,cv2.BORDER_CONSTANT,value=0)
img2 = crop_image(img, 128)
else:
img2 = crop_image(img, 128)
cv2.imwrite(newFilePath, img2)
counter += 1
print('已经转换 %s 张图' % (counter))
print(str(datetime.datetime.now()) + " 完成 ! ! ! ! ! ! ")
flag_transform = 0
# 搜索数据中特定格式的文件, 拷贝到另一个文件夹内
if flag_transform:
print(str(datetime.datetime.now()) + " 开始转换 ......... ")
counter = 0
for roots, dirs, files in os.walk(inputPathRoot):
for index in range(len(files)):
formatFile = files[index].split('.')[-1]
if formatFile == 'tif':
# print(files)
# for name in files:
# if '.JPG' in name:
# print(name)
oldFilePath = os.path.join(roots, files[index])
if 'Patch_Uint8' in oldFilePath:
newroots = roots.replace(inputPathRoot,outputPathRoot)
if not os.path.exists(newroots):
os.makedirs(newroots)
newFilePath = os.path.join(outputPathRoot, files[index])
shutil.copyfile(oldFilePath, newFilePath)
# os.system("mstar2jpeg -i %s -o %s -q 95" % (oldFilePath,newFilePath))
counter += 1
print('已经转换 %s 张图' % (counter))
print(str(datetime.datetime.now()) + " 完成 ! ! ! ! ! ! ")
else :
print('转换功能未激活')