文章目录
- 1. 判断xml和图像文件在两个文件夹是否一一对应.py
- 2. 判断xml中标签是否为空,移动空xml和图像到指定路径.py
- 3. 删除坐标有问题的xml和对应的图像.py
- 4.1 给标注的单个图像画框.py
- 4.2 图像批量显示标注位置
- 4.3 根据xml的类别给标注的图像批量画框(对图像分类显示标注位置)
- 5.1 从一堆图像里复制出我们要的图像文件.py
- 5.2 根据txt文件保存需要的图像到新文件夹.py
- 6.1 再筛xml
- 6.2 再保存对应的xml文件到新文件夹.py
- 7.1 滑动窗口式的裁剪图片与标注框.py
- 7.2 expot.py
- 8.删掉不要的裁剪图像和对应的xml.py
- 9. 扣框或者中心裁剪.py
- 10. 根据xml计算目标长宽面积等.py
- 11. 删掉不符合分辨率的图像.py
- 12. 划分训练集测试集的图像和xml.py
1. 判断xml和图像文件在两个文件夹是否一一对应.py
import cv2
import os
import shutil
def check_img(img_path):
imgs = os.listdir(img_path)
for img in imgs:
if img.split(".")[-1] != "jpg":
print(img)
shutil.move(img_path + "/" + img, "./error/" + img)
def check_anno(anno_path):
anno_files = os.listdir(anno_path)
for file in anno_files:
if file.split(".")[-1] != "xml":
print(file)
shutil.move(anno_path + "/" + file, "./error/" + file)
def ckeck_img_label(img_path, anno_path):
imgs = os.listdir(img_path)
anno_files = os.listdir(anno_path)
files = [i.split(".")[0] for i in anno_files]
for img in imgs:
if img.split(".")[0] not in files:
print(img)
shutil.move(img_path + "/" + img, "./error/" + img)
imgs = os.listdir(img_path)
images = [j.split(".")[0] for j in imgs]
for file in anno_files:
if file.split(".")[0] not in images:
print(file)
shutil.move(anno_path + "/" + file, "./error/" + file)
if __name__ == "__main__":
if not os.path.exists("./error/"):
os.makedirs("./error")
img_path = r"./11yueimgs/"
anno_path = r"./11yuexmls/"
print("============check image=========")
check_img(img_path)
print("============check anno==========")
check_anno(anno_path)
print("============check both==========")
ckeck_img_label(img_path, anno_path)
2. 判断xml中标签是否为空,移动空xml和图像到指定路径.py
import os
import xml.etree.ElementTree as ET
import shutil
origin_ann_dir = './11yuexmls/'
new_ann_dir = './trashxmls/'
origin_pic_dir = './11yueimgs/'
new_pic_dir = './trashimgs/'
k = 0
p = 0
q = 0
if not os.path.exists(new_ann_dir):
os.mkdir(new_ann_dir)
if not os.path.exists(new_pic_dir):
os.mkdir(new_pic_dir)
for dirpaths, dirnames, filenames in os.walk(origin_ann_dir):
for filename in filenames:
print("process...")
k = k + 1
print(k)
if os.path.isfile(r'%s%s' % (origin_ann_dir, filename)):
origin_ann_path = os.path.join(r'%s%s' % (origin_ann_dir, filename))
new_ann_path = os.path.join(r'%s%s' % (new_ann_dir, filename))
tree = ET.parse(origin_ann_path)
root = tree.getroot()
if len(root.findall('object')):
p = p + 1
else:
print(filename)
old_xml = origin_ann_dir + filename
new_xml = new_ann_dir + filename
old_pic = origin_pic_dir + filename.replace("xml", "jpg")
new_pic = new_pic_dir + filename.replace("xml", "jpg")
q = q + 1
shutil.move(old_pic, new_pic)
shutil.move(old_xml, new_xml)
print("ok, ", p)
print("empty, ", q)
3. 删除坐标有问题的xml和对应的图像.py
import xml.etree.ElementTree as xml_tree
import os
import shutil
"""
删除xmin,ymin>=xmax,ymax坐标的xml和对应的图像
"""
def check_box(path):
files = os.listdir(path)
i = 0
for anna_file in files:
tree = xml_tree.parse(path + "/" + anna_file)
root = tree.getroot()
for obj in root.findall('object'):
bbox = obj.find('bndbox')
if (float(bbox.find('ymin').text) >= float(bbox.find('ymax').text)) or (
float(bbox.find('xmin').text) >= float(bbox.find('xmax').text)):
print(anna_file)
i += 1
try:
shutil.move(path + "/" + anna_file, "./error2/" + anna_file)
shutil.move("./11yueimgs/" + anna_file.split(".")[0] + ".jpg",
"./error2/" + anna_file.split(".")[0] + ".jpg")
except:
pass
print(i)
if __name__ == "__main__":
if not os.path.exists("./error2"):
os.mkdir("./error2")
check_box("./11yuexmls")
4.1 给标注的单个图像画框.py
import os
import cv2
"""
(单张图像显示标注位置)
"""
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def GetAnnotBoxLoc(AnotPath):
tree = ET.ElementTree(file=AnotPath)
root = tree.getroot()
ObjectSet = root.findall('object')
ObjBndBoxSet = {
}
for Object in ObjectSet:
ObjName = Object.find('code').text
BndBox = Object.find('bndbox')
x1 = int(BndBox.find('xmin').text)
y1 = int(BndBox.find('ymin').text)
x2 = int(BndBox.find('xmax').text)
y2 = int(BndBox.find('ymax').text)
BndBoxLoc = [x1, y1, x2, y2]
if ObjName in ObjBndBoxSet:
ObjBndBoxSet[ObjName].append(BndBoxLoc)
else:
ObjBndBoxSet[ObjName] = [BndBoxLoc]
return ObjBndBoxSet
def display(objBox, pic):
img = cv2.imread(pic)
for key in objBox.keys():
for i in range(len(objBox[key])):
cv2.rectangle(img, (objBox[key][i][0], objBox[key][i][1]), (objBox[key][i][2], objBox[key][i][3]),
(0, 0, 255), 2)
cv2.putText(img, key, (objBox[key][i][0], objBox[key][i][1]), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 1)
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.imshow('img', img)
cv2.imwrite('display.jpg', img)
cv2.waitKey(0)
if __name__ == '__main__':
pic = r"C:\Desktop\0.jpg"
ObjBndBoxSet = GetAnnotBoxLoc(r"E:\xx\xml\2.xml")
print(ObjBndBoxSet)
display(ObjBndBoxSet, pic)
4.2 图像批量显示标注位置
import cv2
import numpy as np
import xml.dom.minidom
import os
import argparse
"""
(图像批量显示标注位置)
"""
def main():
img_path = './imgs/'
anno_path = './xmls/'
cut_path = './outs/'
imagelist = os.listdir(img_path)
for image in imagelist:
image_pre, ext = os.path.splitext(image)
img_file = img_path + image
img = cv2.imread(img_file)
xml_file = anno_path + image_pre + '.xml'
DOMTree = xml.dom.minidom.parse(xml_file)
collection = DOMTree.documentElement
objects = collection.getElementsByTagName("object")
for object in objects:
namelist = object.getElementsByTagName('code')
objectname = namelist[0].childNodes[0].data
bndbox = object.getElementsByTagName('bndbox')[0]
xmin = bndbox.getElementsByTagName('xmin')[0]
xmin_data = xmin.childNodes[0].data
ymin = bndbox.getElementsByTagName('ymin')[0]
ymin_data = ymin.childNodes[0].data
xmax = bndbox.getElementsByTagName('xmax')[0]
xmax_data = xmax.childNodes[0].data
ymax = bndbox.getElementsByTagName('ymax')[0]
ymax_data = ymax.childNodes[0].data
xmin = int(xmin_data)
xmax = int(xmax_data)
ymin = int(ymin_data)
ymax = int(ymax_data)
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), thickness=2)
cv2.putText(img, objectname, (xmin, ymin), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), thickness=2)
cv2.imwrite(cut_path + '{}.jpg'.format(image_pre), img)
if __name__ == '__main__':
main()
4.3 根据xml的类别给标注的图像批量画框(对图像分类显示标注位置)
from __future__ import division
import os
from PIL import Image
import xml.dom.minidom
import numpy as np
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import cv2
"""
根据xml的类别给标注的图像批量画框(对图像分类显示标注位置)
"""
ImgPath = './11yueimgs/'
AnnoPath = './11yuexmls/'
ProcessedPath = './class_kuang/'
if not os.path.exists(ProcessedPath):
os.makedirs(ProcessedPath)
imagelist = os.listdir(ImgPath)
for image in imagelist:
image_pre, ext = os.path.splitext(image)
imgfile = ImgPath + image
print(imgfile)
if not os.path.exists(AnnoPath + image_pre + '.xml'): continue
xmlfile = AnnoPath + image_pre + '.xml'
DomTree = xml.dom.minidom.parse(xmlfile)
annotation = DomTree.documentElement
objectlist = annotation.getElementsByTagName('object')
img = cv2.imread(imgfile)
for objects in objectlist:
namelist = objects.getElementsByTagName('code')
objectname = namelist[0].childNodes[0].data
bndbox = objects.getElementsByTagName('bndbox')[0]
xmin = bndbox.getElementsByTagName('xmin')[0]
xmin_data = xmin.childNodes[0].data
ymin = bndbox.getElementsByTagName('ymin')[0]
ymin_data = ymin.childNodes[0].data
xmax = bndbox.getElementsByTagName('xmax')[0]
xmax_data = xmax.childNodes[0].data
ymax = bndbox.getElementsByTagName('ymax')[0]
ymax_data = ymax.childNodes[0].data
xmin = int(xmin_data)
xmax = int(xmax_data)
ymin = int(ymin_data)
ymax = int(ymax_data)
savepath = ProcessedPath + objectname
if not os.path.exists(savepath):
os.makedirs(savepath)
cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0),
thickness=2)
cv2.putText(img, objectname,(int(xmin), int(ymin)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), thickness=2)
cv2.imwrite(savepath + '/' + image_pre + '.jpg',img)
5.1 从一堆图像里复制出我们要的图像文件.py
import os
import random
"""
根据需求,先筛选出需要的图像根据txt中图片的名字批量提取对应的图片并保存到另一个文件夹
1.先获得需要转换的图像名称的txt文件
"""
data_base_dir = "./11yueimgs"
file_list = []
write_file_name = '11yue_need_imgs.txt'
write_file = open(write_file_name, "w")
for file in os.listdir(data_base_dir):
if file.endswith(".jpg"):
write_name = file
file_list.append(write_name)
sorted(file_list)
number_of_lines = len(file_list)
for current_line in range(number_of_lines):
write_file.write(file_list[current_line][:-4] + '\n')
5.2 根据txt文件保存需要的图像到新文件夹.py
import sys
import re
from PIL import Image,ImageFile
import os
ImageFile.LOAD_TRUNCATED_IMAGES = True
if not os.path.exists("./outimgs"):
os.makedirs("./outimgs")
data = []
for line in open("./11yue_need_imgs.txt", "r"):
data.append(line)
for a in data:
im = Image.open('./imgs/{}'.format(a[:-1])+".jpg")
im.save('./outimgs/{}'.format(a[:-1])+".jpg")
im.close()
6.1 再筛xml
import sys
sys.path.append('D:\\Program files\\Anaconda\\libs')
import os
import random
data_base_dir = r"F:\zy\2haotu"
file_list = []
write_file_name = 'image.txt'
write_file = open(write_file_name, "w")
for file in os.listdir(data_base_dir):
if file.endswith(".jpg"):
write_name = file
file_list.append(write_name)
sorted(file_list)
number_of_lines = len(file_list)
for current_line in range(number_of_lines):
write_file.write(file_list[current_line][:-4] +'\n')
6.2 再保存对应的xml文件到新文件夹.py
import os
import shutil
if __name__ == '__main__':
f = open("./image.txt","r")
line = f.readline()
line = line[:-1]
if not os.path.exists("./out"):
os.makedirs("./out")
while line:
line = f.readline()
line = line.strip('\n')
print(line)
new_path = r"F:/zy/6yuexmls/"+line+".xml"
print(new_path)
try:
shutil.move(new_path, './out')
except:
print("Not find error.")
f.close()
7.1 滑动窗口式的裁剪图片与标注框.py
"""
这里是实现分辨率为w_h裁剪成多张a_a大小的图片(a<=w且a<=h),以大小为a/2的步长滑动,裁剪同时修改标注框。
1.把这个文件命名为xml_parse.py
"""
import xml.dom.minidom as xmldom
import os.path
def voc_xml_parse(xml_path):
object_list = []
domobj = xmldom.parse(xml_path)
elementobj = domobj.documentElement
folderobj = elementobj.getElementsByTagName("folder")[0]
filenameobj = elementobj.getElementsByTagName("filename")[0]
sizeobj = elementobj.getElementsByTagName("size")[0]
head = {
'folder': folderobj, 'filename': filenameobj, 'size': sizeobj,}
object_list = elementobj.getElementsByTagName("object")
return head, object_list
def voc_xml_modify(modify_xml_path, head, object_list):
dom = xmldom.Document()
root = dom.createElement('annotation')
dom.appendChild(root)
for obj in head.values():
root.appendChild(obj)
for obj in object_list:
root.appendChild((obj))
with open(modify_xml_path, 'w', encoding='utf-8') as f:
dom.writexml(f, addindent='\t', newl='\n', encoding='utf-8')
return
7.2 expot.py
import cv2
import os
import codecs
import xml_parse
import xml.dom.minidom as xmldom
import xml.etree.ElementTree as ET
import numpy as np
from PIL import Image
from tqdm import tqdm
import sys
sys.setrecursionlimit(10000)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint16)
def crop_xml_modify(head, objectlist, hmin, wmin, new_height, new_width, origin_xml__path):
filenameobj = head['filename']
sizeobj = head['size']
width = sizeobj.getElementsByTagName('width')[0]
width.childNodes[0].data = str(new_width)
height = sizeobj.getElementsByTagName('height')[0]
height.childNodes[0].data = str(new_height)
obj = objectlist
i = 0
while (i < obj.length):
bndbox = obj[i].getElementsByTagName('bndbox')[0]
xmin = bndbox.getElementsByTagName('xmin')[0]
XMIN = float(xmin.childNodes[0].data)
ymin = bndbox.getElementsByTagName('ymin')[0]
YMIN = float(ymin.childNodes[0].data)
xmax = bndbox.getElementsByTagName('xmax')[0]
XMAX = float(xmax.childNodes[0].data)
ymax = bndbox.getElementsByTagName('ymax')[0]
YMAX = float(ymax.childNodes[0].data)
if (XMIN >= wmin) and (XMAX <= (wmin + new_width)) and (YMIN >= hmin) and (YMAX <= (hmin + new_height)):
xmin.childNodes[0].data = str(int(XMIN - wmin))
xmax.childNodes[0].data = str(int(XMAX - wmin))
ymin.childNodes[0].data = str(int(YMIN - hmin))
ymax.childNodes[0].data = str(int(YMAX - hmin))
else:
obj.remove(obj[i])
i = i - 1
i = i + 1
return head, obj
def crop_dataset(imgpath, output_shape, annotation, cropAnno, cropImg, stride):
origin_image = cv2.imread(imgpath)
height, width = origin_image.shape[:2]
domobj = xmldom.parse(annotation)
elementobj = domobj.documentElement
name = elementobj.getElementsByTagName("name")
size = len(name)
x = 0
newheight = output_shape
newwidth = output_shape
while x < width:
y = 0
if x + newwidth <= width:
while y < height:
head, objectlist = xml_parse.voc_xml_parse(annotation)
if y + newheight <= height:
hmin = y
hmax = y + newheight
wmin = x
wmax = x + newwidth
else:
hmin = height - newheight
hmax = height
wmin = x
wmax = x + newwidth
y = height
modify_head, modify_objectlist = crop_xml_modify(head, objectlist, hmin, wmin, newheight, newwidth,
origin_xml__path)
cropAnno1 = cropAnno + '_' + str(wmax) + '_' + str(hmax) + '_' + str(output_shape) + '.xml'
xml_parse.voc_xml_modify(cropAnno1, modify_head, modify_objectlist)
cropImg1 = cropImg + '_' + str(wmax) + '_' + str(hmax) + '_' + str(output_shape) + '.png'
cv2.imwrite(cropImg1, origin_image[hmin: hmax, wmin: wmax])
y = y + stride
if y + output_shape == height:
y = height
else:
while y < height:
head, objectlist = xml_parse.voc_xml_parse(annotation)
if y + newheight <= height:
hmin = y
hmax = y + newheight
wmin = width - newwidth
wmax = width
else:
hmin = height - newheight
hmax = height
wmin = width - newwidth
wmax = width
y = height
modify_head, modify_objectlist = crop_xml_modify(head, objectlist, hmin, wmin, newheight, newwidth,
origin_xml__path)
cropAnno1 = cropAnno + '_' + str(wmax) + '_' + str(hmax) + '_' + str(output_shape) + '.xml'
xml_parse.voc_xml_modify(cropAnno1, modify_head, modify_objectlist)
cropImg1 = cropImg + '_' + str(wmax) + '_' + str(hmax) + '_' + str(output_shape) + '.png'
cv2.imwrite(cropImg1, origin_image[hmin: hmax, wmin: wmax])
y = y + stride
x = width
x = x + stride
if x + output_shape == width:
x = width
if __name__ == '__main__':
output_shape = 2048
stride = int(output_shape / 2)
imgpath = './myimgs/'
annotation = './myxmls/'
cropAnno = './myoutxmls/'
cropImg = './myoutimgs/'
if not os.path.exists(cropImg):
os.mkdir(cropImg)
if not os.path.exists(cropAnno):
os.mkdir(cropAnno)
for each in tqdm(os.listdir(annotation)):
name = each.split('.')[0]
origin_img_path = os.path.join(imgpath, name + '.jpg')
origin_xml__path = os.path.join(annotation, name + '.xml')
crop_img_path = os.path.join(cropImg, name)
crop_xml__path = os.path.join(cropAnno, name)
crop_dataset(origin_img_path, output_shape, origin_xml__path, crop_xml__path, crop_img_path, stride)
8.删掉不要的裁剪图像和对应的xml.py
import os
import xml.etree.ElementTree as ET
import shutil
origin_ann_dir = './data/Annotations/'
new_ann_dir = './trashxml/'
origin_pic_dir = './data/images/'
new_pic_dir = './trashimg/'
k = 0
p = 0
q = 0
for dirpaths, dirnames, filenames in os.walk(origin_ann_dir):
for filename in filenames:
print("process...")
k = k + 1
print(k)
if os.path.isfile(r'%s%s' % (origin_ann_dir, filename)):
origin_ann_path = os.path.join(r'%s%s' % (origin_ann_dir, filename))
new_ann_path = os.path.join(r'%s%s' % (new_ann_dir, filename))
tree = ET.parse(origin_ann_path)
root = tree.getroot()
if len(root.findall('object')):
p = p + 1
else:
print(filename)
old_xml = origin_ann_dir + filename
new_xml = new_ann_dir + filename
old_pic = origin_pic_dir + filename.replace("xml", "jpg")
new_pic = new_pic_dir + filename.replace("xml", "jpg")
q = q + 1
shutil.move(old_pic, new_pic)
shutil.move(old_xml, new_xml)
print("ok, ", p)
print("empty, ", q)
9. 扣框或者中心裁剪.py
from __future__ import division
import os
from PIL import Image,ImageFile
import xml.dom.minidom
import numpy as np
ImageFile.LOAD_TRUNCATED_IMAGES = True
ImgPath = './Q/'
AnnoPath = './xml/'
ProcessedPath = './kuang/'
imagelist = os.listdir(ImgPath)
for image in imagelist:
image_pre, ext = os.path.splitext(image)
imgfile = ImgPath + image
print(imgfile)
if not os.path.exists(AnnoPath + image_pre + '.xml'): continue
xmlfile = AnnoPath + image_pre + '.xml'
DomTree = xml.dom.minidom.parse(xmlfile)
annotation = DomTree.documentElement
filenamelist = annotation.getElementsByTagName('filename')
objectlist = annotation.getElementsByTagName('object')
i = 1
for objects in objectlist:
namelist = objects.getElementsByTagName('code')
objectname = namelist[0].childNodes[0].data
savepath = ProcessedPath + objectname
if not os.path.exists(savepath):
os.makedirs(savepath)
bndbox = objects.getElementsByTagName('bndbox')
cropboxes = []
for box in bndbox:
x1_list = box.getElementsByTagName('xmin')
x1 = int(x1_list[0].childNodes[0].data)
y1_list = box.getElementsByTagName('ymin')
y1 = int(y1_list[0].childNodes[0].data)
x2_list = box.getElementsByTagName('xmax')
x2 = int(x2_list[0].childNodes[0].data)
y2_list = box.getElementsByTagName('ymax')
y2 = int(y2_list[0].childNodes[0].data)
w = x2 - x1
h = y2 - y1
obj = np.array([x1, y1, x2, y2])
shift = np.array([[1, 1, 1, 1]])
XYmatrix = np.tile(obj, (1, 1))
cropboxes = XYmatrix * shift
print(cropboxes)
img = Image.open(imgfile)
for cropbox in cropboxes:
cropedimg = img.crop(cropbox)
cropedimg.save(savepath + '/' + image_pre + '_' + str(i) + '.jpg')
i += 1
10. 根据xml计算目标长宽面积等.py
import os
import xml.etree.ElementTree as ET
import numpy as np
np.set_printoptions(suppress=True, threshold=10000000)
import matplotlib
from PIL import Image
def parse_obj(xml_path, filename):
tree = ET.parse(xml_path + filename)
objects = []
for obj in tree.findall('object'):
obj_struct = {
}
obj_struct['name'] = obj.find('name').text
bbox = obj.find('bndbox')
obj_struct['bbox'] = [int(bbox.find('xmin').text),
int(bbox.find('ymin').text),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
objects.append(obj_struct)
return objects
def read_image(image_path, filename):
im = Image.open(image_path + filename)
W = im.size[0]
H = im.size[1]
area = W * H
im_info = [W, H, area]
return im_info
if __name__ == '__main__':
image_path = '/VOCdevkit/VOC2007/JPEGImages/'
xml_path = '/VOCdevkit/VOC2007/Annotations/'
filenamess = os.listdir(xml_path)
filenames = []
for name in filenamess:
name = name.replace('.xml', '')
filenames.append(name)
print(filenames)
recs = {
}
ims_info = {
}
obs_shape = {
}
classnames = []
num_objs={
}
obj_avg = {
}
for i, name in enumerate(filenames):
print('正在处理 {}.xml '.format(name))
recs[name] = parse_obj(xml_path, name + '.xml')
print('正在处理 {}.jpg '.format(name))
ims_info[name] = read_image(image_path, name + '.jpg')
print('所有信息收集完毕。')
print('正在处理信息......')
for name in filenames:
im_w = ims_info[name][0]
im_h = ims_info[name][1]
im_area = ims_info[name][2]
for object in recs[name]:
if object['name'] not in num_objs.keys():
num_objs[object['name']] = 1
else:
num_objs[object['name']] += 1
ob_w = object['bbox'][2] - object['bbox'][0]
ob_h = object['bbox'][3] - object['bbox'][1]
ob_area = ob_w * ob_h
w_rate = ob_w / im_w
h_rate = ob_h / im_h
area_rate = ob_area / im_area
if not object['name'] in obs_shape.keys():
obs_shape[object['name']] = ([[ob_w,
ob_h,
ob_area,
w_rate,
h_rate,
area_rate]])
else:
obs_shape[object['name']].append([ob_w,
ob_h,
ob_area,
w_rate,
h_rate,
area_rate])
if object['name'] not in classnames:
classnames.append(object['name'])
for name in classnames:
obj_avg[name] = (np.array(obs_shape[name]).sum(axis=0)) / num_objs[name]
print('{}的情况如下:*******\n'.format(name))
print(' 目标平均W={}'.format(obj_avg[name][0]))
print(' 目标平均H={}'.format(obj_avg[name][1]))
print(' 目标平均area={}'.format(obj_avg[name][2]))
print(' 目标平均与原图的W比例={}'.format(obj_avg[name][3]))
print(' 目标平均与原图的H比例={}'.format(obj_avg[name][4]))
print(' 目标平均原图面积占比={}\n'.format(obj_avg[name][5]))
print('信息统计计算完毕。')
11. 删掉不符合分辨率的图像.py
from PIL import Image
import os
import sys
def get_urllist():
base = r'E:\Mysoft\trash\trash02\my\\'
list = os.listdir(base)
urllist = []
for i in list:
url = base + i
urllist.append(url)
return urllist
def get_imgSize(filename):
print(filename)
img = Image.open(filename)
imgSize = img.size
print(imgSize)
return imgSize
if __name__ == '__main__':
file_list = get_urllist()
print("test1")
for a in file_list:
try:
imgSize = get_imgSize(a)
maxSize = max(imgSize)
minSize = min(imgSize)
print(maxSize, minSize)
if (maxSize != 640 and minSize != 640 ):
os.remove(a)
print("已移除小于360x360分辨率的图片:%s" % a)
else:
pass
except:
os.remove(a)
12. 划分训练集测试集的图像和xml.py
import os
from shutil import copy2
image_original_path = "./img/"
label_original_path = "./anno/"
parent_path = os.path.dirname(os.getcwd())
train_image_path = os.path.join(parent_path, "trainimg/")
train_label_path = os.path.join(parent_path, "trainanno/")
test_image_path = os.path.join(parent_path, 'testimg/')
test_label_path = os.path.join(parent_path, 'testanno/')
def mkdir():
if not os.path.exists(train_image_path):
os.makedirs(train_image_path)
if not os.path.exists(train_label_path):
os.makedirs(train_label_path)
if not os.path.exists(test_image_path):
os.makedirs(test_image_path)
if not os.path.exists(test_label_path):
os.makedirs(test_label_path)
def main():
mkdir()
all_image = os.listdir(image_original_path)
print(all_image)
for i in range(len(all_image)):
if i % 8 != 0:
copy2(os.path.join(image_original_path, all_image[i]), train_image_path)
else:
copy2(os.path.join(image_original_path, all_image[i]), test_image_path)
all_label = os.listdir(label_original_path)
for i in range(len(all_label)):
if i % 8 != 0:
copy2(os.path.join(label_original_path, all_label[i]), train_label_path)
else:
copy2(os.path.join(label_original_path, all_label[i]), test_label_path)
if __name__ == '__main__':
main()