5
5
6
6
import cv2
7
7
import numpy as np
8
- from PIL import Image
9
8
10
9
11
10
def get_ext (path_list ):
@@ -145,7 +144,7 @@ def get_name_with_group_list(
145
144
return name_list
146
145
147
146
148
- def get_list_with_postfix (dataset_path : str , postfix : str ):
147
+ def get_list_with_suffix (dataset_path : str , suffix : str ):
149
148
name_list = []
150
149
if os .path .isfile (dataset_path ):
151
150
print (f" ++>> { dataset_path } is a file. <<++ " )
@@ -158,45 +157,12 @@ def get_list_with_postfix(dataset_path: str, postfix: str):
158
157
else :
159
158
print (f" ++>> { dataset_path } is a folder. <<++ " )
160
159
name_list = [
161
- os .path .splitext (f )[0 ] for f in os .listdir (dataset_path ) if f .endswith (postfix )
160
+ os .path .splitext (f )[0 ] for f in os .listdir (dataset_path ) if f .endswith (suffix )
162
161
]
163
162
name_list = list (set (name_list ))
164
163
return name_list
165
164
166
165
167
- def rgb_loader (path ):
168
- with open (path , "rb" ) as f :
169
- img = Image .open (f )
170
- return img .convert ("L" )
171
-
172
-
173
- def binary_loader (path ):
174
- assert os .path .exists (path ), f"`{ path } ` does not exist."
175
- with open (path , "rb" ) as f :
176
- img = Image .open (f )
177
- return img .convert ("L" )
178
-
179
-
180
- def load_data (pre_root , gt_root , name , postfixs ):
181
- pre = binary_loader (os .path .join (pre_root , name + postfixs [0 ]))
182
- gt = binary_loader (os .path .join (gt_root , name + postfixs [1 ]))
183
- return pre , gt
184
-
185
-
186
- def normalize_pil (pre , gt ):
187
- gt = np .asarray (gt )
188
- pre = np .asarray (pre )
189
- gt = gt / (gt .max () + 1e-8 )
190
- gt = np .where (gt > 0.5 , 1 , 0 )
191
- max_pre = pre .max ()
192
- min_pre = pre .min ()
193
- if max_pre == min_pre :
194
- pre = pre / 255
195
- else :
196
- pre = (pre - min_pre ) / (max_pre - min_pre )
197
- return pre , gt
198
-
199
-
200
166
def make_dir (path ):
201
167
if not os .path .exists (path ):
202
168
print (f"`{ path } ` does not exist,we will create it." )
@@ -206,16 +172,13 @@ def make_dir(path):
206
172
print (f"`{ path } `已存在" )
207
173
208
174
209
- def imread_wich_checking (path , for_color : bool = True , with_cv2 : bool = True ) -> np .ndarray :
175
+ def imread_with_checking (path , for_color : bool = True ) -> np .ndarray :
210
176
assert os .path .exists (path = path ) and os .path .isfile (path = path ), path
211
- if with_cv2 :
212
- if for_color :
213
- data = cv2 .imread (path , flags = cv2 .IMREAD_COLOR )
214
- data = cv2 .cvtColor (data , cv2 .COLOR_BGR2RGB )
215
- else :
216
- data = cv2 .imread (path , flags = cv2 .IMREAD_GRAYSCALE )
177
+ if for_color :
178
+ data = cv2 .imread (path , flags = cv2 .IMREAD_COLOR )
179
+ data = cv2 .cvtColor (data , cv2 .COLOR_BGR2RGB )
217
180
else :
218
- data = np . array ( Image . open ( path ). convert ( "RGB" if for_color else "L" ) )
181
+ data = cv2 . imread ( path , flags = cv2 . IMREAD_GRAYSCALE )
219
182
return data
220
183
221
184
@@ -233,8 +196,8 @@ def get_gt_pre_with_name(
233
196
img_path = os .path .join (pre_root , pre_prefix + img_name + pre_suffix )
234
197
gt_path = os .path .join (gt_root , gt_prefix + img_name + gt_suffix )
235
198
236
- pre = imread_wich_checking (img_path , for_color = False )
237
- gt = imread_wich_checking (gt_path , for_color = False )
199
+ pre = imread_with_checking (img_path , for_color = False )
200
+ gt = imread_with_checking (gt_path , for_color = False )
238
201
239
202
if pre .shape != gt .shape :
240
203
pre = cv2 .resize (pre , dsize = gt .shape [::- 1 ], interpolation = cv2 .INTER_LINEAR ).astype (
@@ -271,8 +234,8 @@ def get_gt_pre_with_name_and_group(
271
234
img_path = os .path .join (pre_root , pre_prefix + file_name + pre_suffix )
272
235
gt_path = os .path .join (gt_root , gt_prefix + file_name + gt_suffix )
273
236
274
- pre = imread_wich_checking (img_path , for_color = False )
275
- gt = imread_wich_checking (gt_path , for_color = False )
237
+ pre = imread_with_checking (img_path , for_color = False )
238
+ gt = imread_with_checking (gt_path , for_color = False )
276
239
277
240
if pre .shape != gt .shape :
278
241
pre = cv2 .resize (pre , dsize = gt .shape [::- 1 ], interpolation = interpolation ).astype (np .uint8 )
0 commit comments