-
Notifications
You must be signed in to change notification settings - Fork 57
/
Preprocess.py
120 lines (77 loc) · 2.28 KB
/
Preprocess.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
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
import pandas as pd
import cv2
import os
import tqdm
from scipy.io import loadmat
import matplotlib.pyplot as plt
image_path = 'Data1/images/'
gt_path = 'Data1/ground_truth/'
train_image_paths = []
train_gt_paths = []
for new_file in tqdm.tqdm(os.listdir(gt_path)):
name_split = new_file.split('.')
image_name = name_split[0][3:]
image_name = image_name + '.jpg'
if 'gt' in new_file:
image_name = name_split[0][3:]
image_name = image_name + '.jpg'
path_img = os.path.join(image_path , image_name)
train_image_paths.append(path_img)
train_gt_paths.append(os.path.join(gt_path , new_file))
X_final = []
Y_final = []
grid_h = 16
grid_w = 16
img_w = 512
img_h = 512
for z in tqdm.tqdm(range(len(train_image_paths))):
new_file = train_image_paths[z]
#print(new_file)
x = cv2.imread(train_image_paths[z])
x_sl = 512/x.shape[1]
y_sl = 512/x.shape[0]
img = cv2.resize(x,(512,512))
X_final.append(img)
#plt.imshow(cv2.imread(new_file))
#plt.show()
i = " "
if 'img' in new_file:
i = ", "
Y = np.zeros((grid_h,grid_w,1,5))
file = train_gt_paths[z]
name = open(file , 'r')
data = name.read()
data = data.split("\n")
data = data[:-1]
for li in data:
temp_list = []
file_data = li.split(i)
strr = file_data[4]
bb = file_data[:4]
xmin = int(bb[0])*x_sl
xmax = int(bb[2])*x_sl
ymin = int(bb[1])*y_sl
ymax = int(bb[3])*y_sl
#te = cv2.rectangle(img,(int(xmin),int(ymin)),(int(xmax),int(ymax)) , color = (0,255,0))
w = (xmax - xmin)/img_w
h = (ymax - ymin)/img_h
x = ((xmax + xmin)/2)/img_w
y = ((ymax + ymin)/2)/img_h
x = x * grid_w
y = y * grid_h
Y[int(y),int(x),0,0] = 1
Y[int(y),int(x),0,1] = x - int(x)
Y[int(y),int(x),0,2] = y - int(y)
Y[int(y),int(x),0,3] = w
Y[int(y),int(x),0,4] = h
#plt.imshow(te)
#plt.show()
Y_final.append(Y)
X = np.array(X_final)
X_final = []
Y = np.array(Y_final)
Y_final = []
X = (X - 127.5)/127.5
np.save('Data1/X.npy',X)
np.save('Data1/Y.npy',Y)