-
Notifications
You must be signed in to change notification settings - Fork 0
/
dimension_stack_test.py
105 lines (94 loc) · 4.22 KB
/
dimension_stack_test.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import cv2
import numpy as np
from tqdm import tqdm
import concurrent.futures
from multiprocessing.pool import ThreadPool
import time
from PIL import Image
data_folder = os.path.join("..", "..", "data", "retail10k")
images_foldername = "images"
images_path = os.path.join(data_folder, images_foldername)
images = os.listdir(images_path)
images.sort()
for image in images:
imgs = []
img = cv2.imread(os.path.join(images_path, image))
img2 = cv2.imread(os.path.join(images_path, image))
imgs.append(img)
imgs.append(img2)
imgs.append(img)
all_images = np.concatenate([*imgs], axis=2)
a = 1
"""
def write_img_to_disk(imgID):
edge_image = cv2.imread(os.path.join(edges_path, imgID))
image = cv2.imread(os.path.join(images_path, imgID))
depth_path = os.path.join(depths_path, imgID[:-4] + depth_maks_extension)
depth_image = cv2.imread(depth_path)
#convert depth and edges to grayscale
if depth_to_grayscale:
depth_image = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
depth_image = np.expand_dims(depth_image_gray, axis=2)
#edge_image_gray = cv2.cvtColor(edge_image, cv2.COLOR_BGR2GRAY)
#edge_image_gray = np.expand_dims(edge_image_gray, axis=2)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #convert to RGB
stacked = np.concatenate((image, edge_image_gray, depth_image), axis=2)
np.save(os.path.join(data_folder, numpy_foldername, f"{imgID[:-4]}_7dimRGBCD.npy"), stacked)
#def jere_pad(img )
a = np.load(os.path.join(data_folder, numpy_foldername, "002ccf65cf100cc58438664b005482e5_5dimRGBCD.npy"))
a1 = np.copy(a)
a2 = np.copy(a)
#a_cv = cv2.imread(os.path.join(data_folder, numpy_foldername, "002ccf65cf100cc58438664b005482e5.jpg"))
b = a[:, :, 0:3]
b = cv2.cvtColor(b, cv2.COLOR_RGB2BGR)
color = (114, 114, 114)
color2= (114, 114, 114, 114, 114, 114, 114)
top, bottom, left, right = 0, 0, 100, 100
im = cv2.copyMakeBorder(b, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
a_2 = np.zeros((a.shape[0]+top+bottom, a.shape[1]+left+right, a.shape[2]), dtype=np.uint8)
n_2 = np.zeros((a.shape[0]+top+bottom, a.shape[1]+left+right, a.shape[2]), dtype=np.uint8)
for channel in range(a.shape[2]):
a_2[:, :, channel] = cv2.copyMakeBorder(a1[:, :, channel], top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0, 0, 0, 0])
n_2[:, :, channel] = np.pad(a2[:, :, channel], ((top, bottom), (left, right)), 'constant', constant_values=[[0, 0], [0, 0]])
#a_2[:,:,0:3] = cv2.cvtColor(a_2[:,:,0:3], cv2.COLOR_BGR2RGB)
n2 = n_2[:,:,0:3]
n2_canny = n_2[:,:,3:4]
n2_depth = n_2[:,:,4:5]
x2 = a_2[:,:,0:3]
x2_canny = a_2[:,:,3:4]
x2_depth = a_2[:,:,4:5]
x = a[:,:,0:3]
x_canny = a[:,:,3:4]
x_depth = a[:,:,4:5]
#im2 = cv2.copyMakeBorder(a, 0, 0, left, right, cv2.BORDER_CONSTANT, value=color2)
# add border
#b = cv2.cvtColor(b, cv2.COLOR_BGR2RGB)
c = Image.fromarray(b)
print(a.shape)
#TODO:sanity check na zapis i distribuciju ovi piksela u svakon kanalu neman pojma bokteeba
t1 = time.time()
with ThreadPool(20) as pool:
pool.map(write_img_to_disk, images, chunksize=10)
t2=time.time()
print(f"gotov sa zapisom u {round(t2-t1,2)} sekundi")
for imageID in tqdm(images):
#load all images independently
edge_image = cv2.imread(os.path.join(edges_path, imageID))
image = cv2.imread(os.path.join(images_path, imageID))
depth_path = os.path.join(depths_path, imageID[:-4] + depth_maks_extension)
depth_image = cv2.imread(depth_path)
#convert depth and edges to grayscale
if depth_to_grayscale:
depth_image_gray = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
depth_image_gray = np.expand_dims(depth_image_gray, axis=2)
edge_image_gray = cv2.cvtColor(edge_image, cv2.COLOR_BGR2GRAY)
edge_image_gray = np.expand_dims(edge_image_gray, axis=2)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #convert to RGB
#stack them all up in one big matrix
stacked = np.concatenate((image, edge_image_gray, depth_image_gray), axis=2)
#stacked = np.concatenate((image, edge_image_gray, depth_image), axis=2)
map(write_img_to_disk, [stacked], [os.path.join(data_folder, numpy_foldername, f"{imageID[:-4]}_5dimensionalRGBCD.npy")])
#np.save(os.path.join(data_folder, numpy_foldername, f"{imageID[:-4]}_7dimensionalRGBCD.npy"), stacked)
pass
"""