赞
踩
In [1]
- #解压数据集
- !ls /home/aistudio/data
- !unzip -q /home/aistudio/data/data249593/datasets.zip
- !mv datasets VOCdata
- # 将数据集移动到工作区
- !mv /home/aistudio/VOCdata /home/aistudio/work/
- %cd /home/aistudio/work/VOCdata/
- # 重命名图片文件夹
- !mv JPEGImages images
data249593 /home/aistudio/work/VOCdata
In [ ]
!unzip test.zip
In [2]
- %cd /home/aistudio/work/VOCdata/
- import os
- import random
- trainval_percent = 0.1
- train_percent = 0.9
- xmlfilepath = '/home/aistudio/work/VOCdata/Annotations' #xml文件存放地址
- if not os.path.exists('ImageSets/'):
- os.makedirs('ImageSets/')
-
- 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/trainval.txt', 'w')
- ftest = open('ImageSets/test.txt', 'w')
- ftrain = open('ImageSets/train.txt', 'w')
- fval = open('ImageSets/val.txt', 'w')
- for i in list:
- name = total_xml[i][:-4] + '\n'
- if i in trainval:
- ftrainval.write(name)
- if i in train:
- ftest.write(name)
- else:
- fval.write(name)
- else:
- ftrain.write(name)
- ftrainval.close()
- ftrain.close()
- fval.close()
- ftest.close()
/home/aistudio/work/VOCdata
In [3]
- %cd /home/aistudio/work/VOCdata/
- import xml.etree.ElementTree as ET
- import pickle
- import os
- from os import listdir, getcwd
- from os.path import join
- sets = ['train', 'test','val']
- Imgpath = '/home/aistudio/work/VOCdata/images' #图片文件夹
- xmlfilepath = '/home/aistudio/work/VOCdata/Annotations/' #xml文件存放地址
- ImageSets_path='ImageSets/'
- classes = ['cat','dog'] # 识别目标种类
- def convert(size, box):
- dw = 1. / size[0]
- dh = 1. / size[1]
- x = (box[0] + box[1]) / 2.0
- y = (box[2] + box[3]) / 2.0
- 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(image_id):
- in_file = open(xmlfilepath+'%s.xml' % (image_id))
- out_file = open('labels/%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 &#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。