1,目标检测试集挑选,已经有VOC格式的JPEGImages,Annotations,和testval.txt列表
JPEGImages/62F1DBDB-4798-E8A5-4FA5-14C9E19ADA5B.jpg Annotations/62F1DBDB-4798-E8A5-4FA5-14C9E19ADA5B.xml
JPEGImages/2C042431-5CBE-9D69-646C-B5930204E15B.jpg Annotations/2C042431-5CBE-9D69-646C-B5930204E15B.xml
生成导出img和对应的label-C X1 Y1 W H
import os
import cv2
from PIL import Image
from random import randint
import xml.etree.ElementTree as ET
names = ["A","B","C"]
def drawRealBox(xml, w, h):
xmlfile=os.path.join("./Annotations", xml)
print xmlfile
txtfile=os.path.join("./labels", xml.replace('xml','txt'))
f = open(txtfile,'w')
tree = ET.parse(xmlfile)
objs = tree.findall('object')
for obj in objs:
box = obj.find('bndbox')
name = obj.find('name').text
x1=int(box.find('xmin').text)
y1=int(box.find('ymin').text)
x2=int(box.find('xmax').text)
y2=int(box.find('ymax').text)
cx = (x1)/float(w)
cy = (y1)/float(h)
cw = (x2-x1)/float(w)
ch = (y2-y1)/float(h)
index = names.index(name)
f.write("%d %f %f %f %f\n" % (index, cx,cy,cw,ch))
f.close()
allimg = os.listdir('./JPEGImages')
file_ynh = open("./testval.txt",'r')
lines = file_ynh.readlines()
for line in lines:
line.replace("\n","")
txtpath = line.split(' ')[0]
txtimg = txtpath.split('/')[-1]
jpg = cv2.imread(os.path.join('./JPEGImages', txtimg))
cv2.imwrite(os.path.join('./image', txtimg),jpg)
h,w_,C=jpg.shape
xml = txtimg.replace('jpg','xml')
drawRealBox(xml, w_, h)
file_ynh.close()
2.解析自定义的label,生成pathname+多标签格式的txt:
#-*-coding:utf-8-*-
import os
import cv2
import random
import shutil
import numpy
from PIL import Image
out0 ='''<?xml version="1.0" encoding="utf-8"?>
<annotation>
<folder>None</folder>
<filename>%(name)s</filename>
<source>
<database>None</database>
<annotation>None</annotation>
<image>None</image>
<flickrid>None</flickrid>
</source>
<owner>
<flickrid>None</flickrid>
<name>None</name>
</owner>
<segmented>0</segmented>
<size>
<width>%(width)d</width>
<height>%(height)d</height>
<depth>3</depth>
</size>
'''
out1 = ''' <object>
<name>%(class)s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>%(xmin)d</xmin>
<ymin>%(ymin)d</ymin>
<xmax>%(xmax)d</xmax>
<ymax>%(ymax)d</ymax>
</bndbox>
</object>
'''
out2 = '''</annotation>
'''
def yololabel(allimg):
def rect2rect(x1, y1, x2, y2, w, h):
px = float(x1 + x2) / 2 / w
py = float(y1 + y2) / 2 / h
pw = float(x2 - x1 + 1) / w
ph = float(y2 - y1 + 1) / h
return [px, py, pw, ph]
def getxywh(out):
xs = []
ys = []
for line in out:
points = line[0]
xs.append(points[0])
xs.append(points[2])
ys.append(points[1])
ys.append(points[3])
x1 = min(xs)
x2 = max(xs)
y1 = min(ys)
y2 = max(ys)
w = x2-x1+1
h = y2-y1+1
return [x1, y1, w, h]
def proberect(txt, w, h):
f = open(txt)
lines = f.readlines()
out = [] #为了截取的信息
boxout = [] #label信息
fxml = txt.replace('标注文本', 'TargetDetection/PointXML')
fxml = fxml.replace('.txt', '.xml')
fxml = open(fxml, 'w')
imgfile = txt.split('/')[-1]
source = {}
source['name'] = imgfile
source['width'] = w
source['height'] = h
fxml.write(out0 % source)
label = {}
for box in lines:
box = box.replace('\n', '')
box = box.split()
if box[0] != 'DP' and box[0] != 'JS':
print txt
raise RuntimeError('txt中分类标签不是DP也不是JS')
label['class'] = box[0]
# box = box[1].split(',')
copybox = box
box = box[1:5]
if box[0] == 'NaN' or box[1] is 'NaN' or box[2] is 'NaN' or box[3] is 'NaN':
continue
try:
x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
except Exception as e:
print txt # 有一个标签没加空格,这里会报错,就会知道是哪一个了
exit()
if int(box[0]) > int(box[2]) and int(box[1]) > int(box[3]):
label['xmin'] = x2
label['ymin'] = y2
label['xmax'] = x1
label['ymax'] = y1
box[0], box[1], box[2], box[3] = box[2], box[3], box[0], box[1]
elif int(box[0]) > int(box[2]) or int(box[1]) > int(box[3]):
print txt
raise RuntimeError('box的大小不是左上右下,也不是右下左上')
exit()
else:
label['xmin'] = x1
label['ymin'] = y1
label['xmax'] = x2
label['ymax'] = y2
fxml.write(out1 % label)
#out.append(rect2rect(x1, y1, x2, y2, w, h))
out.append([[x1, y1, x2, y2],copybox[0]])
boxout.append([copybox[0],copybox[5:]])
fxml.write(out2)
imgout = getxywh(out) # 为了计算每个图所有物体的占用区域
return out,imgout,boxout
def groberect(points, ww, hh):
x1 = points[0]
y1 = points[1]
x2 = points[2]
y2 = points[3]
w = x2 - x1 + 1
h = y2 - y1 + 1
px = float(x1 + x2) / 2
py = float(y1 + y2) / 2
w = w * 1.2
h = h * 1.2
l = max(0, px - w / 2)
r = min(ww - 1, px + w / 2)
t = max(0, py - h / 2)
b = min(hh - 1, py + h / 2)
# x1y1 x2y2
return [[int(l), int(t), int(r), int(b)], int(w), int(h)]
global imgroot
global pointroot
path1 = imgroot.replace('红绿灯图片', 'TargetDetection')
if not os.path.exists(path1):
os.makedirs(path1)
pathPointXML = path1 + '/' + 'PointXML'
if not os.path.exists(pathPointXML):
os.makedirs(pathPointXML)
path2 = imgroot.replace('红绿灯图片', 'TargetClassification')
if not os.path.exists(path2):
os.makedirs(path2)
pathDP = path2 + '/' + 'DP'
if not os.path.exists(pathDP):
os.makedirs(pathDP)
pathJS = path2 + '/' + 'JS'
if not os.path.exists(pathJS):
os.makedirs(pathJS)
pathFYB = path2 + '/' + 'FYB'
if not os.path.exists(pathFYB):
os.makedirs(pathFYB)
path3 = imgroot.replace('红绿灯图片', 'TargetAnalysis')
if not os.path.exists(path3):
os.makedirs(path3)
pathDP = path3 + '/' + 'DP'
if not os.path.exists(pathDP):
os.makedirs(pathDP)
pathJS = path3 + '/' + 'JS'
if not os.path.exists(pathJS):
os.makedirs(pathJS)
allimage = allimg.keys()
lenall = len(allimage)
num = 0
for im in allimage:
num += 1
print num," / ",lenall
if num ==1388:
aa=1
impath = os.path.join(imgroot, im + '.jpg')
img = cv2.imread(impath)
if img is None:
continue
h, w, _ = img.shape
labfile = os.path.join(pointroot, im + '.txt')
try:
lines,imgout,boxouts = proberect(labfile, w, h)
except:
continue
if len(lines) < 1:
continue
numi = 0
num_i = 0
for line in lines:
boxout = boxouts[num_i]
rect, ww, hh = groberect(line[0], w, h)
newjpgname_1 = allimg[im]+ '_' + str(numi) + '.jpg'
newjpgname_2 = allimg[im]+ '_' + str(numi+1) + '.jpg'
h1 = 0 if (rect[1]-5)<0 else rect[1]-5
h0 = 0 if (rect[0]-5)<0 else rect[0]-5
h3 = h if (rect[3]+5)>h else rect[3]+5
h2 = w if (rect[2]+5)>w else rect[2]+5
h11 = 0 if (rect[1]-3)<0 else rect[1]-3
h00 = 0 if (rect[0]-3)<0 else rect[0]-3
h33 = h if (rect[3]+3)>h else rect[3]+3
h22 = w if (rect[2]+3)>w else rect[2]+3
newimg1 = img[h1:h3,h0:h2].copy()
newimg2 = img[h11:h33,h00:h22].copy()
#newimg = img[rect[1]:rect[3],rect[0]:rect[2]].copy()
# 因为这是右闭区间
'''
newimg1 = img[rect[1]:rect[1]+hh, rect[0]:rect[0]+ww].copy()
ww = x2 - x1 + 1
hh = y2 - y1 + 1
'''
if line[1] == 'DP':
Obclass1 = os.path.join('./TargetClassification/DP/', newjpgname_1)
cv2.imwrite(Obclass1, newimg1)
Obclass2 = os.path.join('./TargetClassification/DP/', newjpgname_2)
cv2.imwrite(Obclass2, newimg2)
elif line[1] == 'JS':
Obclass1 = os.path.join('./TargetClassification/JS/', newjpgname_1)
cv2.imwrite(Obclass1, newimg1)
Obclass2 = os.path.join('./TargetClassification/JS/', newjpgname_2)
cv2.imwrite(Obclass2, newimg2)
else:
print impath
raise RuntimeError('txt中分类标签不是DP也不是JS')
exit()
newtxtname = Obclass1.replace('jpg','txt')
txtpath = newtxtname.replace('TargetClassification','TargetAnalysis')
labf = open(txtpath, 'w')
labf.write('%s ' % (boxout[0]))
str2list = " ".join(boxout[1])
labf.write('%s\n' % str2list)
labf.close()
newtxtname = Obclass2.replace('jpg','txt')
txtpath = newtxtname.replace('TargetClassification','TargetAnalysis')
labf = open(txtpath, 'w')
labf.write('%s ' % (boxout[0]))
str2list = " ".join(boxout[1])
labf.write('%s\n' % str2list)
labf.close()
while 1:
x = random.randint(1, w)
y = random.randint(1, h)
if( x > imgout[0] and x < (imgout[0]+imgout[2]-1) and y > imgout[1] and x < (imgout[1]+imgout[3]-1) ):
continue
cropImg = img[(y):(y + hh), (x):(x + ww)]
newjpgname = allimg[im] + '_' + str(num_i)+ '_X' + '.jpg'
cv2.imwrite(os.path.join('./TargetClassification/FYB/', newjpgname), cropImg)
break
numi += 2
num_i += 1
def listallfile_form_map(trainf, testf):
train = {}
test = {}
trf = open(trainf, 'r')
lines = trf.readlines()
for li in lines:
li = li.replace('\n','')
key,val = li.split(':')
train[key] = val
tef = open(testf, 'r')
lines = tef.readlines()
for li in lines:
li = li.replace('\n','')
key,val = li.split(':')
test[key] = val
return train, test
def saveremap(f, remap):
keys = remap.keys()
for k in keys:
f.write('%s:%s\n' % (k, remap[k]))
def listallfile(img, lab):
global rename
num = 0
train = {}
test = {}
laball = os.listdir(lab)
imgall = os.listdir(img)
for lab in laball:
if not lab.replace('txt', 'jpg') in imgall:
continue
newname = rename.format('%08d' % num)
if random.randint(0, 10) == 1:
test[lab.split('.')[0]] = newname
else:
train[lab.split('.')[0]] = newname
num += 1
return train, test
########### main #################
pointroot = './标注文本'
imgroot = './红绿灯图片'
rename = 'newTrain_2019_06_26_{}'
if os.path.exists('./train_remap.txt') and os.path.exists('./test_remap.txt'):
train, test = listallfile_form_map('./train_remap.txt', './test_remap.txt')
else:
train, test = listallfile(imgroot, pointroot)
f = open('./train_remap.txt', 'w')
saveremap(f, train)
f = open('./test_remap.txt', 'w')
saveremap(f, test)
yololabel(train)
yololabel(test)
########### main #################
import os
import cv2
#import ranmdom
import random
all_labels = os.listdir("./TargetAnalysis/DP")
all_images = os.listdir("./TargetClassification/DP")
light = ["UPP","OFF"]
color = ["RD","YL","GR","BL"]
typee = ["WZ","YP","LT","RT","UP","XX"]
name = "2019-06-28-"
num=0
def is_makedirs(dirs):
if not os.path.exists(dirs):
os.makedirs(dirs)
is_makedirs("./txt")
trainf = open("./txt/dp_train.txt", "w")
testf = open("./txt/dp_test.txt", "w")
cunimage_save = "./save_image"
is_makedirs(cunimage_save)
for image in all_images:
label = image.replace(".jpg",".txt")
label = os.path.join("./TargetAnalysis/DP", label)
img = cv2.imread("./TargetClassification/DP" + "/" + image)
print(image)
try:
img.shape
except:
print('fail to read xxx.jpg')
continue
labf = open(label,"r")
labs = labf.readlines()
for lab in labs:
labout = []
lab = lab.replace("\n","")
print(lab)
lab = lab.split(" ")
print(lab[0])
if (str(lab[0]).__str__()) in "JS":
continue
else:
lab = lab[1:]
lablight = [0,0,0]
print(lab[0])
lablight[0] = light.index(lab[0])
lablight[1] = light.index(lab[1])
lablight[2] = light.index(lab[2])
labout = labout+lablight
labcolor = [0,0,0,0]
light_color_1 = labcolor
light_color_1[color.index(lab[3])] = 1
labout = labout+light_color_1
light_color_2 = labcolor
light_color_2[color.index(lab[4])] = 1
labout = labout+light_color_2
light_color_3 = labcolor
light_color_3[color.index(lab[5])] = 1
labout = labout+light_color_3
labtype = [0,0,0,0,0,0]
light_type_1 = labtype
light_type_1[typee.index(lab[6])] = 1
labout = labout+light_type_1
light_type_2 = labtype
light_type_2[typee.index(lab[7])] = 1
labout = labout+light_type_2
light_type_3 = labtype
light_type_3[typee.index(lab[8])] = 1
labout = labout+light_type_3
num=num+1
print(labout)
jpgname = name+"{}.jpg".format("%08d" % num)
cv2.imwrite(os.path.join(cunimage_save, jpgname),img)
if random.randint(1,10) == 1:
f = testf
else:
f = trainf
f.write("./data/image/%s" % jpgname)
for i in labout:
f.write(" %d" % i)
f.write("\n")
trainf.close()
testf.close()
all_labels = os.listdir("./TargetAnalysis/JS")
all_images = os.listdir("./TargetClassification/JS")
trainf = open("./txt/js_train.txt", "w")
testf = open("./txt/js_test.txt", "w")
for image in all_images:
label = image.replace(".jpg",".txt")
label = os.path.join("./TargetAnalysis/JS", label)
img = cv2.imread("./TargetClassification/JS" + "/" + image)
print(image)
try:
img.shape
except:
print('fail to read xxx.jpg')
continue
labf = open(label,"r")
labs = labf.readlines()
for lab in labs:
labout = []
lab = lab.replace("\n","")
print(lab)
lab = lab.split(" ")
print(lab[0])
if (str(lab[0]).__str__()) in "JS":
lab = lab[1:]
lablight = [0]
print(lab[0])
lablight[0] = light.index(lab[0])
labout = labout+lablight
num=num+1
print(labout)
jpgname = name+"{}.jpg".format("%08d" % num)
cv2.imwrite(os.path.join(cunimage_save, jpgname),img)
if random.randint(1,10) == 1:
f = testf
else:
f = trainf
f.write("./data/image/%s" % jpgname)
for i in labout:
f.write(" %d" % i)
f.write("\n")
trainf.close()
testf.close()
all_images = os.listdir("./TargetClassification/FYB")
trainf = open("./txt/fyb_train.txt", "w")
testf = open("./txt/fyb_test.txt", "w")
for image in all_images:
img = cv2.imread("./TargetClassification/FYB" + "/" + image)
print(image)
try:
img.shape
except:
print('fail to read xxx.jpg')
continue
num=num+1
jpgname = name+"{}.jpg".format("%08d" % num)
if random.randint(1,10) == 1:
f = testf
else:
f = trainf
f.write("./data/image/%s" % jpgname)
f.write("\n")
trainf.close()
testf.close()
#-*-coding:utf-8-*-
import os
import cv2
import random
import shutil
import numpy
from PIL import Image
file1 = open('./txt/dp_train.txt','r')
file2 = open('./txt/dp_test.txt','r')
file3 = open('./txt/js_train.txt','r')
file4 = open('./txt/js_test.txt','r')
file5 = open('./txt/fyb_train.txt','r')
file6 = open('./txt/fyb_test.txt','r')
label_1 = file1.readlines()
label_2 = file2.readlines()
label_3 = file3.readlines()
label_4 = file4.readlines()
label_5 = file5.readlines()
label_6 = file6.readlines()
Vname = open('./txt/train_srn_1.txt','w')#test_srn
for lab in label_1:
lab = lab.replace('\n', '')
imgpath = lab.split(' ')[0]
imgname = imgpath.split('/')[-1]
labInfo = [int(i) for i in lab.split(' ')[1:]]
#灯盘 计时器 背景 计时器亮 计时器灭 灯盘灯1 灯盘灯2 灯盘灯3 灯盘灯1颜色红 灯盘灯1颜色黄 灯盘灯1颜色绿 灯盘灯1颜色黑 3+2+3+4
#灯盘灯2颜色红 灯盘灯2颜色黄 灯盘灯2颜色绿 灯盘灯2颜色黑 灯盘灯3颜色红 灯盘灯3颜色黄 灯盘灯3颜色绿 灯盘灯3颜色黑 4+4
#灯盘灯1未知(黑) 灯盘灯1上 灯盘灯1左 灯盘灯1右 灯盘灯1圆盘 灯盘灯1数字 6
#灯盘灯2未知(黑) 灯盘灯2上 灯盘灯2左 灯盘灯2右 灯盘灯2圆盘 灯盘灯2数字 6
#灯盘灯3未知(黑) 灯盘灯3上 灯盘灯3左 灯盘灯3右 灯盘灯3圆盘 灯盘灯3数字 6
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,1,0,0,\
255,255,\
labInfo[0],labInfo[1],labInfo[2],\
labInfo[3],labInfo[4],labInfo[5],labInfo[6],\
labInfo[7],labInfo[8],labInfo[9],labInfo[10],\
labInfo[11],labInfo[12],labInfo[13],labInfo[14],\
labInfo[15],labInfo[16],labInfo[17],labInfo[18],labInfo[19],labInfo[20],\
labInfo[21],labInfo[22],labInfo[23],labInfo[24],labInfo[25],labInfo[26],\
labInfo[27],labInfo[28],labInfo[29],labInfo[30],labInfo[31],labInfo[32]))
for lab in label_3:
lab = lab.replace('\n', '')
imgpath = lab.split(' ')[0]
imgname = imgpath.split('/')[-1]
labInfo = [int(i) for i in lab.split(' ')[1:]]
#灯盘 计时器 背景 计时器亮 计时器灭 灯盘灯1 灯盘灯2 灯盘灯3 灯盘灯1颜色红 灯盘灯1颜色黄 灯盘灯1颜色绿 灯盘灯1颜色黑 3+2+3+4
#灯盘灯2颜色红 灯盘灯2颜色黄 灯盘灯2颜色绿 灯盘灯2颜色黑 灯盘灯3颜色红 灯盘灯3颜色黄 灯盘灯3颜色绿 灯盘灯3颜色黑 4+4
#灯盘灯1未知(黑) 灯盘灯1上 灯盘灯1左 灯盘灯1右 灯盘灯1圆盘 灯盘灯1数字 6
#灯盘灯2未知(黑) 灯盘灯2上 灯盘灯2左 灯盘灯2右 灯盘灯2圆盘 灯盘灯2数字 6
#灯盘灯3未知(黑) 灯盘灯3上 灯盘灯3左 灯盘灯3右 灯盘灯3圆盘 灯盘灯3数字 6
if(labInfo[0]==1):
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,0,1,0,\
1,0,\
255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255))
else:
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,0,1,0,\
0,1,\
255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255))
for lab in label_5:
lab = lab.replace('\n', '')
imgname = lab.split('/')[-1]
#灯盘 计时器 背景 计时器亮 计时器灭 灯盘灯1 灯盘灯2 灯盘灯3 灯盘灯1颜色红 灯盘灯1颜色黄 灯盘灯1颜色绿 灯盘灯1颜色黑 3+2+3+4
#灯盘灯2颜色红 灯盘灯2颜色黄 灯盘灯2颜色绿 灯盘灯2颜色黑 灯盘灯3颜色红 灯盘灯3颜色黄 灯盘灯3颜色绿 灯盘灯3颜色黑 4+4
#灯盘灯1未知(黑) 灯盘灯1上 灯盘灯1左 灯盘灯1右 灯盘灯1圆盘 灯盘灯1数字 6
#灯盘灯2未知(黑) 灯盘灯2上 灯盘灯2左 灯盘灯2右 灯盘灯2圆盘 灯盘灯2数字 6
#灯盘灯3未知(黑) 灯盘灯3上 灯盘灯3左 灯盘灯3右 灯盘灯3圆盘 灯盘灯3数字 6
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,0,0,1,\
255,255,\
255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255))
Vname.close()
Vname = open('./txt/test_srn_1.txt','w')
for lab in label_2:
lab = lab.replace('\n', '')
imgpath = lab.split(' ')[0]
imgname = imgpath.split('/')[-1]
labInfo = [int(i) for i in lab.split(' ')[1:]]
#灯盘 计时器 背景 计时器亮 计时器灭 灯盘灯1 灯盘灯2 灯盘灯3 灯盘灯1颜色红 灯盘灯1颜色黄 灯盘灯1颜色绿 灯盘灯1颜色黑 3+2+3+4
#灯盘灯2颜色红 灯盘灯2颜色黄 灯盘灯2颜色绿 灯盘灯2颜色黑 灯盘灯3颜色红 灯盘灯3颜色黄 灯盘灯3颜色绿 灯盘灯3颜色黑 4+4
#灯盘灯1未知(黑) 灯盘灯1上 灯盘灯1左 灯盘灯1右 灯盘灯1圆盘 灯盘灯1数字 6
#灯盘灯2未知(黑) 灯盘灯2上 灯盘灯2左 灯盘灯2右 灯盘灯2圆盘 灯盘灯2数字 6
#灯盘灯3未知(黑) 灯盘灯3上 灯盘灯3左 灯盘灯3右 灯盘灯3圆盘 灯盘灯3数字 6
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,1,0,0,\
255,255,\
labInfo[0],labInfo[1],labInfo[2],\
labInfo[3],labInfo[4],labInfo[5],labInfo[6],\
labInfo[7],labInfo[8],labInfo[9],labInfo[10],\
labInfo[11],labInfo[12],labInfo[13],labInfo[14],\
labInfo[15],labInfo[16],labInfo[17],labInfo[18],labInfo[19],labInfo[20],\
labInfo[21],labInfo[22],labInfo[23],labInfo[24],labInfo[25],labInfo[26],\
labInfo[27],labInfo[28],labInfo[29],labInfo[30],labInfo[31],labInfo[32]))
for lab in label_4:
lab = lab.replace('\n', '')
imgpath = lab.split(' ')[0]
imgname = imgpath.split('/')[-1]
labInfo = [int(i) for i in lab.split(' ')[1:]]
#灯盘 计时器 背景 计时器亮 计时器灭 灯盘灯1 灯盘灯2 灯盘灯3 灯盘灯1颜色红 灯盘灯1颜色黄 灯盘灯1颜色绿 灯盘灯1颜色黑 3+2+3+4
#灯盘灯2颜色红 灯盘灯2颜色黄 灯盘灯2颜色绿 灯盘灯2颜色黑 灯盘灯3颜色红 灯盘灯3颜色黄 灯盘灯3颜色绿 灯盘灯3颜色黑 4+4
#灯盘灯1未知(黑) 灯盘灯1上 灯盘灯1左 灯盘灯1右 灯盘灯1圆盘 灯盘灯1数字 6
#灯盘灯2未知(黑) 灯盘灯2上 灯盘灯2左 灯盘灯2右 灯盘灯2圆盘 灯盘灯2数字 6
#灯盘灯3未知(黑) 灯盘灯3上 灯盘灯3左 灯盘灯3右 灯盘灯3圆盘 灯盘灯3数字 6
if(labInfo[0]==1):
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,0,1,0,\
1,0,\
255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255))
else:
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,0,1,0,\
0,1,\
255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255))
for lab in label_6:
lab = lab.replace('\n', '')
imgname = lab.split('/')[-1]
#灯盘 计时器 背景 计时器亮 计时器灭 灯盘灯1 灯盘灯2 灯盘灯3 灯盘灯1颜色红 灯盘灯1颜色黄 灯盘灯1颜色绿 灯盘灯1颜色黑 3+2+3+4
#灯盘灯2颜色红 灯盘灯2颜色黄 灯盘灯2颜色绿 灯盘灯2颜色黑 灯盘灯3颜色红 灯盘灯3颜色黄 灯盘灯3颜色绿 灯盘灯3颜色黑 4+4
#灯盘灯1未知(黑) 灯盘灯1上 灯盘灯1左 灯盘灯1右 灯盘灯1圆盘 灯盘灯1数字 6
#灯盘灯2未知(黑) 灯盘灯2上 灯盘灯2左 灯盘灯2右 灯盘灯2圆盘 灯盘灯2数字 6
#灯盘灯3未知(黑) 灯盘灯3上 灯盘灯3左 灯盘灯3右 灯盘灯3圆盘 灯盘灯3数字 6
Vname.write('./data/Red_Yellow_Green/images/%s %d %d %d \
%d %d \
%d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d \
%d %d %d %d %d %d\n' % (imgname,0,0,1,\
255,255,\
255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255,\
255,255,255,255,255,255))
Vname.close()
file1.close()
file2.close()
file3.close()
file4.close()
file5.close()
file6.close()
train_file = open('./txt/train_srn_1.txt','r')#test_srn
test_file = open('./txt/test_srn_1.txt','r')
shuffle_f_train = open('./txt/train_srn.txt','w')#test_srn
shuffle_f_test = open('./txt/test_srn.txt','w')
import random
train_lines = train_file.readlines()
test_lines = test_file.readlines()
random.shuffle(train_lines)
random.shuffle(test_lines)
shuffle_f_train.writelines(train_lines)
shuffle_f_test.writelines(test_lines)
shuffle_f_train.close()
shuffle_f_test.close()
train_file.close()
test_file.close()