3 import sys, os, os.path, glob, math, cv2
4 from datetime import datetime
5 from optparse import OptionParser
12 if l.startswith("Bounding box"):
13 b = [x.strip() for x in l.split(":")[1].split("-")]
14 c = [x[1:-1].split(",") for x in b]
15 d = [int(x) for x in sum(c, [])]
18 if l.startswith("Image filename"):
19 path = os.path.join(os.path.join(ipath, ".."), l.split('"')[-2])
23 def adjust(box, tb, lr):
25 mix = int(round(box[0] - lr))
26 miy = int(round(box[1] - tb))
28 max = int(round(box[2] + lr))
29 may = int(round(box[3] + tb))
31 return [mix, miy, max, may]
33 if __name__ == "__main__":
34 parser = OptionParser()
35 parser.add_option("-i", "--input", dest="input", metavar="DIRECTORY", type="string",
36 help="path to Inria train data folder")
38 parser.add_option("-o", "--output", dest="output", metavar="DIRECTORY", type="string",
39 help="path to store data", default=".")
41 parser.add_option("-t", "--target", dest="target", type="string", help="should be train or test", default="train")
43 (options, args) = parser.parse_args()
45 parser.error("Inria data folder required")
47 if options.target not in ["train", "test"]:
48 parser.error("dataset should contain train or test data")
50 octaves = [-1, 0, 1, 2]
52 path = os.path.join(options.output, datetime.now().strftime("rescaled-" + options.target + "-%Y-%m-%d-%H-%M-%S"))
55 neg_path = os.path.join(path, "neg")
58 pos_path = os.path.join(path, "pos")
61 print "rescaled Inria training data stored into", path, "\nprocessing",
65 whole_mod_w = int(64 * octave) + 2 * int(20 * octave)
66 whole_mod_h = int(128 * octave) + 2 * int(20 * octave)
68 cpos_path = os.path.join(pos_path, "octave_%d" % each)
72 gl = glob.iglob(os.path.join(options.input, "annotations/*.txt"))
73 for image, boxes in [parse(options.input, open(__p)) for __p in gl]:
75 height = box[3] - box[1]
76 scale = height / float(96)
78 mat = cv2.imread(image)
79 mat_h, mat_w, _ = mat.shape
81 rel_scale = scale / octave
83 d_w = whole_mod_w * rel_scale
84 d_h = whole_mod_h * rel_scale
86 top_bottom_border = (d_h - (box[3] - box[1])) / 2.0
87 left_right_border = (d_w - (box[2] - box[0])) / 2.0
89 box = adjust(box, top_bottom_border, left_right_border)
90 inner = [max(0, box[0]), max(0, box[1]), min(mat_w, box[2]), min(mat_h, box[3]) ]
92 cropped = mat[inner[1]:inner[3], inner[0]:inner[2], :]
94 top = int(max(0, 0 - box[1]))
95 bottom = int(max(0, box[3] - mat_h))
96 left = int(max(0, 0 - box[0]))
97 right = int(max(0, box[2] - mat_w))
98 cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE)
99 resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h)
102 if round(math.log(scale)/math.log(2)) < each:
103 out_name = "_upscaled" + out_name
105 cv2.imwrite(os.path.join(cpos_path, "sample_%d" % idx + out_name), resized)
107 flipped = cv2.flip(resized, 1)
108 cv2.imwrite(os.path.join(cpos_path, "sample_%d" % idx + "_mirror" + out_name), flipped)
114 cneg_path = os.path.join(neg_path, "octave_%d" % each)
117 for each in [__n for __n in glob.iglob(os.path.join(options.input, "neg/*.*"))]:
118 img = cv2.imread(each)
119 min_shape = (1.5 * whole_mod_h, 1.5 * whole_mod_w)
121 if (img.shape[1] <= min_shape[1]) or (img.shape[0] <= min_shape[0]):
122 out_name = "negative_sample_%i_resized.png" % idx
124 ratio = float(img.shape[1]) / img.shape[0]
126 if (img.shape[1] <= min_shape[1]):
127 resized_size = (int(min_shape[1]), int(min_shape[1] / ratio))
129 if (img.shape[0] <= min_shape[0]):
130 resized_size = (int(min_shape[0] * ratio), int(min_shape[0]))
132 img = sft.resize_sample(img, resized_size[0], resized_size[1])
134 out_name = "negative_sample_%i.png" % idx
136 cv2.imwrite(os.path.join(cneg_path, out_name), img)