1、图片重命名->rename.py
import os
#import cv2
import shutil
jpg_path = '/media/em/data_1/0716/data2/socket/mark'
xml_path = '/media/em/data_1/0716/data2/socket/xml/'
out_jpg_path = '/media/em/data_1/0716/data2/socket/JPEGImages1/'
out_xml_path = '/media/em/data_1/0716/data2/socket/Annotations1/'
if not os.path.exists(out_jpg_path):
os.mkdir(out_jpg_path)
print(out_jpg_path + '----创建成功')
if not os.path.exists(out_xml_path):
os.mkdir(out_xml_path)
print(out_xml_path + '----创建成功')
list_jpg = os.listdir(jpg_path)
#list_xml =os.listdir(xml_path)
#i = len(list_jpg)
i = 20000
for list_jpgs in list_jpg:
total_num_fil = len(list_jpg)
#no_houzhui = list_jpgs.split('.')[0]
no_houzhui = os.path.splitext(list_jpgs)[0]
print(no_houzhui)
#print(tmp[0])
jpg_name = jpg_path + '/' + str(no_houzhui) + '.jpg'
xml_name = xml_path + str(no_houzhui) + '.xml'
dst_jpg = out_jpg_path + str(i) + '.jpg'
shutil.copy(jpg_name, dst_jpg)
#os.rename(jpg_name,dst_jpg)
dst_xml = out_xml_path + str(i) + '.xml'
print(jpg_name)
shutil.copy(xml_name, dst_xml)
#os.rename(xml_name,dst_xml)
i=i+1
print(i)
print("finish!")
2、数据规范性检查->xml_jpg
# -*- coding: utf-8 -*-
#根据单张图片的xml文件,在图片上标注出目标
import xml.etree.ElementTree as ET
import os
import cv2
jpg_path = '/media/em/data_1/0716/data2/socket/JPEGImages1'
xml_path = '/media/em/data_1/0716/data2/socket/Annotations1'
list_jpg = os.listdir(jpg_path)
list_xml = os.listdir(xml_path)
color = (4, 250, 7)
for list_jpgs in list_jpg:
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
print(list_jpgs)
no_houzhui = os.path.splitext(list_jpgs)[0]
xml_file = xml_path + '/' + no_houzhui + '.xml'
tree = ET.parse(xml_file)
root = tree.getroot()
jpg_file = jpg_path + '/' + no_houzhui +'.jpg'
im = cv2.imread(jpg_file)
print(im.shape)
w = im.shape[0]
h = im.shape[1]
for object in root.findall('object'):
object_name = object.find('name').text
Xmin = int(object.find('bndbox').find('xmin').text)
Ymin = int(object.find('bndbox').find('ymin').text)
Xmax = int(object.find('bndbox').find('xmax').text)
Ymax = int(object.find('bndbox').find('ymax').text)
if(Xmin < 0 or Xmax >= h or Ymin < 0 or Ymax >= w or Xmin >= Xmax or Ymin >= Ymax):
print('Xmin---Xmax---Ymin---Ymax')
print(Xmin,Xmax,Ymin,Ymax)
print("data error!!!!")
cv2.rectangle(im, (Xmin, Ymin), (Xmax, Ymax), color, 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(im, object_name, (Xmin, Ymin - 7), font, 0.5, (6, 230, 230), 2)
dst = cv2.resize(im, (640, 480))
cv2.imshow('01', dst)
cv2.waitKey(0)
continue
else:
cv2.rectangle(im, (Xmin, Ymin), (Xmax, Ymax), color, 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(im, object_name, (Xmin, Ymin - 7), font, 0.5, (6, 230, 230), 2)
dst = cv2.resize(im, (640, 480))
cv2.imshow('01', dst)
#cv2.waitKey()
cv2.waitKey(0)
3、yolo数据集划分与label生成
3.1、train.txt,trainval.txt,test.txt,val.txt生成
import os
import random
import sys
#if len(sys.argv) < 2:
# print("no directory specified, please input target directory")
# exit()
#root_path = sys.argv[1]
root_path = '/media/em/data_1/0716/VOCdevkit/VOC2007'
xmlfilepath = root_path + '/Annotations'
txtsavepath = root_path + '/ImageSets/Main'
if not os.path.exists(root_path):
print("cannot find such directory: " + root_path)
exit()
if not os.path.exists(txtsavepath):
os.makedirs(txtsavepath)
trainval_percent = 0.9
train_percent = 0.8
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)
print("train and val size:", tv)
print("train size:", tr)
ftrainval = open(txtsavepath + '/trainval.txt', 'w')
ftest = open(txtsavepath + '/test.txt', 'w')
ftrain = open(txtsavepath + '/train.txt', 'w')
fval = open(txtsavepath + '/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()
3.2、yolov4的label和数据集划分
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 = ["background_waiguan_obj", "chetou", "chewei",
"chepai", "chebiao", "qianzhudeng", "qianfudeng",
"houzhudeng", "jiaotaban", "xinglijia", "lungu", "tiehua",
"fanghuzhuangzhi", "weiyi", "toukui_mtc", "paiqikong", "cheshen",
"houshijing", "NUM_T", "socket", "luntai", "chelian","sanjiaojia", "fanguangbeixin"]
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('/media/em/data_1/0716/VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
out_file = open('/media/em/data_1/0716/VOCdevkit/VOC%s/labels/%s.txt'%(year, 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('/media/em/data_1/0716/VOCdevkit/VOC%s/labels/'%(year)):
os.makedirs('/media/em/data_1/0716/VOCdevkit/VOC%s/labels/'%(year))
image_ids = open('/media/em/data_1/0716/VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
list_file = open('/media/em/data_1/0716/VOCdevkit/%s_%s.txt'%(year, image_set), 'w')
for image_id in image_ids:
list_file.write('/media/em/data_1/0716/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%( year, image_id))
convert_annotation(year, image_id)
list_file.close()
print('finish!')
#os.system("cat 2007_train.txt 2007_test.txt > train.txt")
#os.system("cat 2007_train.txt 2007_val.txt 2007_test.txt > train.all.txt")
至此yolov4生成2007_train.txt,2007_test.txt,2007_val.txt和labels(文件夹),数据集制作完成