-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
89 lines (67 loc) · 2.83 KB
/
preprocess.py
File metadata and controls
89 lines (67 loc) · 2.83 KB
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
import pandas as pd
import numpy as np
import os
import glob
import cv2
from tqdm import tqdm
import warnings
warnings.filterwarnings(action='ignore')
x_img_paths = sorted(glob.glob('./data/train/x/*.png'))
y_img_paths = sorted(glob.glob('./data/train/y/*.png'))
df_simul = pd.DataFrame({'x':x_img_paths, 'y':y_img_paths})
from os.path import join as opj
removeFile = True
Train_dir_1 = f'./data/train/x'
os.makedirs(Train_dir_1, exist_ok=True)
Train_dir_2 = f'./data/train/y'
os.makedirs(Train_dir_2, exist_ok=True)
for idx in tqdm(range(len(df_simul))):
img = cv2.imread(df_simul.iloc[idx, 0])
imgname = df_simul.iloc[idx, 0].split('/')[-1]
im1 = img[:, :754]
im2 = img[:, 754:]
im1 = cv2.resize(im1, (320, 320), interpolation=cv2.INTER_NEAREST)
im2 = cv2.resize(im2, (320, 320), interpolation=cv2.INTER_NEAREST)
im1 = cv2.rotate(im1, cv2.ROTATE_90_CLOCKWISE)
im2 = cv2.rotate(im2, cv2.ROTATE_90_CLOCKWISE)
img2 = cv2.hconcat([im1, im2])
im1 = cv2.rotate(im1, cv2.ROTATE_90_CLOCKWISE)
im2 = cv2.rotate(im2, cv2.ROTATE_90_CLOCKWISE)
img3 = cv2.hconcat([im1, im2])
im1 = cv2.rotate(im1, cv2.ROTATE_90_CLOCKWISE)
im2 = cv2.rotate(im2, cv2.ROTATE_90_CLOCKWISE)
img4 = cv2.hconcat([im1, im2])
label = cv2.imread(df_simul.iloc[idx, 1], cv2.IMREAD_GRAYSCALE)
labelname = df_simul.iloc[idx, 1].split('/')[-1]
lab1 = label[:, :754]
lab2 = label[:, 754:]
lab1 = cv2.resize(lab1, (320, 320), interpolation=cv2.INTER_NEAREST)
lab2 = cv2.resize(lab2, (320, 320), interpolation=cv2.INTER_NEAREST)
lab1 = cv2.rotate(lab1, cv2.ROTATE_90_CLOCKWISE)
lab2 = cv2.rotate(lab2, cv2.ROTATE_90_CLOCKWISE)
label2 = cv2.hconcat([lab1, lab2])
lab1 = cv2.rotate(lab1, cv2.ROTATE_90_CLOCKWISE)
lab2 = cv2.rotate(lab2, cv2.ROTATE_90_CLOCKWISE)
label3 = cv2.hconcat([lab1, lab2])
lab1 = cv2.rotate(lab1, cv2.ROTATE_90_CLOCKWISE)
lab2 = cv2.rotate(lab2, cv2.ROTATE_90_CLOCKWISE)
label4 = cv2.hconcat([lab1, lab2])
save_path_1 = opj(Train_dir_1, f'r0_{imgname}')
save_path_2 = opj(Train_dir_2, f'r0_{labelname}')
save_path_3 = opj(Train_dir_1, f'r1_{imgname}')
save_path_4 = opj(Train_dir_2, f'r1_{labelname}')
save_path_5 = opj(Train_dir_1, f'r2_{imgname}')
save_path_6 = opj(Train_dir_2, f'r2_{labelname}')
save_path_7 = opj(Train_dir_1, f'r3_{imgname}')
save_path_8 = opj(Train_dir_2, f'r3_{labelname}')
cv2.imwrite(save_path_1, img)
cv2.imwrite(save_path_2, label)
cv2.imwrite(save_path_3, img2)
cv2.imwrite(save_path_4, label2)
cv2.imwrite(save_path_5, img3)
cv2.imwrite(save_path_6, label3)
cv2.imwrite(save_path_7, img4)
cv2.imwrite(save_path_8, label4)
if removeFile:
os.remove(df_simul.iloc[idx, 0])
os.remove(df_simul.iloc[idx, 1])