-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjyzprocess_yolo3.py
84 lines (83 loc) · 3.57 KB
/
jyzprocess_yolo3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -- coding: utf-8 --
# 这个脚本是用来处理从一串绝缘子中截取单个绝缘子片的
# 作者:叶帆帆
# 创建时间,2018年10月19日
import os
from PIL import Image
import xml.dom.minidom
import numpy as np
from tqdm import tqdm
import shutil
ImgPath = '/home/tanghmn/YFF/jyzdata/jyzimgs'
AnnoPath = '/home/tanghmn/YFF/jyzdata/jyzxmls'
ProcessedPath = '/Volumes/Seagate Backup Plus Drive/aligned_noPS/classone_crop'
trainval = '/home/tanghmn/YFF/jyzdata/trainval.txt'
isclassone = 0
if __name__ == '__main__':
##如果保存图片的目录不存在就创建
# if not os.path.exists(ProcessedPath):
# os.makedirs(ProcessedPath)
imagelist = os.listdir(ImgPath)
f1 = open(trainval, 'w')
##按照图片列表进行遍历
for image in tqdm(imagelist):
if image.find('classone') != -1:
isclassone = 1
else:
isclassone = 0
# print(image)
img_pre, ext = os.path.splitext(image)
imgfile = os.path.join(ImgPath, image)
xmlfile = os.path.join(AnnoPath, img_pre + '.xml')
# print(xmlfile)
##判断该图片是否存在对应的xml文件,如果不存在就跳过
if not os.path.exists(xmlfile):
continue
DomTree = xml.dom.minidom.parse(xmlfile)
annotation = DomTree.documentElement
filenameList = annotation.getElementsByTagName('filename')
filename = filenameList[0].childNodes[0].data
objectlist = annotation.getElementsByTagName('object')
##定义一个列表,存储每个目标的边框信息
cropboxes = []
for obj in objectlist:
namelist = obj.getElementsByTagName('name')
objectname = namelist[0].childNodes[0].data
bndbox = obj.getElementsByTagName('bndbox')
##获取点的坐标
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
box_cor = np.array([x1, y1, x2, y2])
cropboxes.append(box_cor)
##打开图片进行裁剪操作
current_img = Image.open(imgfile)
width, height = current_img.size
# 定义目标index
idx = 0
if len(cropboxes) > 0:
f1.write(ImgPath + '/' + image+'\n')
with open('/home/tanghmn/YFF/jyzdata/jyzlabels/' + image[:-4] + '.txt', 'w') as f2:
for cropbox in cropboxes:
# print 'cropbox:',cropbox
minX = max(0, cropbox[0])/width
minY = max(0, cropbox[1])/height
maxX = min(cropbox[2], width)/width
maxY = min(cropbox[3], height)/height
# print(minX, minY, maxX, maxY)
f2.write(str(isclassone) + ' ' + str(minX) + ' ' + str(minY) + ' ' + str(maxX-minX) + ' ' + str(maxY-minY) +'\n')
# cropbox = (minX, minY, maxX, maxY)
# print(cropbox)
# cropedimg = current_img.crop(cropbox)
# cropedimg.save(ProcessedPath + '/' + img_pre + '_' + str(idx) + '.jpg')
# idx += 1
f2.close()
f1.write('\n')