-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtransform_data.py
67 lines (60 loc) · 2.13 KB
/
transform_data.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
import numpy as np
import codecs
import cv2
import matplotlib.pylab as plt
import read_data
import config
train_images_dir = config.train_images_dir
train_labels_dir = config.train_labels_dir
save_images_dir = "train_images/resize_images/"
save_labels_dir = "train_images/resize_ground_truth/"
def test_main():
image = cv2.imread(train_images_dir + "img_1.jpg")
shape = image.shape
image_resized = cv2.resize(image, (512, 512))
cv2.imwrite(save_images_dir + "img_1.jpg", image_resized)
import IPython
labels = read_data.read_ground_truth(train_labels_dir, 1)
j = 0
IPython.embed()
for box in labels[0]:
img_crop = image[box[1]: box[5], box[0]:box[4]]
cv2.imwrite(save_images_dir + str(j) + "img.jpg", img_crop)
j += 1
for box in labels[0]:
for i in range(0, 8, 2):
box[i] = (int)(box[i] * 512 / shape[1])
for i in range(1, 8, 2):
box[i] = (int)(box[i] * 512 / shape[0])
img_crop = image_resized[box[1]: box[5], box[0]:box[4]]
cv2.imwrite(save_images_dir + str(j) + "img.jpg", img_crop)
j += 1
def resize_images():
all_images = read_data.read_datasets(train_images_dir, config.all_trains, order="hwc")
i = 1
for image in all_images:
image = cv2.resize(image, (512, 512))
cv2.imwrite(save_images_dir + "img_" + str(i) + ".jpg", image)
if i % 100 == 0:
print(i)
i += 1
def resize_labels():
all_labels = read_data.read_ground_truth(train_labels_dir, config.all_trains)
image_width = config.image_width
image_height = config.image_width
j = 1
for boxes in all_labels:
with open(save_labels_dir + "gt_img_" + str(j) + ".txt", "w") as f:
for box in boxes:
for i in range(0, 8, 2):
box[i] = (int)(box[i] * 512 / image_width )
for i in range(1, 8, 2):
box[i] = (int)(box[i] * 512 / image_height)
for num in box:
f.write(str(num) + ", ")
f.write("\n")
j += 1
def main():
resize_labels()
if __name__ == "__main__":
main()