-
Notifications
You must be signed in to change notification settings - Fork 39
/
mirror.py
141 lines (121 loc) · 4.33 KB
/
mirror.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import cv2
import math
import numpy as np
import os
import glob
import json
import shutil
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ElementTree, Element
def h_MirrorImg(img_path,img_write_path):
img = cv2.imread(img_path)
mirror_img = cv2.flip(img, 1) #
cv2.imwrite(img_write_path,mirror_img)
def v_MirrorImg(img_path,img_write_path):
img = cv2.imread(img_path)
mirror_img = cv2.flip(img, 0) #
cv2.imwrite(img_write_path,mirror_img)
def a_MirrorImg(img_path,img_write_path):
img = cv2.imread(img_path)
mirror_img = cv2.flip(img, -1) #
cv2.imwrite(img_write_path,mirror_img)
def h_MirrorAnno(anno_path,anno_write_path):
tree = ET.parse(anno_path)
root = tree.getroot()
size=root.find('size')
w=int(size.find('width').text)
objects = root.findall("object")
for obj in objects:
bbox = obj.find('bndbox')
x1 = float(bbox.find('xmin').text)
x2 = float(bbox.find('xmax').text)
x1=w-x1+1
x2=w-x2+1
assert x1>0
assert x2>0
bbox.find('xmin').text=str(int(x2))
bbox.find('xmax').text=str(int(x1))
tree.write(anno_write_path) # 保存修改后的XML文件
def v_MirrorAnno(anno_path,anno_write_path):
tree = ET.parse(anno_path)
root = tree.getroot()
size = root.find('size')
h=int(size.find('height').text)
objects = root.findall("object")
for obj in objects:
bbox = obj.find('bndbox')
y1 = float(bbox.find('ymin').text)
y2 = float(bbox.find('ymax').text)
y1=h-y1+1
y2=h-y2+1
assert y1>0
assert y2>0
bbox.find('ymin').text=str(int(y2))
bbox.find('ymax').text=str(int(y1))
tree.write(anno_write_path) # 保存修改后的XML文件
def a_MirrorAnno(anno_path,anno_write_path):
tree = ET.parse(anno_path)
root = tree.getroot()
size = root.find('size')
w=int(size.find('width').text)
h = int(size.find('height').text)
objects = root.findall("object")
for obj in objects:
bbox = obj.find('bndbox')
x1 = float(bbox.find('xmin').text)
y1 = float(bbox.find('ymin').text)
x2 = float(bbox.find('xmax').text)
y2 = float(bbox.find('ymax').text)
x1=w-x1+1
x2=w-x2+1
y1 = h - y1+1
y2 = h - y2+1
assert x1 > 0
assert x2 > 0
assert y1 > 0
assert y2 > 0
bbox.find('xmin').text=str(int(x2))
bbox.find('xmax').text=str(int(x1))
bbox.find('ymin').text=str(int(y2))
bbox.find('ymax').text=str(int(y1))
tree.write(anno_write_path) # 保存修改后的XML文件
def mirror(img_dir,anno_dir,img_write_dir,anno_write_dir):
if not os.path.exists(img_write_dir):
os.makedirs(img_write_dir)
if not os.path.exists(anno_write_dir):
os.makedirs(anno_write_dir)
img_names=os.listdir(img_dir)
for img_name in img_names:
img_path=os.path.join(img_dir,img_name)
h_img_write_path=os.path.join(img_write_dir,img_name[:-4]+'h'+'.jpg')
anno_path=os.path.join(anno_dir,img_name[:-4]+'.xml')
h_anno_write_path = os.path.join(anno_write_dir, img_name[:-4]+'h'+'.xml')
#
v_img_write_path = os.path.join(img_write_dir, img_name[:-4] + 'v' + '.jpg')
v_anno_write_path = os.path.join(anno_write_dir, img_name[:-4] + 'v' + '.xml')
#
a_img_write_path = os.path.join(img_write_dir, img_name[:-4] + 'a' + '.jpg')
a_anno_write_path = os.path.join(anno_write_dir, img_name[:-4] + 'a' + '.xml')
#
h_MirrorImg(img_path,h_img_write_path)
v_MirrorImg(img_path,v_img_write_path)
a_MirrorImg(img_path, a_img_write_path)
h_MirrorAnno(anno_path,h_anno_write_path)
v_MirrorAnno(anno_path, v_anno_write_path)
a_MirrorAnno(anno_path, a_anno_write_path)
img_dir='several/JPEGImages'
anno_dir='several/Annotations'
img_write_dir='Mirror/mirrored_JPEGImages'
anno_write_dir='Mirror/mirrored_Annotations'
mirror(img_dir,anno_dir,img_write_dir,anno_write_dir)
'''
imga=cv2.imread('14a.jpg')
imgh=cv2.imread('14h.jpg')
imgv=cv2.imread('14v.jpg')
cv2.rectangle(imga,(606,898),(855,1432),(0,255,0),4)
cv2.rectangle(imgh,(606,489),(855,1023),(0,255,0),4)
cv2.rectangle(imgv,(32,898),(281,1432),(0,255,0),4)
cv2.imwrite('a.jpg',imga)
cv2.imwrite('v.jpg',imgv)
cv2.imwrite('h.jpg',imgh)
'''