Skip to content

Commit 6a9fe90

Browse files
committed
Feat: enhance feature; Fix: bug for x,y
1 parent 1591c8a commit 6a9fe90

File tree

4 files changed

+151
-12
lines changed

4 files changed

+151
-12
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Resizing/Origin/
2222
Resizing/Resized/
2323

2424

25+
# large files in Enhancing/
26+
Enhancing/Origin/
27+
Enhancing/Enhanced/
28+
29+
2530
# large files in Origin_Dataset/
2631
Origin_Dataset/
2732

Enhancing/sync_enhance.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# coding=utf-8
2+
3+
"""
4+
Labeling_Tool
5+
Resize pic to defined size.
6+
7+
__author__ = 'JNingWei'
8+
"""
9+
10+
import os
11+
import cv2
12+
13+
14+
def dst_check(dst):
15+
import shutil
16+
try:
17+
shutil.rmtree(dst)
18+
except OSError:
19+
pass
20+
os.makedirs(dst)
21+
22+
23+
def get_path_lists(src):
24+
check_suffix = lambda x : True if os.path.splitext(x)[1] in [".jpg", ".JPG", ".png", ".PNG"] else False
25+
src_image_paths = [os.path.join(src, name) for name in os.listdir(src) if check_suffix(name)]
26+
assert len(src_image_paths)
27+
src_image_paths.sort()
28+
src_label_paths = [path.replace(os.path.splitext(path)[1], ".txt") for path in src_image_paths]
29+
dst_image_paths = [path.replace("Origin", "Enhanced").replace(os.path.splitext(path)[1], os.path.splitext(path)[1].lower()) for path in src_image_paths]
30+
dst_label_paths = [path.replace("Origin", "Enhanced").replace(os.path.splitext(path)[1], ".txt") for path in src_image_paths]
31+
return src_image_paths, src_label_paths, dst_image_paths, dst_label_paths
32+
33+
34+
def get_image_size(image_path):
35+
image = cv2.imread(image_path)
36+
h, w = image.shape[:2]
37+
h_max, w_max = h - 1, w - 1
38+
return h_max, w_max
39+
40+
41+
def enhance_image(src_image_path, dst_image_path, h_flip, v_flip, hv_flip):
42+
image = cv2.imread(src_image_path)
43+
if True:
44+
# Original image 原图像
45+
cv2.imwrite(os.path.splitext(dst_image_path)[0]+"-o"+os.path.splitext(dst_image_path)[1], image)
46+
if h_flip:
47+
# Flipped Horizontally 图像水平翻转
48+
h_flip = cv2.flip(image, 1)
49+
cv2.imwrite(os.path.splitext(dst_image_path)[0]+"-h"+os.path.splitext(dst_image_path)[1], h_flip)
50+
if v_flip:
51+
# Flipped Vertically 图像垂直翻转
52+
v_flip = cv2.flip(image, 0)
53+
cv2.imwrite(os.path.splitext(dst_image_path)[0]+"-v"+os.path.splitext(dst_image_path)[1], v_flip)
54+
if hv_flip:
55+
# Flipped Horizontally & Vertically 图像水平垂直翻转
56+
hv_flip = cv2.flip(image, -1)
57+
cv2.imwrite(os.path.splitext(dst_image_path)[0]+"-hv"+os.path.splitext(dst_image_path)[1], hv_flip)
58+
59+
60+
def enhance_label(src_label_path, dst_label_path, h_flip, v_flip, h_v_flip, h, w):
61+
r_file = open(src_label_path, "r")
62+
lines = r_file.readlines()
63+
_, messages = lines[0], lines[1:]
64+
r_file.close()
65+
if True:
66+
# Original label
67+
o_file = open(os.path.splitext(dst_label_path)[0]+"-o"+os.path.splitext(dst_label_path)[1], "w")
68+
o_file.writelines(lines[0])
69+
o_file.writelines(messages)
70+
o_file.close()
71+
if h_flip:
72+
# Flipped Horizontally 水平翻转
73+
h_file = open(os.path.splitext(dst_label_path)[0]+"-h"+os.path.splitext(dst_label_path)[1], "w")
74+
h_file.writelines(lines[0])
75+
for mess in messages:
76+
if mess.split():
77+
x1, y1, x2, y2 = map(int, mess.split())
78+
x1_new = w - x1
79+
y1_new = y1
80+
x2_new = w - x2
81+
y2_new = y2
82+
new_mess = "{0} {1} {2} {3}\n".format(x1_new, y1_new, x2_new, y2_new)
83+
h_file.writelines(new_mess)
84+
h_file.close()
85+
if v_flip:
86+
# Flipped Vertically 垂直翻转
87+
v_file = open(os.path.splitext(dst_label_path)[0]+"-v"+os.path.splitext(dst_label_path)[1], "w")
88+
v_file.writelines(lines[0])
89+
for mess in messages:
90+
if mess.split():
91+
x1, y1, x2, y2 = map(int, mess.split())
92+
x1_new = x1
93+
y1_new = h - y1
94+
x2_new = x2
95+
y2_new = h - y2
96+
new_mess = "{0} {1} {2} {3}\n".format(x1_new, y1_new, x2_new, y2_new)
97+
v_file.writelines(new_mess)
98+
v_file.close()
99+
if hv_flip:
100+
# Flipped Horizontally & Vertically 水平垂直翻转
101+
hv_file = open(os.path.splitext(dst_label_path)[0]+"-hv"+os.path.splitext(dst_label_path)[1], "w")
102+
hv_file.writelines(lines[0])
103+
for mess in messages:
104+
if mess.split():
105+
x1, y1, x2, y2 = map(int, mess.split())
106+
x1_new = w - x1
107+
y1_new = h - y1
108+
x2_new = w - x2
109+
y2_new = h - y2
110+
new_mess = "{0} {1} {2} {3}\n".format(x1_new, y1_new, x2_new, y2_new)
111+
hv_file.writelines(new_mess)
112+
hv_file.close()
113+
114+
115+
def main(src, dst, h_flip, v_flip, hv_flip):
116+
dst_check(dst)
117+
src_image_paths, src_label_paths, dst_image_paths, dst_label_paths = get_path_lists(src)
118+
for (src_image_path, src_label_path, dst_image_path, dst_label_path) in zip(src_image_paths, src_label_paths, dst_image_paths, dst_label_paths):
119+
h_max, w_max = get_image_size(src_image_path)
120+
enhance_image(src_image_path, dst_image_path, h_flip, v_flip, hv_flip)
121+
enhance_label(src_label_path, dst_label_path, h_flip, v_flip, hv_flip, h_max, w_max)
122+
123+
124+
if __name__ == "__main__":
125+
SRC = './Origin' # dir for origin pics
126+
DST = './Enhanced' # dir for resized pics
127+
h_flip = True # horizontal
128+
v_flip = True # vertical
129+
hv_flip = True # both horizontal and vertical
130+
main(SRC, DST, h_flip, v_flip, hv_flip)

Resizing/image_resize.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828

2929
# main operation
3030
import cv2
31-
for i, origin_pic in enumerate(origin_pics):
31+
# for i, origin_pic in enumerate(origin_pics):
32+
# img = cv2.imread(origin_pic)
33+
# resized_pic = cv2.resize(img, new_size)
34+
# cv2.imwrite('{}/{:>03d}.jpg'.format(dst, i+1), resized_pic)
35+
for origin_pic in origin_pics:
3236
img = cv2.imread(origin_pic)
3337
resized_pic = cv2.resize(img, new_size)
34-
cv2.imwrite('{}/{:>03d}.jpg'.format(dst, i+1), resized_pic)
38+
cv2.imwrite(origin_pic.replace(src, dst), resized_pic)

Resizing/sync_resize.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ def dst_check(dst):
1919
pass
2020
os.makedirs(dst)
2121

22-
def resize_location(x, scale, min, max):
22+
def resize_location(value, scale, min, max):
2323
# 类型检查
24-
x = int(x)
24+
value = int(value)
2525
# 尺寸缩放
26-
x = int(round(float(x) * scale))
26+
value = int(round(float(value) * scale))
2727
# 边界溢出检查
28-
x = min if x < min else x
29-
x = max if x > max else x
30-
return x
28+
value = min if value < min else value
29+
value = max if value > max else value
30+
return value
3131

3232

3333
def get_path_lists(src):
@@ -62,12 +62,12 @@ def resize_label(src_label_path, dst_label_path, origin_size, new_size, idx):
6262
w_file.writelines(lines[0])
6363
for mess in messages:
6464
if mess.split():
65-
y1, x1, y2, x2 = mess.split()
66-
y1_new = resize_location(y1, scale_h, min=0, max=new_h-1)
65+
x1, y1, x2, y2 = mess.split()
6766
x1_new = resize_location(x1, scale_w, min=0, max=new_w-1)
68-
y2_new = resize_location(y2, scale_h, min=0, max=new_h-1)
67+
y1_new = resize_location(y1, scale_h, min=0, max=new_h-1)
6968
x2_new = resize_location(x2, scale_w, min=0, max=new_w-1)
70-
new_mess = "{0} {1} {2} {3}\n".format(y1_new, x1_new, y2_new, x2_new)
69+
y2_new = resize_location(y2, scale_h, min=0, max=new_h-1)
70+
new_mess = "{0} {1} {2} {3}\n".format(x1_new, y1_new, x2_new, y2_new)
7171
w_file.writelines(new_mess)
7272
r_file.close()
7373
w_file.close()

0 commit comments

Comments
 (0)