|
a |
|
b/datasets/pretreatment_rgb.py |
|
|
1 |
import os |
|
|
2 |
from time import time |
|
|
3 |
from multiprocessing import Pool |
|
|
4 |
from tqdm import tqdm |
|
|
5 |
import numpy as np |
|
|
6 |
import os |
|
|
7 |
import pickle |
|
|
8 |
import numpy as np |
|
|
9 |
import cv2 |
|
|
10 |
from tqdm import tqdm |
|
|
11 |
|
|
|
12 |
SRC_0 = 'rgb_input_path' |
|
|
13 |
DST_0 = 'rgb_pkl_output_path' |
|
|
14 |
|
|
|
15 |
SRC = SRC_0 # Path_of_RGB_rearranged |
|
|
16 |
DST = DST_0 # Path_of_RGB_256128pkl_PadResized |
|
|
17 |
|
|
|
18 |
def resize_with_padding(img, target_size): |
|
|
19 |
h, w, _ = img.shape |
|
|
20 |
target_h, target_w = target_size |
|
|
21 |
resized_img = cv2.resize(img, (int(w * target_h / h), target_h)) |
|
|
22 |
padded_img = np.zeros((target_h, target_w, 3), dtype=np.uint8) |
|
|
23 |
x_offset = (target_w - resized_img.shape[1]) // 2 |
|
|
24 |
if x_offset < 0 : |
|
|
25 |
x_offset = abs(x_offset) |
|
|
26 |
padded_img = resized_img[:, x_offset:x_offset+target_w,:] |
|
|
27 |
else: |
|
|
28 |
padded_img[:, x_offset:x_offset + resized_img.shape[1]] = resized_img |
|
|
29 |
return padded_img |
|
|
30 |
|
|
|
31 |
def job(src, id): |
|
|
32 |
for ty in sorted(os.listdir(os.path.join(src, id))): |
|
|
33 |
for vi in sorted(os.listdir(os.path.join(src, id, ty))): |
|
|
34 |
exist_file = os.path.join(DST, id, ty, vi, vi+"-aligned-rgbs.pkl") |
|
|
35 |
if os.path.exists(exist_file): |
|
|
36 |
print('Have Passed: ' + DST + '/' + id + '/' + ty) |
|
|
37 |
continue |
|
|
38 |
ratios = [] |
|
|
39 |
aligned_imgs = [] |
|
|
40 |
for img_file in sorted(os.listdir(os.path.join(src, id, ty, vi))): |
|
|
41 |
img_path = os.path.join(src, id, ty, vi, img_file) |
|
|
42 |
img = cv2.imread(img_path) |
|
|
43 |
ratio = img.shape[1]/img.shape[0] |
|
|
44 |
ratios.append(ratio) |
|
|
45 |
aligned_img = np.transpose(cv2.cvtColor(resize_with_padding(img, (256, 128)), cv2.COLOR_BGR2RGB), (2, 0, 1)) |
|
|
46 |
aligned_imgs.append(aligned_img) |
|
|
47 |
if len(aligned_imgs) > 0: |
|
|
48 |
output_path = os.path.join(DST, id, ty, vi) |
|
|
49 |
os.makedirs(output_path, exist_ok=True) |
|
|
50 |
pickle.dump(np.asarray(aligned_imgs), open(os.path.join(output_path, vi+"-aligned-rgbs.pkl"), "wb")) |
|
|
51 |
pickle.dump(np.asarray(ratios), open(os.path.join(output_path, vi+"-ratios.pkl"), "wb")) |
|
|
52 |
print('Successfully saved: ' + DST + '/' + id + '/' + ty + '/' + vi) |
|
|
53 |
|
|
|
54 |
if __name__ == '__main__': |
|
|
55 |
a = time() |
|
|
56 |
po = Pool(8) |
|
|
57 |
src_path = SRC |
|
|
58 |
|
|
|
59 |
cnt = 0 |
|
|
60 |
need_data = sorted(os.listdir(src_path)) |
|
|
61 |
for id in tqdm(need_data[:]): |
|
|
62 |
po.apply_async(job,(src_path, id,)) |
|
|
63 |
cnt = cnt + 1 |
|
|
64 |
|
|
|
65 |
print('---START---') |
|
|
66 |
po.close() |
|
|
67 |
po.join() |
|
|
68 |
print(cnt) |
|
|
69 |
|
|
|
70 |
t = time() - a |
|
|
71 |
print('---END---{}'.format(t)) |