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 )
0 commit comments